%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Check out the accompanying book, Even Better Books with LaTeX the Agile Way in 2023, for a discussion of the template and step-by-step instructions. https://amzn.to/3HqwgXM https://leanpub.com/eBBwLtAW/
% The template was originally created by Clemens Lode, LODE Publishing (www.lode.de), on 1/1/2023. Feel free to use this template for your book project! 
% I would be happy if you included a short mention in your book in order to help others to create their own books, too ("Book template based on \textit{Even Better Books with LaTeX the Agile Way in 2023} by Clemens Lode").
% Contact me at mail@lode.de if you need help with the template or are interested in our editing and publishing services.
% And don't forget to follow us on Instagram! https://www.instagram.com/lodepublishing/ https://www.instagram.com/betterbookswithlatex/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To create an EPUB file, select pdfLaTeX in the Menu in the top-left and choose the conversion method in the /latexmkrc file.
% Select document class scrbook to be in the two-page mode and accommodate for the binding of a printed book.
% The bibliography receives an entry in the table of contents but no number.
\documentclass[pagesize=auto,bibliography=totocnumbered]{scrbook}
\input{setup.tex}
\title{\mytitle}
% Load additional LaTeX libraries.
\input{lib/packages}
% Set up externalization to save all tikz pictures also as JPG files (for the ebook/HTML output).
\ifxetex
\else   
    \usetikzlibrary{external}
    \tikzexternalize
    % Modify the behavior of tikzpicture: convert the generated image PDF to a jpg and insert that jpg (instead of the PDF) into the document.
    \tikzset{jpg export/.style={external/system call={pdflatex \tikzexternalcheckshellescape -interaction=batchmode -jobname "\image" "\texsource"; convert -gravity center -extent 1245 -strip -quality 100 -density 300 -transparent white "\image.pdf" "\image.jpg"},/pgf/images/external info,/pgf/images/include external/.code=\includegraphics{##1.jpg}}}
   
    % Activate "jpg export" configuration.
    \tikzset{jpg export}
    % Output the pdf to an existing directory.
    \tikzsetexternalprefix{tikz-cache/} 
    
\fi
%%%%%%%%%%%%%%%%%
% Preamble
%%%%%%%%%%%%%%%%%
% Redefining the \nameref command to use italics formatting needs to be done in the preamble:
\makeatletter
\AtBeginDocument{\@ifdefinable{\myorg@nameref}{\LetLtxMacro\myorg@nameref\nameref\DeclareRobustCommand*{\nameref}[1]{\textit{\myorg@nameref{#1}}}}}
\makeatother
% (Only) printed books have indexes.
% Allow three columns for the index to save space.
% Start tracking index commands, which have to be in the main file.
\ifxetex
    \usepackage{idxlayout}
    \makeindex[title=Index,columns=3]
\else
% This package is needed only when using tex4ebook and when a standalone EPUB file is the goal.
%    \usepackage{tex4ebook}
\fi
\begin{document}
\ifxetex
\else
%    \coverimage[scale=0.8]{OEBPS/cover.jpg}
\fi
\input{style-guide.tex}
\pagestyle{empty}
%%%%%%%%%%%%%%%%%
% Front matter
%%%%%%%%%%%%%%%%%
\frontmatter
% Front matter chapter entries use Roman page numbering (i, ii, iii, iv, ...).
\pagenumbering{roman}
% Remove this line for your actual book. It is only for advertisement.
\input{front/template-front}\blankpage
\input{front/half-title}\blankpage
% The additional title with the cover is not needed for e-books.
\ifxetex
    \input{front/title}\newpage
\fi
\input{front/publisher}\newpage
\input{front/dedication}\blankpage
\input{front/epigraph}\blankpage
\input{front/tableofcontents.tex}\blankpage
\input{front/foreword}\blankpage
\input{front/preface}\blankpage
\ifxetex
% Headers and Footers
    \pagestyle{fancy}
    \fancyhf{}
% Left-hand even page
    \fancyhead[LE]{\nouppercase{\rightmark\hfill\leftmark}}
% Right-hand odd page
    \fancyhead[RO]{\nouppercase{\leftmark\hfill\rightmark}}
% Left-hand even page / Right-hand odd page
    \fancyfoot[LE,RO]{\thepage}
% Re-define plain style
    \fancypagestyle{plain}{
        \fancyhf{}
        \fancyfoot[LE,RO]{\thepage}
        \renewcommand{\headrulewidth}{0pt} % optional: removes horizontal line in header
    }
\fi
%%%%%%%%%%%%%%%
% Main matter
%%%%%%%%%%%%%%%
\mainmatter
% Reset to normal page numbering (1, 2, 3, ...).
\renewcommand*{\chapterpagestyle}{plain}
\pagenumbering{arabic}
    
% Add additional chapters / remove chapters if necessary.
\input{chapters/0-latex.tex}\newpage
\input{chapters/01-advantages-latex.tex}\newpage
\input{chapters/02-generate-first-ebook.tex}\newpage
\input{chapters/03-filling-template.tex}\newpage
%%%%%%%%%%%%%%%%%
% Back matter
%%%%%%%%%%%%%%%%%
\backmatter
\input{chapters/04-tikz-examples.tex}\newpage
\input{back/advertisement}\blankpage
\input{back/author}\blankpage
\input{back/thebooksstory}\blankpage
\input{back/questions}\blankpage
\index{XeLaTeX@\textit{XeLaTeX}|textbf}
\input{back/ideas}\blankpage
\index{XeLaTeX@\textit{XeLaTeX}|textbf}
\input{back/glossary}\blankpage
% This adds a separate quotations page (sources in the e-book are already included in the text body).
\ifxetex
    \input{back/quotations}\blankpage
\fi
\input{back/bibliography}\blankpage
%%%%%%%%%%%%%%%%%
% Appendix
%%%%%%%%%%%%%%%%%
\appendix
% The index page exists only for printed PDFs.
\ifxetex
    % This is an optional command to display a prologue before the index.
    % \indexprologue{Replace index prologue with an own introduction (errata, formatting, abbreviations, etc.).}
    
    \printindex\blankpage
    \thispagestyle{empty}
\fi
\input{back/amazon}\blankpage
\input{back/last}
\end{document}