\IfFileExists{lucimatx.sty}{
  % ----- if Lucida availalable------------------
  \usepackage[
    scale=0.875,
    stdmathitalics=true,
    stdmathdigits=true]{lucimatx}
 \linespread{1.02}
}{
  % ------ if Lucida not available --------------
  \usepackage{times}
  \usepackage{amssymb}
}

\usepackage{makeidx}
\makeindex

% \usepackage{index}
% \newindex{funs}{fdx}{fns}{Functions}

\usepackage{amsmath}
\usepackage{xspace}
\usepackage{fancyvrb}
%\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage{url}
\usepackage{changebar}
\usepackage{alltt}
\usepackage{lscape}

\usepackage[titles]{tocloft}
\setlength{\cftbeforechapskip}{0.5ex}
\setlength{\cftbeforesecskip}{0.5ex}
\renewcommand{\cftchapfont}{\normalsize\rm}
\renewcommand{\cftchappagefont}{\normalsize\rm}
\renewcommand{\cftpartfont}{\normalsize\rm\bfseries}
\renewcommand{\cftpartpagefont}{\normalsize\rm\bfseries}
\renewcommand{\cftchapaftersnum}{.\hspace*{12pt}}
\renewcommand{\cftchapaftersnumb}{\hspace*{8pt}}

\setcounter{tocdepth}{0}

\titleformat{\chapter}{\bfseries\LARGE}{\thechapter.}{1.5pc}{}{}%{1.5pc}{}{}
\titlespacing{\chapter}{0pt}{-24pt}{24pt}{}
%
\titleformat{\section}{\bfseries\large}{\thesection.}{1em}{}
%\titlespacing{\section}{0pt}{12pt}{6pt}
%
\titleformat{\subsection}{\bfseries}{}{0em}{}
%\titlespacing{\subsection}{-1em}{12pt}{6pt}
\titleformat{\subsubsection}{\slshape}{}{0em}{}
%\titlespacing{\subsection}{-1em}{12pt}{6pt}

\usepackage{color}
\definecolor{linkcolor}{RGB}{0,0,128}
\usepackage[pagebackref,pdfpagelabels,plainpages=false,hypertexnames=true,colorlinks=true,linkcolor=linkcolor,anchorcolor=linkcolor,citecolor=linkcolor,filecolor=linkcolor,menucolor=linkcolor,runcolor=linkcolor,urlcolor=linkcolor,pdfborder={0 0 0}]{hyperref}


% BG: Stuff for changed text
\usepackage[final,author=]{fixme}
\fxuselayouts{pdfcmargin}
\fxusetargetlayout{changebar}
\newcommand{\A}[1]{\cbstart#1\cbend}
\newcounter{FXNOTE}[page]
\newcommand{\FXA}[1][\value{FXNOTE}]{\ifnum#1=0\fxnote{Addition to section \thesection}\stepcounter{FXNOTE}\fi}
\newcommand{\FXC}[1][\value{FXNOTE}]{\ifnum#1=0\fxnote{Change to section \thesection}\stepcounter{FXNOTE}\fi}
\renewcommand\englishlistfixmename{List of changes}

\newcommand{\Stan}{Stan\xspace}
\newcommand{\stanc}{{\ttfamily stanc}\xspace}
\newcommand*{\Cpp}{C\raise.2ex\hbox{\footnotesize ++}\xspace} %\ensuremath{++}
\newcommand{\clang}{{\ttfamily clang\raise.2ex\hbox{\footnotesize ++}}\xspace} 
\newcommand{\gpp}{{\ttfamily g\raise.2ex\hbox{\footnotesize ++}}\xspace} 

\newcommand{\acronym}[1]{#1\xspace}

\newcommand{\ASCII}{\acronym{ASCII}}
\newcommand{\BNF}{\acronym{BNF}}
\newcommand{\MATLAB}{\acronym{MATLAB}}
\newcommand{\R}{\acronym{R}}
\newcommand{\SPLUS}{\acronym{S}}
\newcommand{\BUGS}{\acronym{BUGS}}
\newcommand{\JAGS}{\acronym{JAGS}}
\newcommand{\MCMC}{\acronym{MCMC}}
\newcommand{\HMC}{\acronym{HMC}}
\newcommand{\NUTS}{\acronym{NUTS}}
\newcommand{\MSVC}{\acronym{MSVC}}
\newcommand{\LKJ}{\acronym{LKJ}}
\newcommand{\CPC}{\acronym{CPC}}

\newcommand{\code}[1]{{\tt #1}}
\newcommand{\mycaption}[2]{\caption{{\it #2}\label{#1.figure}}}

\newcommand{\distro}[1]{\mbox{\sf #1}}
\newcommand{\Betafun}{\mbox{B}}
\newcommand{\setlist}[1]{\{ #1 \}}
\newcommand{\reals}{\mathbb{R}}
\newcommand{\posreals}{\mathbb{R}^+}
\newcommand{\nats}{\mathbb{N}}
\newcommand{\bigo}[1]{{\mathcal{O}}(#1)}
\newcommand{\erfc}[1]{\operatorname{erfc}#1}
\newcommand{\erf}[1]{\operatorname{erf}#1}

\newcommand{\refappendix}[1]{Appendix~\ref{#1.appendix}}
\newcommand{\refpart}[1]{Part~\ref{#1.part}}
\newcommand{\refchapter}[1]{Chapter~\ref{#1.chapter}}
\newcommand{\refsection}[1]{Section~\ref{#1.section}}
\newcommand{\reffigure}[1]{Figure~\ref{#1.figure}}
\newcommand{\refnote}[1]{Footnote~\ref{#1.footnote}}


\newcommand{\fitem}[4]{\item[\begin{minipage}{\textwidth}{\tt #1 {\bfseries #2}(#3)}\end{minipage}]\mbox{ }
 \\[-10pt] #4\index{{\tt\bfseries #2 }!{\tt (#3):\,#1}|hyperpage}}
% need special command for items requiring escapes in index
\newcommand{\fitemindex}[5]{\item[{\tt #1 {\bfseries #2}(#3)}]\mbox{ }
  \\[2pt] #4\index{{\tt\bfseries #5 }!{\tt (#3):\,#1}|hyperpage}}
\newcommand{\fitemindexsort}[6]{\item[{\tt #1 {\bfseries #2}(#3)}]\mbox{ }
  \\[2pt] #4\index{{\tt\bfseries #6 }@{\tt\bfseries #5 }!{\tt (#3):\,#1}|hyperpage}}
\newcommand{\farg}[1]{{\tt\slshape #1}}

\newcommand{\pitem}[3]{\subsubsection{Sampling Statement}
\begin{description}
\item {\tt \farg{#1} \textasciitilde\ {\bfseries #2}(\farg{#3});}
  \\
  Increment log probability with {\tt #2\_log(\farg{#1},\farg{#3})},
  dropping constant additive terms; \refsection{sampling-statements}
  explains sampling statements.%
\index{{\tt\bfseries #2 }!sampling statement|hyperpage}
\end{description}}

\newcommand{\indentarrow}{ \rotatebox[origin=c]{180}{\ensuremath{\Lsh}} }

\newcommand{\hiershortcmd}[3]{\item[#1 \tt #2] \mbox{ } \\ #3}
\newcommand{\hierlongcmd}[4]{\item[#1 \tt #2] \mbox{ } \\ #3 \\ #4}
\newcommand{\hiercmdarg}[6]{\item[#1 \tt #2={\slshape <#3>}] \mbox{ } \\ #4 \\ #5 \\ (#6)}

\newcommand{\shortcmd}[2]{ \hiershortcmd{}{#1}{#2} }
\newcommand{\longcmd}[3]{ \hierlongcmd{}{#1}{#2}{#3} }
\newcommand{\cmdarg}[5]{ \hiercmdarg{}{#1}{#2}{#3}{#4}{#5} }

\newcommand{\samp}{{\lower.78ex\hbox{\texttt{\char`\~}}}}%
%{{\lower1.1ex\hbox{\Large\texttt{\char`\~}}}}

% iPad: 3w x 4h, 

\setlength{\paperwidth}{6in}
\setlength{\paperheight}{8in}
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight

\setlength{\textwidth}{5in}
\setlength{\textheight}{6.875in}
\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{-0.875in}
\setlength{\headsep}{18pt}

\raggedbottom

\sloppy % no line overruns

