\usepackage[paper=a4paper,vmargin=1.5cm,left=4.5cm,right=3.5cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[final,protrusion=true,expansion=true]{microtype}
\usepackage{xcolor}
\PassOptionsToPackage{final,colorlinks,linkcolor=red!60!orange!85!black}{hyperref}
\usepackage[numbered]{hypdoc}
\usepackage{fancyvrb}
\usepackage[final]{listings}
\usepackage{enumitem}
\usepackage{bookmark}
\usepackage{siunitx}
\usepackage{footnote}
\usepackage{etoc}
\usepackage{accsupp}
\usepackage{tikz}
\usetikzlibrary{positioning,patterns.meta,fit}
\usepackage[british]{babel}
\usepackage{varioref}
\usepackage{embedfile}
\usepackage{dtx-attach}
\embedfile[mimetype=text/plain]{sankey.ins}
\usepackage{sankey}

\colorlet{bgcode}{yellow!50!gray!5}
\colorlet{keyword}{blue!50!cyan!50!black}
\colorlet{comment}{red!75!black}

\newcommand\emptyaccsupp[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\lstset{
  literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\texteuro}}1 {£}{{\pounds}}1 {°}{{\degres}}1
  {’}{{\textquoteright}}1 {‘}{{\textquoteleft}}1
  {«}{{<<}}1 {»}{{>>}}1
  {—}{{---}}1 {−}{{$-$}}1
  {\^\^A}{{}}0                % verbatim in .dtx file
}

\lstset{
  fancyvrb=true,
  escapechar=`,
  extendedchars=true,
  fontadjust=true,
  columns=fullflexible,
  flexiblecolumns=true,
  inputencoding=utf8,
  keepspaces=true,
  backgroundcolor=\color{bgcode},
  basicstyle=\mdseries\ttfamily,
  keywordstyle=\color{keyword},
  stringstyle=\ttfamily\color{green!50!black},
  commentstyle=\color{comment}\itshape,
  emphstyle=\bfseries\color{red},
  numbersep=5pt,
  %numbers=left,
  numberstyle=\tiny\emptyaccsupp,
  showstringspaces=false,
  upquote=true,
  aboveskip=.5\parskip,
  belowskip=.5\parskip,
  framexleftmargin=1pt,
  framexrightmargin=1pt,
  gobble=2,              % \lstlisting code in .dtx
}

\lstdefinestyle{textsmall}{basicstyle=\color{black}\small\mdseries\ttfamily}
\lstdefinestyle{textfootnotesize}{basicstyle=\color{black}\footnotesize\mdseries\ttfamily}
\lstdefinestyle{textscriptsize}{basicstyle=\color{black}\scriptsize\mdseries\ttfamily}
\lstdefinestyle{texttiny}{basicstyle=\color{black}\tiny\mdseries\ttfamily}

\lstdefinestyle{LaTeX}{
  language=[LaTeX]TeX,
  moretexcs={
    Lab,
    LabSet,
    Qty,
    QtySet,
    colorlet,
    coordinate,
    countries,
    country,
    countryname,
    definecolor,
    draw,
    endcountry,
    fill,
    foreach,
    hashband,
    hdist,
    hwidth,
    node,
    nodename,
    path,
    sankeyadvance,
    sankeydubins,
    sankeyend,
    sankeyend,
    sankeyfork,
    sankeynode,
    sankeynodealias,
    sankeynodeend,
    sankeynodestart,
    sankeyoutin,
    sankeyset,
    sankeystart,
    sankeyturn,
    sankeyturnleft,
    sankeyturnleftbackward,
    sankeyturnright,
    sankeyturnrightbackward,
    sisetup,
    startcountry,
    tikzset,
    turnandstop,
    usepackage,
    vdist,
  }
}
\lstdefinestyle{LaTeXsmall}{style=LaTeX,style=textsmall}
\lstdefinestyle{LaTeXfootnotesize}{style=LaTeX,style=textfootnotesize}
\lstdefinestyle{LaTeXscriptsize}{style=LaTeX,style=textscriptsize}
\lstdefinestyle{LaTeXtiny}{style=LaTeX,style=texttiny}

\def\code{\lstinline[basicstyle=\mdseries\ttfamily\color{red!50!black}]}

\edef\samplecodename{\jobname-code.vrb}

\newcommand\constant[1]{\textcolor{violet}{\texttt{#1}}}
\newcommand\ARG[1]{\texttt{\{#1\}}}
\newcommand\OPTARG[1]{\textcolor{green!50!black}{\texttt{[#1]}}}
\newcommand\VAR[1]{\textit{\texttt{\ensuremath{\langle}#1\ensuremath{\rangle}}}}
\newcommand\NOTE[1]{\leavevmode\marginpar{#1}}

\def\mynobreakpar{\par\nobreak\@afterheading}
\def\docprefix#1{\texttt{\textcolor{gray}{#1}}}
\def\sankeykeysprefix{/sankey}

\newenvironment{sankeyoption}[5][]{% [prefix] key, val, default, initially
  \begingroup
  %
  \def\keypath{#1}%
  \def\key{#2}%
  \def\val{#3}%
  \def\default{#4}%
  \def\initially{#5}%
  %\setlength\parindent{\dimexpr.5\parindent\relax}%
  % \vspace{.25\baselineskip plus .25\baselineskip minus 0mm}
  \parfillskip 0pt plus 1fil%
  \leavevmode%
  \ttfamily%
  \hspace*{-1cm}%
  \textcolor{red!75!black}{\texttt{\docprefix{\ifdefempty{\keypath}{\sankeykeysprefix/}{\keypath/}}\key}}%
  \ifdefempty{\val}{}{=\val}%
  \hfill%
  \ifdefempty{\default}{\null}{(default:\,\constant{\default})}%
  \ifdefempty{\initially}{\null}{(initially:\,\constant{\initially})}%
  \mynobreakpar%
  \endgroup%
  \parskip=.5\baselineskip plus .25\baselineskip minus .25\baselineskip
  \parfillskip=30pt plus 1fil
  \itemize[topsep=0pt,partopsep=0pt,itemsep=0pt]\item[]
}{%
  \enditemize%
}

\newcommand\MACRO{\hspace*{-1cm}}

\newlist{sankeyconstantsdesc}{description}{1}
\setlist[sankeyconstantsdesc]{align=right,labelindent=1.5em,labelsep=.5em,leftmargin=!,font=\normalfont}

\newlength{\myparskip}
\setlength{\myparskip}{.75\baselineskip plus 8\baselineskip minus .25\baselineskip}
\usepackage[skip=\myparskip]{parskip}
\newenvironment{miniblock}{%
  \vspace{.5\parskip}%
}{%
}

\tikzset{
  every picture/.style={
    execute at end picture={
      \begin{pgfonlayer}{background}
        \node[fit=(current bounding box),inner sep=0](bb){};
        \def\rs{5mm}
        \draw[gray!25] let
        \p{sw}=(bb.south west), \p{ne}=(bb.north east),
        \n{maxx}={ceil(\x{ne}/\rs)*\rs}, \n{minx}={floor(\x{sw}/\rs)*\rs},
        \n{maxy}={ceil(\y{ne}/\rs)*\rs}, \n{miny}={floor(\y{sw}/\rs)*\rs}
        in (\n{minx},\n{miny}) grid (\n{maxx},\n{maxy});
        \begin{scope}[overlay]
          \draw[gray!50,-latex] let
          \p{sw}=(bb.south west), \p{ne}=(bb.north east),
          \n{maxx}={ceil(\x{ne}/\rs)*\rs}, \n{minx}={floor(\x{sw}/\rs)*\rs},
          \n{maxy}={ceil(\y{ne}/\rs)*\rs}, \n{miny}={floor(\y{sw}/\rs)*\rs}
          in (\n{minx},0) -- (\n{maxx}+1mm,0);
          \draw[gray!50,-latex] let
          \p{sw}=(bb.south west), \p{ne}=(bb.north east),
          \n{maxx}={ceil(\x{ne}/\rs)*\rs}, \n{minx}={floor(\x{sw}/\rs)*\rs},
          \n{maxy}={ceil(\y{ne}/\rs)*\rs}, \n{miny}={floor(\y{sw}/\rs)*\rs}
          in (0,\n{miny}) -- (0,\n{maxy}+1mm);
        \end{scope}
      \end{pgfonlayer}
    }
  }
}

\def\MacroFont{
  \fontencoding\encodingdefault
  \fontfamily\ttdefault
  \fontseries\mddefault
  \fontshape\shapedefault
  \footnotesize%
}
