2

Context: I'm developing a title page (writing my own titlepage environment) and I'm writing my own \thanks command (to give me control over where I spew the footnotes). Not knowing how \footnote does this, I decided to write each footnote's text out to a file (using the newfile package to manage I/O) and then later read from that file into a tabular to format the hanging footnotes.

I ran into problems (various errors and a badly formatted tabular for the second and later footnotes). In developing an MWE to explain the problem, I discovered to my shock that simply commenting out a \centering command caused it to work perfectly.

Why is \centering causing the cascade of errors? How can I retain \centering and still get the desired functionality and output format?

In the following MWE, I draw your attention/scrutiny to \storeTitle and \printTitle because I harbor some suspicion about how I pass the argument #1 from \storeTitle to \printTitle (and whether the expansion(s) is/are appropriate). (I realize the redefining of \printTitle may seem circuitous. Specifying a title is optional. If you don't call \storeTitle, then \printTitle remains empty and nothing happens there.)

\documentclass{article}
\usepackage{newfile}
\newoutputstream{mystream}% Creates file stream for footnotes for title page
\openoutputfile{\jobname.mystream}{mystream}

\newcommand{\printTitle}{}
\newcommand{\storeTitle}[1]{\renewcommand{\printTitle}{{\large#1}}}

\newcounter{ntpfootnote}
\newcommand*{\fnmark}{\textit{\textsuperscript{\alph{ntpfootnote}}}}
\newcommand{\rowForTabular}[1]{\fnmark & #1 \\}
\newcommand{\mythanks}[1]{%
    \addtocounter{ntpfootnote}{1}%  Increments footnote counter
    {\fnmark}% Prints formatted footnote mark
%   Adds to output stream a row of text to become a row of the tabular representing the footnotes
    \addtostream{mystream}{\rowForTabular{#1}}   
}

\storeTitle{Some title\mythanks{with a first footnote.} and also a second footnote.\mythanks{This is the second footnote.}}
\begin{document}

%\centering% Uncomment this line to induce failure
\printTitle
\closeoutputstream{mystream}
\bigskip

\begin{tabular}%
    {p{8pt} p{5.5in}|}%
    \input{\jobname.mystream}
\end{tabular}
\end{document}

The output (uncentered, but otherwise as desired) is: enter image description here

If I then uncomment the \centering command, I get a bunch of errors citing the location of \printTitle:

  • Argument of \@firstoftwo has an extra }.
  • Runaway argument? Paragraph ended before \@firstoftwo was complete.
  • Missing \endcsname inserted.
  • Argument of \@argdef has an extra }.
2

The "cascade" of errors you mention are consequences of the first. TeX, as a macro expansion language, struggles to recover from errors, so one error can possibly induce much more.

The error happens with \centering because it redefines \\, which is expanded when written to the file, and the new definition breaks when expanded in that context.

To fix the issue you can \protect the \\ to avoid its expansion when writing:

\documentclass{article}
\usepackage{newfile}
\newoutputstream{mystream}% Creates file stream for footnotes for title page
\openoutputfile{\jobname.mystream}{mystream}

\newcommand{\printTitle}{}
\newcommand{\storeTitle}[1]{\renewcommand{\printTitle}{{\large#1}}}

\newcounter{ntpfootnote}
\newcommand*{\fnmark}{\textit{\textsuperscript{\alph{ntpfootnote}}}}
\newcommand{\rowForTabular}[1]{\fnmark & #1 \protect\\}% <- \protect here
\newcommand{\mythanks}[1]{%
    \addtocounter{ntpfootnote}{1}%  Increments footnote counter
    {\fnmark}% Prints formatted footnote mark
%   Adds to output stream a row of text to become a row of the tabular representing the footnotes
    \addtostream{mystream}{\rowForTabular{#1}}% <- Here
}

\storeTitle{Some title\mythanks{with a first footnote.} and also a second footnote.\mythanks{This is the second footnote.}}
\begin{document}

\centering% Uncomment this line to induce failure
\printTitle
\closeoutputstream{mystream}
\bigskip

\begin{tabular}%
    % {p{8pt} p{5.5in}|}%
    {p{8pt} p{\dimexpr\textwidth-8pt-4\tabcolsep}|}% <- Suggestion
    \input{\jobname.mystream}
\end{tabular}
\end{document}

You also had an extra space after the \addtostream command, which was enlarging the space after the footnote.

Also, I suggested a way to avoid the overfull \hbox from your table.

  • Thanks! I never would have guessed that \centering would have redefined \\ and/or that that would have caused a problem. Just as a lesson for the future, is \protecting such a use of `\` something that a seasoned LaTeX programmer would do just by instinct and prudence? Or would one need to think that many moves ahead to decide to do that? (I'm a chess player, hence the analogy) – Jim Ratliff Oct 27 '18 at 1:04
  • 1
    All the alignment command redefine \\ (this is one of the reasons why \\ shouldn't be used to break lines in normal text flow), see around line 620 of ltmiscen.dtx. You need to \protect fragile commands (which can't full expand safely) in full expansion contexts, such as inside an \edef or a \write. Some commands are known to be fragile and always need to be protected, some are discovered when they break. One common case is a fragile command inside a \caption (which \writes to the .lof file). – Phelype Oleinik Oct 27 '18 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.