% -----------------------------------------------------------------------------
% preamble includes BEGIN
% -----------------------------------------------------------------------------
\usepackage{longtable, lscape, ifthen}

% -----------------------------------------------------------------------------
% put \T or \B at the ends of lines to add space for super- or sub-
%   scripts above or below, respectively
% ref: ?
\newcommand\T{\rule{0pt}{2.6ex}}       % Top strut
\newcommand\B{\rule[-1.2ex]{0pt}{0pt}} % Bottom strut

% -----------------------------------------------------------------------------
% horizontal line commands; ideally, these would compute the width rather than
%   hardcoding it
% ref: ?
\def\hlinport{\makebox[6.5in]{\hrulefill} \\} % hline outside tabular, port
\def\hlinlscp{\makebox[9in]{\hrulefill} \\} % hline outside tabular, lndscp
%ref: https://stackoverflow.com/a/67335722
\def\hlinnotab{\\makebox[1.0\linewidth]{\hrulefill}\\[1ex]}

% -----------------------------------------------------------------------------
% ref: https://latex.org/forum/viewtopic.php?p=23257#p23257
\newcommand{\nonemptyline}[1]{%
  %\ifthenelse{\equal{#1}{}}{do when empty}{do when not empty}
  \ifthenelse{\equal{#1}{}}{}{#1}%
}

% -----------------------------------------------------------------------------
% For RMarkdown, I needed to put this into a preamble.tex file and include it
%   via `output: pdf_document: includes: in_header: preamble.tex` because
%   Markdown was expanding the \tabfill command before writing the tex file
% ref: https://tex.stackexchange.com/a/119477 in reply to
%      https://tex.stackexchange.com/questions/119473/tabbing-and-line-wrapping
\makeatletter
\newlength\tdima
\newcommand\tabfill[1]{\setlength\tdima{\linewidth}%
  \addtolength\tdima{\@totalleftmargin}%
  \addtolength\tdima{-\dimen\@curtab}%
  \parbox[t]{\tdima}{#1\ifhmode\strut\fi}}
  %\parbox[t]{\tdima}{\nonemptyline{#1}\ifhmode\strut\fi}}
\makeatother
%
% Create a tabbing environment in which to use tabfill
% param #1 is specified the tabstops (as expected by the tabbing
% environment) and is provided in braces after invocation, e.g.:
%   \begin{tabwrap}{\hspace{1.25in}\=}
% param #2 is the contents of the envirnent
\newenvironment{tabwrap}[2]{%
  \begin{tabbing}#1\kill\ignorespaces%
  #2}%
  {\end{tabbing}%
}

% -----------------------------------------------------------------------------
% Make a caption for a non-floating figure or table, e.g.,
% ref: https://github.com/rf-latex/capt-of/blob/main/capt-of.dtx
%      https://texfaq.org/FAQ-figurehere
%   
% Usage: \captionof{*type*}[*move*]{*caption*}
%        *type*    is `figure` or `table` (or some type you've
%                    defined with the`float` package)
%        *move*    is the optional moving argument *caption* (the thing
%                    that goes to the list of tables/figures)
%        *caption* is the text of the caption
\makeatletter
\newcommand\captionof[1]{\def\@captype{#1}\caption}
\makeatother
%
%%ACE \captionof{table}{Hello world from line 210}
% To circumvent mis-numbering of interleaved float and non-float table
%   and figure captions, it is necessary to include the `perpage` package and
%   "make them sorted" (FFI see https://texfaq.org/FAQ-figurehere)
% I (ACE) don't know how to get this package to include:
%    \usepackage{bigfoot}
% so I included the source instead:
\makeatletter
\input{perpage.tex}
\makeatother
%
% Ensure that table numbers are sorted
\MakeSorted{table}
% Ensure that figure numbers are sorted
\MakeSorted{figure}

% -----------------------------------------------------------------------------

% -----------------------------------------------------------------------------
% preamble includes END
% -----------------------------------------------------------------------------
