% cr-formatting-preamble.tex  
% CircuitousRoot LaTeX Formatting, 2023 Reworking: 
% House Style LaTeX preamble 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-local.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 preamble for the CircuitousRoot LaTeX formatting (2023 reworking).
% To use it, \input it as the second non-comment statement of your
% LaTeX document:
%
%    \input{cr-formatting-preamble-mylocaldocument.texfrag}
%    \input{cr-formatting-preamble.texfrag}
%
% You must provide any additional preamble material and the \begin{document}
% statement yourself in your own document.
%
% You should also probably input the "setup" fragments as the first things
% after your document begins:
%
% \begin{document}
% \input{cr-formatting-setup.texfrag}
% \input{cr-formatting-setup-mylocaldocument.texfrag}
%
% For complete usage notes, see the document "CircuitousRoot LaTeX Formatting,"
% which should have accompanied these macros.
%------------------------------------------------------------------------------


% Document Class
%---------------

% All CR Formatting documents are LaTeX "book" documents.
% They may have title pages, but we will hand-write these and so
% suppress the LaTeX defaults.
% LaTeX "book" documents are two-sided by default.  So in the code
% below the "twoside" option is redundant but the "oneside" option
% is required.

% work through "backwards," so Single Column Single Sided is the default
\ifdefined\crDoubleColumnDoubleSided
   \documentclass[notitlepage,twoside]{\crLaTeXDocumentType}
\else
   \ifdefined\crDoubleColumnSingleSided
      \documentclass[notitlepage,oneside]{\crLaTeXDocumentType}
   \else
      \ifdefined\crSingleColumnDoubleSided
         \documentclass[notitlepage,twoside]{\crLaTeXDocumentType}
      \else
         % is crSingleColumnSingleSided
            \documentclass[notitlepage,oneside]{\crLaTeXDocumentType}
      \fi
   \fi
\fi


% Page Geometry
%--------------

% Sheet size:                         % Not sure we need these values,
\newlength{\sheetWidth}               % but define them anyway.
\setlength{\sheetWidth}{8.5in}
\newlength{\sheetHeight}              
\setlength{\sheetHeight}{11in}

% Single Column:
%    Single sided or 1-up:  1.75 + 5 + 1.75
%    Double sided or 2-up:  2    + 5 + 1.5
\newlength{\singleColumnMeasure}
\setlength{\singleColumnMeasure}{5in} 

% Double Column:
%    Single sided or 1-up:  1 + 3.25 + 0.25 + 3.25 + 0.75
%    Double sided or 2-up:  1 + 3.25 + 0.25 + 3.25 + 0.75
\newlength{\doubleColumnColumnMeasure}
\setlength{\doubleColumnColumnMeasure}{3.25in}    
\setlength{\columnsep}{0.25in}  

% Margins, single column:
\newlength{\leftMarginSingleColumn}          % left/right for single sided/1up
\setlength{\leftMarginSingleColumn}{1.75in}
\newlength{\rightMarginSingleColumn}              
\setlength{\rightMarginSingleColumn}{1.75in}
\newlength{\innerMarginSingleColumn}         % inner-outer for double sided/2up
\setlength{\innerMarginSingleColumn}{2in}    % note:  NOT same as 1up
\newlength{\outerMarginSingleColumn}              
\setlength{\outerMarginSingleColumn}{1.5in}

\newlength{\topMarginSingleColumn}
\setlength{\topMarginSingleColumn}{1.25in}
\newlength{\bottomMarginSingleColumn}
\setlength{\bottomMarginSingleColumn}{1in}

% Margins, double column:
\newlength{\leftMarginDoubleColumn}          % left/right for single sided/1up
\setlength{\leftMarginDoubleColumn}{1in}
\newlength{\rightMarginDoubleColumn}              
\setlength{\rightMarginDoubleColumn}{0.75in}
\newlength{\innerMarginDoubleColumn}         % inner/outer for double sided/2up
\setlength{\innerMarginDoubleColumn}{1in}    % note:  same as 1up
\newlength{\outerMarginDoubleColumn}          
\setlength{\outerMarginDoubleColumn}{0.75in}

\newlength{\topMarginDoubleColumn}
\setlength{\topMarginDoubleColumn}{0.75in}
\newlength{\bottomMarginDoubleColumn}
\setlength{\bottomMarginDoubleColumn}{1in}


% Packages
%---------

% work through "backwards," so Single Column Single Sided is the default
\ifdefined\crDoubleColumnDoubleSided
\usepackage[letterpaper,
%            showframe,
            inner   = \innerMarginDoubleColumn,
            outer  = \outerMarginDoubleColumn,
            top    = \topMarginDoubleColumn,
            bottom = \bottomMarginDoubleColumn]
            {geometry}
\else
   \ifdefined\crDoubleColumnSingleSided
   \usepackage[letterpaper,
   %            showframe,
               left   = \leftMarginDoubleColumn,
               right  = \rightMarginDoubleColumn,
               top    = \topMarginDoubleColumn,
               bottom = \bottomMarginDoubleColumn]
               {geometry}
   \else
      \ifdefined\crSingleColumnDoubleSided
      \usepackage[letterpaper,
      %            showframe,
                  inner  = \innerMarginSingleColumn,
                  outer  = \outerMarginSingleColumn,
                  top    = \topMarginSingleColumn,
                  bottom = \bottomMarginSingleColumn]
                  {geometry}
      \else
         % is crSingleColumnSingleSided
         \usepackage[letterpaper,
         %            showframe,
                     left   = \leftMarginSingleColumn,
                     right  = \rightMarginSingleColumn,
                     top    = \topMarginSingleColumn,
                     bottom = \bottomMarginSingleColumn]
%                     bottom = \bottomMarginSingleColumn,verbose]
                     {geometry}
      \fi
   \fi
\fi



% Packages
%---------
% The order here is generally alphabetical, except for:
% fontspec (which requires package anyfontsize),
% xeCJK (which has a lot of explanation) and
% hyperref (which must always appear last).


\usepackage{blindtext}     % fancier lorem ipsum; see also package lipsum
%\usepackage{boxedminipage} % useful for debugging minipages
\usepackage{caption}
\usepackage{contour}       % for Alex Chan's underlining (see also ulem)
\usepackage{csquotes}      % used for block quotes only
\usepackage{enumitem}

% See: https://tex.stackexchange.com/questions/117328/
%   fancyhdr-does-not-apply-same-header-footer-on-chapter-and-non-chapter-pages
%   answer 52,  Jun 2, 2013, by Mico
\usepackage{etoolbox}      % FOLD BACK IN TO cr-formatting
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}

\usepackage{ifthen}
\usepackage{indentfirst}   % to avoid LaTeX's stupid default
\usepackage{fancyhdr}
\usepackage{fancyvrb}      % to control verbatim environment
                           % allows verbatim in footnotes
\usepackage{graphicx}      % 
\usepackage{hanging}       % for hanging indents, for bibliographic entries
\usepackage{lipsum}        % lorem ipsum text; see also package blindtext
\usepackage{multicol}
\usepackage{ragged2e}      % Make the Bibliography RaggedRight
\usepackage{tabto}         % to get tabs
\TabPositions{0.5in, 1.0in, 1.5in, 2.0in, 2.5in, 3.0in,%
              3.5in, 4.0in, 4.5in, 5.0in, 5.5in, 6.0in,%
              6.5in}
\usepackage{textcomp}      % to get copyright and trademark symbols
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage[normalem]{ulem}% for Alex Chan's underlining (see also contour)
\usepackage{wrapfig}
\usepackage{xltxtra}       % to get XeLaTeX logo
\usepackage{xstring}       % for my boilerplate texts
\usepackage{xurl}          % linebreaks in URLs

% Font packages.  Actual font setting is done in cr-formatting-setup.texfrag
\usepackage{fontspec}      % most modernfont handling at time of writing
\usepackage{anyfontsize}   % otherwise we get strange errors in footnotes

% CJK usage note.
% The xeCJK package requires all font setting commands to be in the preamble
% (fontspec puts them in the document).
% The Google Noto fonts provide separate fonts for Chinese traditional,
% Chinese simplified, Japanese and Korean.  This is annoying because you
% can only do \setCJKmainfont once (in the preamble).
% The solution is to do this for one script (I'll do it for traditional Chinese)
% and then use \newCJKfontfamily to define additional font families for
% other scripts.
% To use such a second script (assuming \simplifiedChinese, as below), do, e.g.:
%    {\simplifiedChinese 王祯 }
\usepackage{xeCJK}
\setCJKmainfont{Noto Sans CJK TC}
\newCJKfontfamily\simplifiedChinese{Noto Sans CJK SC}
\setCJKsansfont{Noto Sans CJK TC} % we don't use CJK sans or mono (tt), but if
\setCJKmonofont{Noto Sans CJK TC} % we don't declare something, TeX complains

\usepackage{xr-hyper}
% The most common online advice suggests putting hyperref as the last package.
\usepackage[xetex]{hyperref} % for clickable links

\input{cr-formatting-hyperref-patch.texfrag}


% Underlining
%------------
% This is Alex Chan's solution.  See:
% https://alexwlchan.net/2017/10/latex-underlines/
% It requires the packages contour and ulem.

\renewcommand{\ULdepth}{1.8pt}
\contourlength{0.8pt}

\newcommand{\chanUnderline}[1]{%
  \uline{\phantom{#1}}%
  \llap{\contour{white}{#1}}%
}


% Visual Presentation
%--------------------

% Titles (as of works in citations, 
% not the title of the document being written).
\newcommand{\crTitle}[1]{\textit{#1}} % of a book, journal, etc.
\newcommand{\crTitleCJK}[1]{#1}       % No italics for title in CJK
\newcommand{\crMinorTitle}[1]{``#1''} % of an article, chapter, section, etc.

% A formal term, keyword, or program identifier, distinguished visually.
\newcommand{\crTerm}[1]{\textbf{#1}}
\newcommand{\crTTTerm}[1]{\textbf{\texttt{#1}}}

% For italics, bold, typewriter, and emphasized text 
% use the standard LaTeX constructs
% \textit{}, \textbf{}, \texttt{}, \emph{}

% For literal program code use the standard LaTeX verbatim environment.

% Show Clickability
% The command for clickability should be separate from the command for
% underlining in case we want to change the visual presentation of
% clickability later.
\newcommand{\crShowClickability}[1]{\chanUnderline{#1}}

% Bibliographic Entries and Notes; Citations

\newlength\crBiblioIndent
\setlength{\crBiblioIndent}{\parindent}
\newcommand{\crBiblioEntry}[1]{\medskip\hangpara{\crBiblioIndent}{1}{#1}\par} 

\newcommand{\crBiblioNote}[1]{\smallskip\hangpara{\crBiblioIndent}{0}{#1}\par}

\newcommand{\crBiblioCitation}[1]{(#1)}



% Other Column/Duplex Settings
%-----------------------------

% work through "backwards," so Single Column Single Sided is the default
\ifdefined\crDoubleColumnDoubleSided
   \linespread{1}
\else
   \ifdefined\crDoubleColumnSingleSided
      \linespread{1}
   \else
      \ifdefined\crSingleColumnDoubleSided
         \linespread{1.25}
      \else
         % is crSingleColumnSingleSided
         \linespread{1.25}
      \fi
   \fi
\fi

% Miscellaneous Other Settings
%-----------------------------

%\raggedbottom                         % helps to avoid underfull vboxes
\newcommand{\crTB}{\textbackslash}    % conveniently shorter

