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.

Fran
  • 80,769
  • 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. – Ulrike Fischer Aug 23 '21 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 '21 at 22:34
  • 1
    @Fran https://tex.stackexchange.com/a/583939/134574 – Phelype Oleinik Aug 23 '21 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. – Phelype Oleinik Aug 24 '21 at 00: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 '21 at 00:29

2 Answers2

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 }
  • Phelype, I'm having the same issue: https://tex.stackexchange.com/questions/622212/lyx-misplace-noalign-include-tex-table I tried adding your suggestion to preamble but that did not fix it. Any ideas? – David Zentler-Munro Nov 12 '21 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 – Phelype Oleinik Nov 12 '21 at 14:08
  • Great thanks: that works! – David Zentler-Munro Nov 12 '21 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? – David Zentler-Munro Nov 17 '21 at 17:21
  • @DavidZentler-Munro No idea. I've never used LyX. If you send me the exported .tex file I can investigate – Phelype Oleinik Nov 17 '21 at 17:28
  • I've added tex file to answer below (let me know if I could provide it in a more useful format) – David Zentler-Munro Nov 17 '21 at 18:56
  • @DavidZentler-Munro It's hard to tell without the contents of the file, but the path /tables/MWE2.tex looks suspicious. In a Linux machine that points to a folder tables in the root directory (which is very unusual). Try with \estauto{tables/MWE2.tex}{3}{c}. But please, don't post additional code in an answer (use some service like https://pastebin.com): that space is reserved for actual answers only. – Phelype Oleinik Nov 18 '21 at 01:45
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

L.J.R.
  • 10,932