5

In a large project, I reuse with \input tabular rows stored as child files. This has worked perfectly in the past, but in my texlive 2021 this fail when a booktabs rule follow the input. MWE:

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{ccc}
\toprule
    1 & 2 & 3\\
\midrule    
\input{foo}
\bottomrule % This causes the error
\end{tabular}
\end{document}

Where foo.tex is simply a & b & c \\.

It fails also ending the tabular rows with \tabularnewline or \cr. The funny point is that \bottomrule works fine inside foo.tex (a & b & c \\\bottomrule) but this is not good a solution because foo.tex could be needed in another table, and not necessarily as the last row. AFAIK, unlike \include, \input add strictly the content of the file, so it should be irrelevant if \bootomrule is inside or outside the child file, but is it not. I wonder if there is a better fix that \input{foot}\\[-12pt]\bottomrule.

5
  • 2
    Input has now hooks and so is more complicated. Search for an expandable input on the site, there were already some questions about this. Aug 23, 2021 at 22:26
  • 1
    Consider using catchfile to store the contents of foo.tex inside a macro that you can then use (\CatchFileDef{\foo}{foo.tex}{}).
    – Werner
    Aug 23, 2021 at 22:34
  • 1
    @Fran tex.stackexchange.com/a/583939/134574 Aug 23, 2021 at 22:36
  • 1
    @Fran I'd recommend you do something like \ExplSyntaxOn \cs_new:Npn \expandableinput #1 { \use:c { @@input } { \file_full_name:n {#1} } } \AddToHook{env/tabular/begin} { \cs_set_eq:NN \input \expandableinput } \ExplSyntaxOff (similar to this). Completely overriding \input may break other things down the road. The above will only change \input in a tabular environment. Aug 24, 2021 at 0:03
  • @PhelypeOleinik I will need also for tabulary and tabularx, but anyway is a good idea since only modify the preamble. Consider to expand your comment in an answer.
    – Fran
    Aug 24, 2021 at 0:29

2 Answers 2

7

Your issue is the same as here and similar to this one. Newer \input adds unexpandable tokens in the way of \noalign (from \bottomrule), then when the \noalign is seen it is too late and you get the error. With no changes to the document body, you can add this to your preamble:

\ExplSyntaxOn
\cs_new:Npn \expandableinput #1
  { \use:c { @@input } { \file_full_name:n {#1} } }
\AddToHook{env/tabular/begin}
  { \cs_set_eq:NN \input \expandableinput }
\ExplSyntaxOff

The code above will redefine \input to be \expandableinput in all tabular environments. To add that to more environments, simply replicate the two lines and change the environment name:

\AddToHook{env/tabularx/begin} % also for tabularx
  { \cs_set_eq:NN \input \expandableinput }
\AddToHook{env/tabulary/begin} % and for tabulary
  { \cs_set_eq:NN \input \expandableinput }
7
  • Phelype, I'm having the same issue: tex.stackexchange.com/questions/622212/… I tried adding your suggestion to preamble but that did not fix it. Any ideas? Nov 12, 2021 at 13:18
  • 1
    @DavidZentler-Munro In your code you seem to be using a custom \estinput command, so you'd have to change \cs_set_eq:NN \input \expandableinput to \cs_set_eq:NN \estinput \expandableinput in the code above Nov 12, 2021 at 14:08
  • Great thanks: that works! Nov 12, 2021 at 14:30
  • Weirdly this solution works in lyx but when I try to export to tex and run from tex it does not work (I get the same misplace noalign error). Any idea why this might be? Nov 17, 2021 at 17:21
  • @DavidZentler-Munro No idea. I've never used LyX. If you send me the exported .tex file I can investigate Nov 17, 2021 at 17:28
0

I will provide an alternative solution with tabularray package of version 2022B (2022-06-01), by using evaluate option in functional library:

\begin{filecontents*}[overwrite]{foo.tex}
  a & b & c \\
\end{filecontents*}

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,functional}
\begin{document}
\begin{booktabs}[evaluate=\fileInput]{ccc}
\toprule
  1 & 2 & 3\\
\midrule
  \fileInput{foo}
\bottomrule
\end{booktabs}
\end{document}

where \fileInput function is provided by functional library.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .