% Copyright 2019 Ekaitz Zarraga <ekaitz@elenq.tech>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
%     http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ElenQ Article template
% -----------------------------------------------------------------------------
%
% Expects some metadata to be set:
%
%   polyglossia-lang:
%       name: english | spanish | basque | ...
%   lang: en-US | es-ES | ...
%   title: The title of the document
%   subtitle: The subtitle of the article
%   author: Author's name
%   license: License of the document
%
% It also accepts some optional metadata:
%
%   toc: true | false  # Activate table of contents (defaults to false)
%   abstract: "Content of the abstract"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[a4paper,11pt
$if(lang)$
  $babel-lang$,
$endif$]{$documentclass$}

% ElenQ logos
\newcommand{\elenqtechsmall}{
  \includegraphics[width=60pt]{$elenq-directory$/elenq-technology-256.png} }
\newcommand{\elenqtechmedium}{
  \includegraphics[width=90pt]{$elenq-directory$/elenq-technology-256.png} }
\newcommand{\elenqtechlarge}{
  \includegraphics[width=120pt]{$elenq-directory$/elenq-technology-512.png} }

\newcommand{\elenqpubsmall}{
  \includegraphics[width=60pt]{$elenq-directory$/elenq-publishing-128.png} }
\newcommand{\elenqpubmedium}{
  \includegraphics[width=90pt]{$elenq-directory$/elenq-publishing-256.png} }
\newcommand{\elenqpublarge}{
  \includegraphics[width=120pt]{$elenq-directory$/elenq-publishing-512.png} }

% References
$if(csl-refs)$
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newlength{\cslentryspacingunit} % times entry-spacing
\setlength{\cslentryspacingunit}{\parskip}
\newenvironment{CSLReferences}[2] % #1 hanging-ident, #2 entry spacing
 {% don't indent paragraphs
  \setlength{\parindent}{0pt}
  % turn on hanging indent if param 1 is 1
  \ifodd #1
  \let\oldpar\par
  \def\par{\hangindent=\cslhangindent\oldpar}
  \fi
  % set entry spacing
  \setlength{\parskip}{#2\cslentryspacingunit}
 }%
 {}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{#1\hfill\break}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{#1}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{#1}\break}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
$endif$

$if(lang)$
  % Load polyglossia as late as possible: uses bidi with RTL langages (e.g. Hebrew, Arabic)
  \usepackage{polyglossia}
  \setmainlanguage[$polyglossia-lang.options$]{$polyglossia-lang.name$}
  $for(polyglossia-otherlangs)$
    \setotherlanguage[$polyglossia-otherlangs.options$]{$polyglossia-otherlangs.name$}
  $endfor$
$endif$

% No indent
\usepackage{parskip}

% Margins
\usepackage[top=2.5cm,
            bottom=2.5cm,
            left=3.25cm,
            right=2.5cm,
            headheight=30pt,
            includeheadfoot=true]{geometry}

% Spacing
\usepackage{setspace}

% Font
\usepackage{anyfontsize} % para seleccionar tamaño de fuente
\usepackage{fontspec}
\setmainfont[
  BoldFont        = Lato-Bold.ttf,
  ItalicFont      = Lato-Italic.ttf,
  BoldItalicFont  = Lato-BoldItalic.ttf
]{Lato-Regular.ttf}

\setmonofont{B612 Mono}[Scale = 0.90]
\newfontfamily\code{B612 Mono}[
  NFSSFamily      = b612,
  Scale           = 0.90,
]
\newcommand{\euro}{€}

% Bibliography
$if(natbib)$
\usepackage[$natbiboptions$]{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex}
$for(bibliography)$
\addbibresource{$bibliography$}
$endfor$
$endif$

% Pretty hyperlinks
\usepackage{hyperref}
$if(colorlinks)$
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref
$endif$
\hypersetup{unicode=true,
$if(title-meta)$
            pdftitle={$title-meta$},
$endif$
$if(author-meta)$
            pdfauthor={$author-meta$},
$endif$
$if(lang)$
            pdflang={$lang$},
$endif$
$if(keywords)$
            pdfkeywords={$for(keywords)$$keywords$$sep$; $endfor$},
$endif$
$if(colorlinks)$
            colorlinks=true,
            linkcolor=$if(linkcolor)$$linkcolor$$else$Maroon$endif$,
            citecolor=$if(citecolor)$$citecolor$$else$Blue$endif$,
            urlcolor=$if(urlcolor)$$urlcolor$$else$Blue$endif$,
$else$
            pdfborder={0 0 0},
$endif$
            breaklinks=true}

% Hyperlinks as footnotes control from Metadata
$if(links-as-notes)$
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$

% Verbatim in footnotes control from Metadata
$if(verbatim-in-note)$
\usepackage{fancyvrb}
\VerbatimFootnotes
$endif$

% Syntax highlight
\usepackage{xcolor}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
  commandchars=\\\{\},
  fontsize=\footnotesize,
  fontfamily=b612}
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textbf{#1}}
\newcommand{\AnnotationTok}[1]{\textit{#1}}
\newcommand{\AttributeTok}[1]{#1}
\newcommand{\BaseNTok}[1]{#1}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textit{#1}}
\newcommand{\CommentVarTok}[1]{\textit{#1}}
\newcommand{\ConstantTok}[1]{#1}
\newcommand{\ControlFlowTok}[1]{\textbf{#1}}
\newcommand{\DataTypeTok}[1]{\underline{#1}}
\newcommand{\DecValTok}[1]{#1}
\newcommand{\DocumentationTok}[1]{\textit{#1}}
\newcommand{\ErrorTok}[1]{\textbf{#1}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{#1}
\newcommand{\FunctionTok}[1]{#1}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textit{#1}}
\newcommand{\KeywordTok}[1]{\textbf{#1}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{#1}
\newcommand{\OtherTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textbf{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{#1}
\newcommand{\SpecialStringTok}[1]{#1}
\newcommand{\StringTok}[1]{#1}
\newcommand{\VariableTok}[1]{#1}
\newcommand{\VerbatimStringTok}[1]{#1}
\newcommand{\WarningTok}[1]{\textit{#1}}


% Multiple page tables if needed
$if(tables)$
\usepackage{longtable,booktabs,array}
$if(multirow)$
\usepackage{multirow}
$endif$
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
$endif$

% Strikeout text if needed
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$

% Custom header
\usepackage{fancyhdr} % man http://osl.ugr.es/CTAN/macros/latex/contrib/fancyhdr/fancyhdr.pdf

% No 'chapter' word
%\addto\captionsenglish{\renewcommand\chaptername{}}

% Scale images to avoid overflow by default
\usepackage{textcomp}
\usepackage{graphicx,grffile}
\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}

% Copyleft symbol
\newcommand{\copyleft}{\reflectbox{\copyright}}

% Prevent overfull lines
\setlength{\emergencystretch}{3em}
\providecommand{\tightlist}{
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% Redefine (sub)paragraphs to behave more like sections
$if(subparagraph)$
$else$
\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
$endif$

\pagestyle{fancy}
% Header
\fancyhead{} % clear header
\fancyhead[RO]{\slshape \rightmark}
\fancyhead[LE]{\slshape \leftmark}

% Footer
\fancyfoot{} % clear footer
\renewcommand{\footrulewidth}{0.4pt}
\fancyfoot[RO, LE]{\thepage}
\fancyfoot[LO, RE]{$if(license)$\textcopyleft $endif$ $author$}

% Header on CHAPTER PAGE TODO
\fancypagestyle{chapter}{%
  \fancyhead[L]{\elenqtechsmall}
  \fancyhead[R]{\LARGE{$title$}}
}

% Footer on cover
\fancypagestyle{titlepage}{%
  \fancyhead{} % clear header
  \renewcommand{\headrulewidth}{0pt}
  \fancyfoot[R]{$author$}
  \fancyfoot[L]{$if(license)$\textcopyleft $license$ $endif$}
}

% Article titlepage, similar to book chapter
\fancypagestyle{articletitle}{%
  \fancyhead{} % clear header
  \fancyhead[L]{\elenqtechsmall}
  \fancyhead[R]{\LARGE{$title$}}
  \fancyfoot[R]{$author$}
  \fancyfoot[L]{$if(license)$\textcopyleft $license$ $endif$}
}

% Beautiful quotes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{framed}

% Use custom leftbar
\renewenvironment{leftbar}[1][\hsize]
  { \color{gray}
      \def\FrameCommand
      {{\color{lightgray}\vrule width 3pt}}
      \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
  }
  {\endMakeFramed}
% Put leftbar on quote
\let\oldquote=\quote
\let\oldendquote=\endquote
\renewenvironment{quote}
    {\vspace{10pt}\leftbar\vspace*{-6pt}\oldquote}
    {\oldendquote\endleftbar\vspace{10pt}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Metathings for title, author and date
$if(title)$
\title{$title$$if(thanks)$\thanks{$thanks$}$endif$}
$endif$
$if(subtitle)$
\providecommand{\subtitle}[1]{}
\subtitle{$subtitle$}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \and $endfor$}
$endif$
\date{$date$}

\begin{document}

% Define cover
% Make Title act similarly to book chapter
\thispagestyle{articletitle}
\begin{center}
\vspace*{0.3cm}
\begin{doublespace}
$if(title)$
{\Huge\bfseries $title$}\\
$endif$
$if(subtitle)$
{\LARGE $subtitle$}
$endif$
\end{doublespace}
\end{center}

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

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

$if(toc)$
{
$if(colorlinks)$
\hypersetup{linkcolor=$if(toccolor)$$toccolor$$else$black$endif$}
$endif$
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$

$if(lot)$
\listoftables
$endif$
$if(lof)$
\listoffigures
$endif$
$body$

%% Bibliography
$if(natbib)$
$if(bibliography)$
$if(biblio-title)$
  $if(has-chapters)$
  \renewcommand\bibname{$biblio-title$}
  $else$
  \renewcommand\refname{$biblio-title$}
  $endif$
  \bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
$endif$
$endif$
$endif$

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

$for(include-after)$
$include-after$
$endfor$

\end{document}
