% cr-formatting-setup-local.tex  
% CircuitousRoot LaTeX Formatting, 2023 Reworking, 
% Locally modifiable "setup" macros.
% Revision 1, 2023-01-03
%
% Copyright 2023 by Dr. David M. MacMillan.
% License:  LaTeX Project Public License Version 1.3c.
%           This work has the LPPL maintenance status 'unmaintained'.
%           This work consists of the files:
%           "cr-formatting-preamble-local.texfrag",
%           "cr-formatting-preamble.texfrag",
%           "cr-formatting-setup.texfrag", and
%           "cr-formatting-setup.texfrag".
%           See:  http://www.latex-project.org/lppl/
%           Note:  This work is described in a document,
%                  "cr-formatting-guide.tex" (CircuitousRoot LaTeX Formatting),
%                  which is licensed under the 
%                  Creative Commons Attribution 4.0 International license.

% Encoding:  UTF-8, Unicode
% Requires:  XeLaTeX (xelatex)

% Usage:
% This is a fragment of a TeX/LaTeX file.  It is intended to provide
% the second portion of the setup for the CircuitousRoot LaTeX 
% formatting (2022 reworking).
% To use it, first cp it to a local file.
% Then \input it as the second thing after your document begins:
%
% \begin{document}
% \input{cr-formatting-setup.texfrag}
% \input{cr-formatting-setup-MYDOCUMENT.texfrag}
%
% Modify these macros to select specific aspects of the house style
% for your document (e.g., book, monograph, or article).
% You can also put any local (to your document) macros here.
%
% You should also have copied and modified 
% "cr-formatting-preamble-local.texfrag" and \input it and
% "cr-formatting-preamble.texfrag" as the
% first non-comment statements of your LaTeX document:
%
%    \input{cr-formatting-preamble-mylocaldocument.texfrag}
%    \input{cr-formatting-preamble.texfrag}
%
% For complete usage notes, see the document "CircuitousRoot LaTeX Formatting,"
% which should have accompanied these macros.
%------------------------------------------------------------------------------

% See:  https://tex.stackexchange.com/questions/66869/remove-part-page
\renewcommand{\part}[1]{\addcontentsline{toc}{part}{#1}}

% Headings, using the titlesec package.
%--------------------------------------

% titlesec calls \sectionbreak automatically.
% do NOT use it (leave it undefined) and break at sections manually (\newpage)
%\newcommand{\sectionbreak}{\clearpage}

% Space before and after headings.
% titlesec puts extra space before chapters; remove it 
% (-20pt is an empirical value which seems to work)

\titlespacing{\chapter}{0pt}%                     %   flush left
                     {-20pt plus 0pt minus 0pt}   %  = 0pt, really
                     {0pt plus 0pt minus 0pt}    %  0pt w/out glue below

\titlespacing{\section}{0pt}%                     %   flush left
                     {0pt plus 0pt minus 0pt}%   % 18pt w/out glue above
                     {6pt plus 0pt minus 0pt}     %  0pt w/out glue below
\titlespacing{\subsection}{0pt}%                  %      flush left
                     {0pt plus 0pt minus 0pt}%   % 14pt w/out glue above
                     {6pt plus 0pt minus 0pt}     %  0pt w/out glue below
\titlespacing{\subsubsection}{0pt}%               %         flush left
                     {0pt plus 0pt minus 0pt}%   % 14pt w/out glue above
                     {6pt plus 0pt minus 0pt}     %  0pt w/out glue below


% Heading formats.
\renewcommand{\thechapter}{\Roman{crPart}.\arabic{chapter}}
\titleformat{\chapter}
  {\normalfont\sffamily\fontsize{18pt}{24pt}\mdseries}
%  {\normalfont\sffamily\fontsize{18pt}{16pt}\bfseries}
% Note:  Previously I used U+25C6, which is a black diamond.
%        TeX Gyre Pagella supports only U+2666, which is a black diamond suit
%        Gillius ADF No. 2 does not support either
%  {\thechapter{\hskip1.5pt\raisebox{2pt}{\fontsize{8pt}{8pt}\selectfont\char"25C6}}}{6pt}{}      
%        So use U+25CA, an open lozenge, instead.  Make it bigger than U+25C6
  {\thechapter{\hskip1.5pt\raisebox{0pt}{\fontsize{18pt}{18pt}\selectfont\char"2022}}}{6pt}{}      

\renewcommand{\thesection}{\Roman{crPart}.\arabic{chapter}.\Alph{section}}
%\renewcommand{\thesection}{\arabic{chapter}.\Alph{section}}
\titleformat{\section}
  {\normalfont\sffamily\fontsize{16pt}{18pt}\mdseries}
%  {\normalfont\sffamily\fontsize{18pt}{18pt}\bfseries}
%  {\thesection{\hskip1.5pt\raisebox{2pt}{\fontsize{8pt}{8pt}\selectfont\char"25C6}}}{6pt}{}
  {\thesection{\hskip1.5pt\raisebox{0pt}{\fontsize{16pt}{16pt}\selectfont\char"2022}}}{6pt}{}

\renewcommand{\thesubsection}{\thesection.\roman{subsection}}
\titleformat{\subsection}
  {\normalfont\sffamily\fontsize{16pt}{16pt}\mdseries}
  {\thesubsection{\hskip1.5pt\raisebox{0pt}{\fontsize{16pt}{16pt}\selectfont\char"2022}}}{6pt}{}


% Note:  We create what are effectively "small cap" arabic numbers here.
%\renewcommand{\thesubsubsection}{\thesubsection.\fontsize{11bp}{11bp}\selectfont\arabic{subsubsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\normalsize\sffamily\arabic{subsubsection}}
\titleformat{\subsubsection}
  {\normalfont\sffamily\fontsize{14pt}{14pt}\mdseries}
  {\thesubsubsection{\hskip1.5pt\raisebox{0pt}{\fontsize{16pt}{16pt}\selectfont\char"2022}}}{6pt}{}



% Table of Contents modifications using titletoc
%
% Change the \part entry in the TOC to suit.
% NOTE:  Assume that the \part name has its roman number hardcoded into it.
%        Thus:  \part{IV:  Re-engineering}
%
\setcounter{tocdepth}{5}  % mas; this is standard LaTeX, not titletoc
%
\titlecontents{part}%                               % kind of section: part
    [0em]%                                          % distance from left margin
    {\vspace{2em}\large\bfseries\sffamily\relax}%   % above-code
    {\contentslabel[\relax]{0em}}%                  % numbered-entry-format
    {}%                                             % numberless-entry-format
    {}%                                             % filler-page-format
    []                                              % below-code
\dottedcontents{chapter}%                               % kind of section: part
    [0em]%                                          % distance from left margin
    {\vspace{0em}\large\mdseries\sffamily\relax}%   % above-code
    {2em}%                                             % label-width
    {1em}                                              % leader-width
\dottedcontents{section}%                               % kind of section: part
    [3em]%                                          % distance from left margin
    {\vspace{0em}\normalsize\mdseries\sffamily\relax}%   % above-code
    {3em}%                                             % label-width
    {1em}                                              % leader-width
\dottedcontents{subsection}%                           % kind of section: part
    [7em]%                                          % distance from left margin
    {\vspace{0em}\normalsize\mdseries\sffamily\relax}%   % above-code
    {4em}%                                             % label-width
    {1em}                                              % leader-width
\dottedcontents{subsubsection}%                        % kind of section: part
    [12em]%                                          % distance from left margin
    {\vspace{0em}\normalsize\mdseries\sffamily\relax}%   % above-code
    {5em}%                                             % label-width
    {1em}                                              % leader-width

% Table of Contents, command to adjust space after "Contents" title
% Do this immediately after \tableofcontents
\newcommand{\crContentsAdjust}{{\addtocontents{toc}{\vspace*{-2pc}}}}


% Debugging
%----------

% Uncomment to get a margin vertical rule flagging overfull hboxes.
% Caution:  If the box is so wide as to be off of the page, this rule
%           will be invisible.
\overfullrule=5pt

% Boilerplate
%\input{texts-mergenthaler-big-red-dwiggins.texfrag}


% Silencing overly verbose output
%--------------------------------
% TO DO:  Turn this into a more general command

% Helper command:  \crFirstWord
%From: 
%https://tex.stackexchange.com/questions/316730/extract-first-word-in-a-string
%Based on an example/query by dbmrq and a response by egreg, 
%both posted Jun 26, 2016 at 20:28
%
% This is the original example by dbmrq (which I can't get to work):
%\makeatletter
%\newcommand\FirstWord[1]{\@firstword#1 \@nil}%
%\newcommand\@firstword{}%
%\def\@firstword#1 #2\@nil{#1\unskip}%
%\makeatother
%
% Here is the solution to dbmrq's problem (his version failed to remove 
% a trailing comma) by egreg.  It works for me here, but is more than I need.
%\makeatletter
%\newcommand\crFirstWord[1]{\@firstword#1 \@nil}%
%\newcommand\@firstword{}%
%\newcommand\@removecomma{}%
%\def\@firstword#1 #2\@nil{\@removecomma#1,\@nil}%
%\def\@removecomma#1,#2\@nil{#1}
%\makeatother

\makeatletter
\newcommand\crFirstWord[1]{\@firstword#1 \@nil}%
\newcommand\@firstword{}%
\def\@firstword#1 #2\@nil{#1}%
\makeatother

% Modify \typeout to remove messages from package "contour".
% Remove all messages from package "contour" from output
\renewcommand{\typeout}[1]{%
   \ifthenelse{\equal{\crFirstWord{#1}}{contour:}}
   {}
   {\message{#1}}
}

% Remove "Missing character" messages...
% These come from xetex and cannot be removed in the LaTeX source.
% You can remove them by:
%   - turning off wordwrap
%   - then filtering the output through grep -v


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Local Below

% Local graphics path
\graphicspath{{./images-as-used/}}

% Footnotes: Markerless
%%%%%%%%%%%%%%%%%%%%%%%
%
% This will give a footnote in the ordinary sequence of footnotes
% but without a number of its own.  The next regular footnote beyone
% this one resumes regular footnote numbering +1 from the regular
% footnote which appeared before it.
% 
% To make sure that this blfootnote doesn't appear to be a part of the
% one before it, it is best to use blfootnote alone
% (e.g., esp. on 1st page to give contact and licensing info).
%
\newcommand\blfootnote[1]{% 
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}%
%  \renewcommand\thefootnote{}\footnote{\hskip-\parindent #1}%
  \addtocounter{footnote}{-1}%
  \endgroup
}
% REF:  https://tex.stackexchange.com/questions/30720/footnote-without-a-marker


% For fancyhdr
\newcounter{crPart}
\setcounter{crPart}{-1}
\newcounter{crChapter}
\setcounter{crChapter}{-1}
\newcounter{crSection}
\setcounter{crSection}{-1}
\setlength{\headheight}{15.2pt}
\fancyhead[L,C,R]{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[L,R]{}

% e.g. IV - 1
\newcommand{\crFancyFootPart}{%
   \fancyfoot[C]{\textsf{\Roman{crPart}\enskip - \thepage}}}
% e.g. IV.2 - 1
\newcommand{\crFancyFootPartChapter}{%
   \fancyfoot[C]{\textsf{\Roman{crPart}.\thecrChapter\enskip - \thepage}}}
% e.g. IV.2.B - 1
\newcommand{\crFancyFootPartChapterSection}{%
   \fancyfoot[C]{\textsf{\Roman{crPart}.\thecrChapter.\Alph{crSection}\enskip - \thepage}}}

\newcommand{\crUnnumberedSection}[1]{\medskip\noindent%
{\fontsize{12.5bp}{13bp}\selectfont%
\textsf{\textsc{#1}}}}

\newcommand{\crTableTitle}[1]{\medskip\noindent%
{\fontsize{12.5bp}{13bp}\selectfont%
\textsf{#1}}}

% Part Entry
%-----------
%
% For the solution to conditional arguments, see:  
%    https://tex.stackexchange.com/questions/537219/
%                         conditionals-inside-newcommand-with-empty-argument
% BUG:  not sure why the fontsize doesn't stick throughout
\newcommand{\crPartEntry}[9]{{\fontsize{10bp}{10bp}\selectfont%
   \medskip%
   \underline{\textsf{#1.#2}}}%  Group.PartNumber
   % #3 optional number-in-series:  .INT 
   \expandafter\ifx\expandafter\relax\detokenize{#3}\relax% if, do nothing
       \else\underline{\fontsize{10bp}{10bp}\selectfont\textsf{.#3}}\fi                                        % else, do #4
   % #4 optional note:  nINT 
   \expandafter\ifx\expandafter\relax\detokenize{#4}\relax% if, do nothing
       \else{\fontsize{10bp}{10bp}\selectfont\textsf{.n#4}}\fi                             % else, do #5
   % #5 optional variation:  vINT 
   \expandafter\ifx\expandafter\relax\detokenize{#5}\relax% if, do nothing
       \else{\fontsize{10bp}{10bp}\selectfont\textsf{.v#5}}\fi                             % else, do #6
   % #6 optional revision:  rINT 
   \expandafter\ifx\expandafter\relax\detokenize{#6}\relax% if, do nothing
       \else{\fontsize{10bp}{10bp}\selectfont\textsf{.r#6}}\fi                             % else, do #7
   % : Part Name
   : \crPartName{\fontsize{10bp}{10bp}\selectfont\textsf{#7}}%
   % #8 optional classification number: c#INT
   \expandafter\ifx\expandafter\relax\detokenize{#8}\relax% if, do nothing
       \else{\fontsize{10bp}{10bp}\selectfont\quad\textbf{c\##8}}\fi%                            % else, do #9
   % #9 optional quantity: (INT)
   \expandafter\ifx\expandafter\relax\detokenize{#9}\relax% if, do nothing
       \else{\fontsize{10bp}{10bp}\selectfont\quad(qty. #9)}\fi%                            % else, but no #10
   } % end of code

% For the part descriptions, use a minipage so that we can do 
% wrapfigure for illustrations in the description.
% (wrapfigure does not work in lists, and the displayuote environment
% is implemented in LaTeX with a list.
\newcommand{\crPartDescriptionParagraph}[1]{{%
\par%       % so this can follow a crPartEntry without a blank line
\medskip%   % visual space before this crPartDescriptionParagraph
\noindent\hspace{2pc}\begin{minipage}[b]{\dimexpr\textwidth-2pc}%
#1%
\end{minipage}}}

\newcommand{\crPartDescriptionParagraphHeading}[1]{\textbf{#1}}

% Part Name (used above in crPartEntry
% Note:  may want to use package mfirstuc also
\newcommand{\crPartName}[1]{{\fontsize{10bp}{10bp}\selectfont{\textsf{#1}}}}

% Part Callout: {symbol}{name}
%\newcommand{\crPartCallout}[2]{{\sffamily {#1} \slshape{#2}}}
\newcommand{\crPartCallout}[2]{\textsf{#1} \textsf{#2}}



% Cut a BiblioEntry line.
% In bibliographic entries you cannot rephrase the entry to eliminate
% overfull hboxes.  The only option is manually to cut the line.
% but a simple \\ will cause the line to go all the way to the left margin
% when it should be hanging.  This manually corrects this problem.
\newcommand{\crCutBiblioLine}{\\ \hskip\crBiblioIndent}

