% ---------------------------------------------------------------------------
% md2latex header for books (docclass: book). Comes after header.tex.
% The Lua filter (rexxpub.lua) writes the macros defined here: \rxtitlepage,
% \rxlistingcaption, \rxanchor, \rxdir, \rxsecnum...
% ---------------------------------------------------------------------------

\usepackage{graphicx}
\usepackage[export]{adjustbox}  % max width/height for pictures
\usepackage{float}              % [H]: output figures stay where the text says
\usepackage[section]{placeins}  % floats do not leave their section
\usepackage{enumitem}
\usepackage{titlesec}
\usepackage{fancyhdr}

% --- Numbering ---------------------------------------------------------------
% Figures and tables are numbered through the book (1, 2, 3...), as listings
% are, unless the YAML says listings: numbering: section ("1.1-1").
\counterwithout{figure}{chapter}
\counterwithout{table}{chapter}

% The deeper levels are headings of their own, not run-in paragraphs
\titleformat{\paragraph}{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{1ex}
\titleformat{\subparagraph}{\normalfont\normalsize\bfseries\itshape}{\thesubparagraph}{1em}{}
\titlespacing*{\subparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1ex}

% --- Running heads -------------------------------------------------------------
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0.4pt}
\fancyhead[LE]{\small\nouppercase{\leftmark}}
\fancyhead[RO]{\small\nouppercase{\rightmark}}
\fancyfoot[LE,RO]{\thepage}
\fancypagestyle{plain}{\fancyhf{}\renewcommand{\headrulewidth}{0pt}\fancyfoot[LE,RO]{\thepage}}

% --- Directions and section numbers of anchors ---------------------------------
% Every target gets a zref label with its place in the document (rxseq) and
% the number of the section it is in (rxsec). "above"/"below" compares the
% places; a reference to an anchor in running text gives its section number.
\usepackage{zref-user}
\makeatletter
\newcounter{rxseq}
\def\rxcursec{}
\zref@newprop{rxseq}[0]{\the\value{rxseq}}
\zref@newprop{rxsec}[??]{\rxcursec}
\zref@addprops{main}{rxseq,rxsec}
\newcommand{\rxsetsec}[1]{\protected@edef\rxcursec{#1}}
\newcommand{\rxanchor}[1]{\stepcounter{rxseq}\zlabel{#1}}
\providecommand{\rxabove}{above}
\providecommand{\rxbelow}{below}
\newcommand{\rxdir}[1]{\stepcounter{rxseq}\zref@refused{#1}%
  \ifnum\zref@extractdefault{#1}{rxseq}{0}<\value{rxseq}\relax\rxabove\else\rxbelow\fi}
\newcommand{\rxsecnum}[1]{\zref@refused{#1}\zref@extractdefault{#1}{rxsec}{??}}
\makeatother

% --- Headings out of the contents (.toc-exclude) ------------------------------
% Written into the .toc around the heading's line (20261102)
\newcounter{rxtocdepth}
\newcommand{\rxtocoff}{\setcounter{rxtocdepth}{\value{tocdepth}}\setcounter{tocdepth}{-10}}
\newcommand{\rxtocon}{\setcounter{tocdepth}{\value{rxtocdepth}}}

% --- Listings ----------------------------------------------------------------
% A numbered listing: the code, then its caption, kept together.
\makeatletter
\providecommand{\rxlistingname}{Listing}
\providecommand{\rxlolname}{List of Listings}
\newcommand{\rxlistingnumbering}[1]{%
  \ifx\relax#1\relax\else
    \counterwithin*{rxlisting}{#1}%
    \expandafter\def\expandafter\therxlisting\expandafter{%
      \csname the#1\endcsname-\arabic{rxlisting}}%
  \fi}
\newcommand{\rxlistingcaption}[2]{%
  \par\nobreak\vspace{-0.4em}\refstepcounter{rxlisting}\label{#1}\rxanchor{#1}%
  \addcontentsline{lol}{rxlisting}{\protect\rxlolentry{\therxlisting}{#2}}%
  {\centering\small\textbf{\rxlistingname~\therxlisting:} #2\par}\vspace{0.6em}}
\newcommand{\rxlolentry}[2]{\rxlistingname~#1: #2}
\newcommand*{\l@rxlisting}{\@dottedtocline{1}{0em}{0em}}
\newcommand{\listoflistings}{\chapter*{\rxlolname}\@mkboth{\rxlolname}{\rxlolname}\@starttoc{lol}}
\makeatother

% Code that is not Rexx (style 1's colours) and program output
\providecommand{\rxoutfont}{\itshape}
\newenvironment{rxoutput}[1][0]
  {\begin{rxblock}[#1]{1}\rxoutfont}{\end{rxblock}}
\providecommand{\rxout}[1]{\texttt{\textit{#1}}}

% --- The title page --------------------------------------------------------------
\newcommand{\rxtitlepage}[5]{% title, subtitle, author, lines, verso
  \begin{titlepage}
    \centering
    \vspace*{0.18\textheight}
    {\sffamily\bfseries\Huge #1\par}
    \vspace{1.2em}
    {\sffamily\Large #2\par}
    \vspace{3em}
    {\Large #3\par}
    \vspace{2em}
    {\large #4\par}
    \vfill
  \end{titlepage}
  \thispagestyle{empty}
  \null\vfill
  {\small\noindent #5\par}
  \cleardoublepage}
