% Scriven Academic LaTeX Template for Pandoc --template flag
% Usage: pandoc ... --template=data/export-templates/scriven-academic.latex
%
% Supports article (default), report, or book document class.
% Bibliography via biblatex/biber with citeproc compatibility.

% Document class: article by default, configurable via Pandoc variable
\documentclass[$if(fontsize)$$fontsize$$else$12pt$endif$,$if(documentclass-options)$$documentclass-options$$else$a4paper$endif$]{$if(documentclass)$$documentclass$$else$article$endif$}

% === Packages ===

% Encoding and fonts
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
$if(mainfont)$
\usepackage{fontspec}
\setmainfont{$mainfont$}
$endif$

% Page geometry: 1-inch margins
\usepackage[$if(geometry)$$geometry$$else$margin=1in$endif$]{geometry}

% Language support
$if(language)$
\usepackage[$language$]{babel}
$else$
\usepackage[english]{babel}
$endif$

% Line spacing
\usepackage{setspace}
$if(linestretch)$
\setstretch{$linestretch$}
$else$
\setstretch{1.5}
$endif$

% Headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\leftmark}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}

% Section numbering
$if(numbersections)$
\setcounter{secnumdepth}{$if(secnumdepth)$$secnumdepth$$else$3$endif$}
$else$
\setcounter{secnumdepth}{3}
$endif$

% 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
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}

% Tables
\usepackage{longtable,booktabs,array}

% Lists
\usepackage{enumitem}

% Colors (for links)
\usepackage[usenames,dvipsnames]{xcolor}

% Hyperlinks
\usepackage[
  colorlinks=true,
  linkcolor=NavyBlue,
  citecolor=NavyBlue,
  urlcolor=NavyBlue,
  bookmarks=true,
  bookmarksnumbered=true,
  pdfstartview=FitH
]{hyperref}

% Math support
\usepackage{amsmath,amssymb}

% Code blocks
$if(highlighting-macros)$
$highlighting-macros$
$endif$

% Tight lists (Pandoc uses these)
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% CSL references (citeproc compatibility)
$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2]%
  {\setlength{\parindent}{0pt}%
   \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces}%
  {\par}
\newcommand{\CSLBlock}[1]{\hfill\break#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\dimexpr\linewidth-\csllabelwidth\relax}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$

% Bibliography via biblatex (if bibliography file provided)
$if(bibliography)$
\usepackage[
  backend=biber,
  style=$if(biblio-style)$$biblio-style$$else$authoryear$endif$,
  sorting=nyt,
  maxcitenames=2,
  maxbibnames=99,
  natbib=true
]{biblatex}
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$

% === Document metadata ===

\title{$if(title)$$title$$if(subtitle)$\\[0.5em]\large $subtitle$$endif$$else$Untitled$endif$}

\author{$if(author)$$for(author)$$if(it.name)$$it.name$$else$$it$$endif$$sep$ \and $endfor$$else$~$endif$}

\date{$if(date)$$date$$else$\today$endif$}

% === Document ===

\begin{document}

% Title page
\maketitle

% Abstract
$if(abstract)$
\begin{abstract}
\noindent $abstract$
\end{abstract}
$endif$

$if(keywords)$
\vspace{1em}
\noindent\textbf{Keywords:} $keywords$
$endif$

% Table of contents
$if(toc)$
\newpage
\tableofcontents
\newpage
$endif$

% List of figures
$if(lof)$
\listoffigures
$endif$

% List of tables
$if(lot)$
\listoftables
$endif$

% Body
$body$

% Bibliography
$if(bibliography)$
\newpage
\printbibliography[title={References}]
$endif$

% Appendix
$if(appendix)$
\appendix
$appendix$
$endif$

\end{document}
