\documentclass[$if(fontsize)$$fontsize$$else$11pt,$endif$$if(lang)$$lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}

$if(fontfamily)$
\usepackage{$fontfamily$}
$else$
\usepackage{lmodern}
$endif$
$if(linestretch)$
\usepackage{setspace}
\setstretch{$linestretch$}
$endif$

\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
$if(euro)$
  \usepackage{eurosym}
$endif$
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{xltxtra, xunicode}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Mapping=tex-text, Scale=MatchLowercase}
  \newcommand{\euro}{€}
$if(mainfont)$
    \setmainfont{$mainfont$}
$endif$
$if(sansfont)$
    \setsansfont{$sansfont$}
$endif$
$if(monofont)$
    \setmonofont[Mapping=tex-ansi]{$monofont$}
$endif$
$if(mathfont)$
    \setmathfont(Digits,Latin,Greek){$mathfont$}
$endif$
\fi

% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$else$
\usepackage[top=0.5in, bottom=.4in, left = 1in, right=1in, includeheadfoot]{geometry}
$endif$
\ifxetex
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
              xetex]{hyperref}
\else
  \usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
            bookmarks=true,
            pdfauthor={$author-meta$},
            pdftitle={$title-meta$},
            colorlinks=true,
            citecolor=$if(citecolor)$$citecolor$$else$blue$endif$,
            urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
            linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls
$if(lang)$
\ifxetex
  \usepackage{polyglossia}
  \setmainlanguage{$mainlang$}
  \setotherlanguages{$for(otherlang)$$otherlang$$sep$,$endfor$}
\else
  \usepackage[shorthands=off,$lang$]{babel}
\fi
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage{biblatex}
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$
$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{cslreferences}
  {$if(csl-hanging-indent)$\setlength{\parindent}{0pt}
  \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}
  {\par}
$endif$
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
\VerbatimFootnotes
$endif$
\usepackage{longtable,booktabs}
$if(graphics)$
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
$endif$
$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
% $if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
% \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
% $endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
$if(numbersections)$
\setcounter{secnumdepth}{5}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(verbatim-in-note)$
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% % Create subtitle command for use in maketitle
% \newcommand{\subtitle}[1]{
%   \posttitle{\large}
% }

% \setlength{\droptitle}{-2em}
$if(title)$
  \title{$title$}
  % \pretitle{\vspace{\droptitle}\centering\huge}
$else$
  \title{}
  % \pretitle{\vspace{\droptitle}}
$endif$
$if(author)$
  \author{$for(author)$$author$$sep$ \\ $endfor$}
  % \preauthor{\centering\large\emph}
  % \postauthor{\par}
$else$
  \author{}
  % \preauthor{}\postauthor{}
$endif$
$if(date)$
  % \predate{\centering\large\emph}
  % \postdate{\par}
  \date{$date$}
$else$
  \date{}
  % \predate{}\postdate{}
$endif$

%% --------- above is almost identical with default rmarkdown
%% document formatting
 % colors for tables and text
\usepackage{ragged2e} % justifying text
\usepackage{setspace} % spacing commands, automatically makes captions single-spaced
  \setstretch{1.2} \frenchspacing
\usepackage{lastpage} % access number of last page for numbering in margin

%% fonts setup
$if(libertine)$
\usepackage{libertine}
$endif$
$if(chinese)$
\usepackage{ctex}
$endif$
\usepackage{gensymb} %degree symbol
\usepackage{array}
\usepackage{multirow}
\usepackage{xcolor}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage{makecell}
\usepackage[hang]{footmisc}

%% tables and figures

$if(numbersections)$
% include section number in figure and table numbers, renew in each section
\usepackage{chngcntr}
\counterwithin{table}{section}
\counterwithin{figure}{section}
$endif$

\robustify\tnote

% table/figure captions (titles and legends)

% \usepackage{caption}
% $if(fancy_captions)$
% \DeclareCaptionFormat{llap}{\llap{#1#2}#3\par} % table / figure number in left margin
% \DeclareCaptionLabelSeparator{spc}{\hspace{.075 in}} % space between table number and caption
% \captionsetup{font={color=$if(citecolor)$$citecolor$$else$blue$endif$, sf}, singlelinecheck=off, margin= 15pt, skip=4pt, size=small, labelfont={sc, sf}, format=llap, labelsep=spc,singlelinecheck=no} % same font as headers
% $else$
% otherwise just
% \captionsetup{font=small, labelfont={sc, bf}}
% $endif$

% table/figure captions (titles and legends)
\usepackage[singlelinecheck=false]{caption} % left justify table captions
\captionsetup{font={color=$if(tabcapcolor)$$tabcapcolor$$else$black$endif$, sf}, skip=$if(tabcapskip)$$tabcapskip$$else$4pt$endif$, size=small, labelfont=sf, labelsep = period} % color and spacing between caption and table proper, updated 

\makeatletter
\let\runtitle\@title
\makeatother

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
$if(company)$
\rhead{\footnotesize \nouppercase{\sc $if(short_title)$$short_title$$else$$title$$endif$} \hspace{.025 in} \hspace{.055 in} \thepage \hspace{.01 in} / \pageref*{LastPage}}
$else$
\rhead{\footnotesize \nouppercase{\sc $if(short_title)$$short_title$$else$$title$$endif$}}
$endif$

   $if(confidential)$
   \lhead{\textcolor{red}{\it Confidential; do not circulate}}
   $endif$

   $if(company)$
   \renewcommand{\footrulewidth}{0.5pt}
   \cfoot{\footnotesize
   $company$
   }
   $else$
   \renewcommand{\footrulewidth}{0pt}
   \rfoot{\footnotesize \thepage}
   $endif$

\fancypagestyle{firststyle}
{
   \fancyhf{}
   $if(company)$
   \renewcommand{\footrulewidth}{0.5pt}
   \cfoot{\footnotesize
   $company$
   }
   $else$
   \renewcommand{\footrulewidth}{0pt}
   $endif$
}

\fancypagestyle{nofooter}
{
   \fancyfoot{}
   \renewcommand{\footrulewidth}{0.0pt}
}

\setlength\parindent{0pt}

\usepackage{titlesec}
\titleformat*{\section}{\sc \large \color[rgb]{0,0,0}}
\titleformat*{\subsection}{\large}
\titleformat*{\subsubsection}{\normalsize}
\titlespacing\section{0pt}{8pt plus 2pt minus 2pt}{0pt plus 2pt minus 2pt}
\titlespacing\subsection{0pt}{6pt plus 2pt minus 2pt}{-2pt plus 2pt minus 2pt}
\titlespacing\subsubsection{0pt}{2pt plus 2pt minus 2pt}{-3pt plus 2pt minus 2pt}


% set up title page
\pretitle{
$if(logo)$
\begin{flushright}
\includegraphics[height=$logo_height$, keepaspectratio]{$logo$}
\end{flushright}
$endif$
\begin{flushleft}\LARGE
}

$if(subtitle)$
\posttitle{
  \end{flushleft}
  \begin{flushleft}\large \vskip -1.5em $subtitle$ \end{flushleft}
}
$else$
\posttitle{
  \end{flushleft}
}
$endif$

\preauthor{\par\begin{flushleft}\large\vskip -.5em}
\postauthor{\end{flushleft}}

\predate{\par\begin{flushleft}\large\vskip -1em}
\postdate{\end{flushleft}  }

\makeatletter
\def\@seccntformat#1{\llap{\csname the#1\endcsname \hspace{.075 in}}}
\makeatother

% suppress chapter in section numbering
\renewcommand*\thesection{\arabic{section}}

% spacing of bulleted lists
\usepackage{enumitem} %control spacing of enumerated items
\setlist[itemize]{noitemsep, topsep=0pt}

%% remove 'abstract' title, justify text
\renewcommand{\abstractname}{\large}
\renewenvironment{abstract} {\abstractname \justifying \rm \normalsize }

\newlength\tbspace
\setlength\tbspace{.25in}

% units.
%\usepackage{units}

$if(highlighting-macros)$
%% change fontsize of R code
\let\oldShaded\Shaded
\let\endoldShaded\endShaded
\renewenvironment{Shaded}{\footnotesize\oldShaded}{\endoldShaded}

%% change fontsize of output
\let\oldverbatim\verbatim
\let\endoldverbatim\endverbatim
\renewenvironment{verbatim}{\footnotesize\oldverbatim}{\endoldverbatim}
$endif$

\def\tightlist{}


$if(watermark)$
\usepackage{tikz}
\usepackage[printwatermark]{xwatermark}
\newsavebox\mybox
\savebox\mybox{\tikz[color=$watermark_color$,opacity=0.2]\node{$watermark$};}
\newwatermark*[
  allpages,
  angle=45,
  scale=6,
  xpos=-20,
  ypos=15
]{\usebox\mybox}
$endif$

% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

$for(header-includes)$
$header-includes$
$endfor$

\begin{document}
$if(title)$
\maketitle
$endif$

$if(footer_on_first_page)$
\thispagestyle{firststyle}
$endif$

$if(abstract)$
\begin{abstract}
$abstract$ \clearpage
\end{abstract}
$endif$

$for(include-before)$
$include-before$

$endfor$
$if(toc)$
{
\hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$black$endif$}
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$
$if(lot)$
\listoftables
$endif$
$if(lof)$
\listoffigures
$endif$

$body$

$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}

$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

$endif$
$for(include-after)$
$include-after$

$endfor$
\end{document}
