\documentclass[\OPTmodes]{book}

\usepackage[papersize={\OPTpaperesize},
            top=\OPTtopmargin,
            bottom=\OPTbottommargin,
            inner=\OPTinnermargin,
            outer=\OPToutermargin,
            bindingoffset=\OPTbindingoffset,
            centering,
            % showframe,
            ]{geometry}

% New line height: 1.05 * 1.2 = 1.26
\renewcommand{\baselinestretch}{1.05}

% Uncomment for diagnostics!
% \usepackage[cam,width=6in,height=9in]{crop}

% Font settings
%--------------------------------------------------------------%  Note:
\usepackage[no-math]{fontspec}                                 %  This exact order
\usepackage{libertine}                                         %  of declarations
\defaultfontfeatures{%                                         %  is important for
  Scale=MatchLowercase, % needed here ...                      %  single quotes not
}                                                              %  to turn out curly
\setmonofont[BoldFont=Inconsolata-LGC-Bold.ttf]{Inconsolata-LGC-Bold.ttf} %  ("typographic")
                                                               %  in verbatim blocks
\defaultfontfeatures{%                                         %  of Haskell code.
  Scale=MatchLowercase, % ... and here again                   %  Now the quote is
  Mapping=tex-text,                                            %  upright and safely
  SmallCapsFeatures={LetterSpace=2.5,WordSpace=1.05},          %  copy-pasteable to
}                                                              %  the REPL without
%--------------------------------------------------------------%  giving errors.

% To use Libertine letters and numbers,
% but tx-style operators in math environment:
\usepackage[libertine]{newtxmath}
\usepackage{amsmath}
% Needed to display additional math unicode symbols (like double-colon)

% https://github.com/wspr/unicode-math/issues/469
\expandafter\let\csname not=\endcsname\relax
\expandafter\let\csname not<\endcsname\relax
\expandafter\let\csname not>\endcsname\relax
\usepackage{unicode-math}
\setmathfont{LibertinusMath-Regular.otf}

\usepackage[all]{nowidow}
\usepackage{emptypage}
\usepackage{noindentafter}

\usepackage{graphicx}
\usepackage{caption, wrapfig, framed, subfigure}
\usepackage[export]{adjustbox}
\captionsetup{labelformat=empty,font=scriptsize}
\usepackage{titlecaps}

\usepackage[usenames,dvipsnames,x11names]{xcolor}
\usepackage{longtable}
\usepackage{booktabs}
% Workaround to fix mismatched left and right math delimiters. Taken from:
% http://tex.stackexchange.com/questions/63410/parentheses-differ-xelatex-fontspec-newtxmath-libertine
\DeclareSymbolFont{parenthesis}{T1}{fxl}{m}{n}
\DeclareMathDelimiter{(}{\mathopen}{parenthesis}{"28}{largesymbols}{"00}
\DeclareMathDelimiter{)}{\mathclose}{parenthesis}{"29}{largesymbols}{"01}
\DeclareMathDelimiter{[}{\mathopen}{parenthesis}{"5B}{largesymbols}{"02}
\DeclareMathDelimiter{]}{\mathclose}{parenthesis}{"5D}{largesymbols}{"03}
\DeclareMathDelimiter{\lbrace}{\mathopen}{parenthesis}{"7B}{largesymbols}{"08}
\DeclareMathDelimiter{\rbrace}{\mathclose}{parenthesis}{"7D}{largesymbols}{"09}

% Use tikz-cd for commutative diagrams
\usepackage{tikz-cd}
\usepackage[framemethod=TikZ]{mdframed}

\usepackage{ifthen}
\usepackage{fancyvrb}
\fvset{fontsize=\small}

\usepackage[cachedir=_minted-ctfp]{minted}
\setminted[text]{fontsize=\small,breaklines}
\usemintedstyle{github}
\makeatletter
% prevents cleaning up the cache at the end of the run (needed to keep the "unused" caches, generated by other editions)
\renewcommand*{\minted@cleancache}{}
\makeatother

\usepackage{imakeidx}
\usepackage[totoc,font=footnotesize]{idxlayout}
\usepackage{fancyhdr}
\pagestyle{plain}
\usepackage[final]{pdfpages} % inserts pages from a pdf file

\usepackage{titlesec}  % to change the appearance of section titles
\usepackage{listings}  % for syntax highlighted code listings
\usepackage{enumerate} % allows customized labels in enumerations
\usepackage{enumitem}  % allows nested enumeration with numbers
\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}  % makes cross references and URLs clickable
\definecolor{LinkRed}{HTML}{80171F}
\hypersetup{
  pdfauthor={Bartosz Milewski},
  pdftitle={Category Theory for Programmers},
  pdfsubject={category theory, computer science, programming, abstraction, composition},
  colorlinks=true,
  linkcolor=LinkRed,
  urlcolor=LinkRed,
}
\usepackage{afterpage}
\newcommand\blankpage{%
    \null
    \thispagestyle{empty}%
    \addtocounter{page}{-1}%
    \newpage}

\usepackage{subfiles}
\makeatletter
\let\org@subfile\subfile
\renewcommand*{\subfile}[1]{%
  \filename@parse{#1}
  \expandafter
  \graphicspath\expandafter{\expandafter{\filename@area}}
  \org@subfile{#1}%
}
\makeatother

% Document colors
\definecolor{Dark}          {HTML} {262626}
\definecolor{DropCapGray}   {HTML} {B8B8B8}
\definecolor{ChapterGray}   {HTML} {C8C8C8}
\definecolor{ChapterViolet} {HTML} {AEAECE}
\definecolor{DropCapViolet} {HTML} {9090C0}

\usepackage{lettrine}  % adds commands that make drop capitals
\renewcommand{\LettrineFontHook}{\rmfamily\mdseries\color{DropCapViolet}}
\renewcommand{\DefaultLraise}{0.00}
\renewcommand{\DefaultLoversize}{0.02}
\renewcommand{\DefaultLhang}{0.12}
\setlength{\DefaultFindent}{1pt}
\setlength{\DefaultNindent}{0em}

% removes the "Part X" text from \part
\renewcommand{\partname}{}
\renewcommand{\thepart}{}

\usepackage{xparse}
\usepackage[abspath]{currfile}

\newenvironment{snip}[2][]
  {\VerbatimEnvironment
  \mdfsetup{
    linecolor=gray!20,
    linewidth=2pt,
    topline=false,
    bottomline=false,
    rightline=false
  }
  \begin{mdframed}\begin{minted}[#1]{#2}}
 {\end{minted}\end{mdframed}
 \vspace{-1ex}}

 \newenvironment{snipv}
 {\VerbatimEnvironment
 \mdfsetup{
   linecolor=gray!20,
   linewidth=2pt,
   topline=false,
   bottomline=false,
   rightline=false
 }
 \begin{mdframed}\begin{Verbatim}[commandchars=\\\{\}]}
{\end{Verbatim}\end{mdframed}
\vspace{-1ex}}

% See the context about this workaround at
% https://github.com/gpoore/minted/issues/354
\makeatletter
\ifwindows
  \def\minted@opt@quote#1{\detokenize\expandafter{\expandafter"\expanded{#1}"}}
\else
  \def\minted@opt@quote#1{\detokenize\expandafter{\expandafter'\expanded{#1}'}}
\fi
\makeatother

\NewDocumentCommand\src{mO{}}{
  \srcsnippet{\currfileabsdir/code/haskell/#1.hs}{blue}{haskell}{#2}
  \ifdefined\OPTCustomLanguage{%
    \unskip
    \srcsnippet{\currfileabsdir/code/\OPTCustomLanguage/#1.\OPTCustomLanguageExt}{\OPTCustomLanguageColor}{\OPTCustomLanguage}{#2}
  }
  \fi
  \NoIndentAfterThis
}
\NewDocumentCommand\srcsnippet{mmmm}{
  \newif\ifbreak
  \def\snippet{#1}
  \mdfsetup{%
    linecolor=#2!20,%
    linewidth=2pt,
    topline=false,
    bottomline=false,
    rightline=false,
  }
  \def\x{#4}
  \ifthenelse{\equal{\x}{b}}{\breaktrue}{\breakfalse}
  \begin{mdframed}
    \ifbreak \inputminted[autogobble,breaklines]{#3}{\snippet}
    \else \inputminted[autogobble]{#3}{\snippet}
    \fi
  \end{mdframed}
}

\newcommand{\acronym}[1]{\textsc{\MakeLowercase{#1}}}
\newcommand{\newterm}[1]{\index{#1}\emph{#1}}
\newcommand{\code}[1]{\Verb+{#1}+}
\newcommand{\heading}[1]{{\sffamily\bfseries #1}}
\newcommand{\mono}[1]{\hbox{\ttfamily\scriptsize #1}}
\newcommand{\monoit}[1]{\hbox{\ttfamily\itshape\scriptsize #1}}
\newcommand{\mathtext}[1]{\ensuremath{\pmb{#1}}}
\newcommand{\urlref}[2]{\href{#1}{#2}\urlfootnote{#1}}

% https://tex.stackexchange.com/questions/219445/line-break-in-texttt/219497
\renewcommand{\code}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`[\lowercase{\endgroup\def~}{[\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \catcode`/=\active\catcode`[=\active\catcode`.=\active
  \scantokens{#1\noexpand}%
  \endgroup
}

\makeatletter
\newcommand\urlfootnote@[1]{\footnote{\url@{#1}}}
\DeclareRobustCommand{\urlfootnote}{\hyper@normalise\urlfootnote@}
\makeatother

\titleformat{\chapter}[display]
  {\color{Dark}\normalfont\sffamily\bfseries\LARGE}
  {\filright \color{ChapterViolet}\sffamily\mdseries
    \fontsize{6em}{0em}\selectfont
    {\addfontfeature{Numbers=OldStyle}\thechapter}}
  {1em}
  {\filright}

\titleformat{\section}
{\color{Dark}\normalfont\Large\sffamily\bfseries}
{\color{DropCapViolet}\thesection}{0.8em}{}

\titleformat{\subsection}
{\color{Dark}\normalfont\large\sffamily\bfseries}
{\color{DropCapViolet}\thesubsection}{0.8em}{}

\titleformat{\subsubsection}
{\color{black}\normalfont\normalsize\sffamily\bfseries}
{\color{DropCapViolet}\thesubsubsection}{0.8em}{}

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

\sloppy
\frenchspacing
\makeindex

% reduce space between items in a list
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

%====================%
%  End of preamble.  %
%====================%

\input{category}
\begin{document}
