% 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 book template
% -----------------------------------------------------------------------------
%
% Expects some metadata to be set:
%
%   title: The title of the document
%   subtitle: The subtitle of the book
%   author: list of author's name
%   license: License of the document
%   lang: es-ES
%   polyglossia:
%     name: spanish
%
% It also accepts some optional metadata:
%
%   toc: true | false  # Activate table of contents (defaults to true)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

\usepackage{tikz}
\usepackage{epstopdf}

% Language control from Metadata
$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=1.5cm,
            bottom=1.5cm,
            left=1.75cm,
            right=2.1cm,
            footskip=1.3cm,
            includeheadfoot=true]{geometry}


% Spacing
\usepackage{setspace}

% Font
\usepackage{moresize}
\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}
\newcommand{\euro}{€}

% Font footnote size
\makeatletter
\renewcommand\footnotesize{%
   \@setfontsize\footnotesize\@ixpt{10}%
   \abovedisplayskip 6\p@ \@plus2\p@ \@minus4\p@
   \abovedisplayshortskip \z@ \@plus\p@
   \belowdisplayshortskip 4\p@ \@plus2\p@ \@minus2\p@
   \def\@listi{\leftmargin\leftmargini
               \topsep 4\p@ \@plus2\p@ \@minus2\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep}%
   \belowdisplayskip \abovedisplayskip
}
\makeatother



% 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(lang)$
            pdflang={$lang$},
          $endif$
          $if(author-meta)$
            pdfauthor={$author-meta$},
          $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{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
  commandchars=\\\{\},
  fontsize=\footnotesize,
  xleftmargin=6mm,
  numbersep=2mm
}
\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}
$endif$

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

% Definitions related to chapters only if book is used
% REMAKE CHAPTER FOR FANCYHEADER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
    \let\stdchapter\chapter
    \renewcommand*\chapter{%
    \@ifstar{\starchapter}{\@dblarg\nostarchapter}}
    \newcommand*\starchapter[1]{
        \stdchapter*{#1}
        \thispagestyle{chapter} % This is the point
        \markboth{\MakeUppercase{#1}}{}
    }
    \def\nostarchapter[#1]#2{
        \stdchapter[{#1}]{#2}
        \thispagestyle{chapter}
    }
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Chapter formating
% https://www.sharelatex.com/learn/Sections_and_chapters#/Customize_chapters_and_sections
\usepackage{titlesec}
\newcommand{\hsp}{\hspace{20pt}}
\titlespacing*{\chapter}{0pt}{0pt}{40pt}

% Title font
\usepackage{titling}
\usepackage{fontspec}
% Specify different font for section headings
\newfontfamily\headingfont[]{Pathway Gothic One}
\titleformat{\chapter}
            [block]
            {\HUGE\headingfont}
            {}
            {0pt}
            {\MakeUppercase}
%\titleformat*{\chapter}{\Huge\headingfont}
\titleformat*{\section}{\Huge\headingfont}
\titleformat*{\subsection}{\LARGE\headingfont}
\titleformat*{\subsubsection}{\Large\headingfont}
\renewcommand{\maketitlehooka}{\headingfont}

% 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
%% ADDED LATER
\renewcommand{\headrulewidth}{0pt}
\fancyhead{} % clear header

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

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

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

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

% Use custom leftbar
\renewenvironment{leftbar}[1][\hsize]
  {\footnotesize\color{darkgray}
      \def\FrameCommand{{\color{lightgray}\vrule width 2pt}}
      \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
  }
  {\endMakeFramed}
% Put leftbar on quote
\let\oldquote=\quote
\let\oldendquote=\endquote
\renewenvironment{quote}
    {\vspace{10pt}\leftbar\vspace*{-10pt}\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
$if(title)$
\begin{titlepage}
\thispagestyle{titlepage}
\vspace*{4cm}
  {\fontsize{35}{45}\headingfont \MakeUppercase{$title$}}
\hfill
% TODO: Logo removed from here, maybe add it down in the middle?
%\logo{1}
%\textcolor[RGB]{220,220,220}{\rule{\textwidth}{0.4pt}}


$if(subtitle)$
\begin{doublespace}
{\LARGE \headingfont $subtitle$}
\end{doublespace}
$endif$
\end{titlepage}
$endif$

% Changes registry
% TODO

$if(acknowledgement)$
\cleardoublepage
\vspace*{4cm}
$acknowledgement$
\vspace*{\fill}
$endif$

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


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

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

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

% Define back cover
\newcommand*\cleartoleftpage{
  \clearpage
  \ifodd\value{page}\hbox{}\newpage\fi
}
% Add backcover
\cleartoleftpage
\thispagestyle{empty}
\null\vfill
%\logo{0.8}
\includegraphics[]{templates/img/publishing_logo.eps}
\vspace{0cm}

\end{document}
