1

I am trying to implement a workaround for the error I get when inputting a table as external file in a file using \input. The code snippet below works for this purpose:

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

I now want to only apply it on some computers, not others, so wanted to put it inside \IfFileExists. I don't know whether it is the best solution, but it certainly does not work:

\documentclass[12pt,fleqn]{article}
\IfFileExists{filename}
{ \ExplSyntaxOn \cs_new:Npn \expandableinput #1 
{ \use:c { @@input } { \file_full_name:n {#1} } }
 \AddToHook{env/tabular/begin}
 { \cs_set_eq:NN \input \expandableinput }
 \AddToHook{env/tabular*/begin}
 { \cs_set_eq:NN \input \expandableinput }
 \ExplSyntaxOff}
{}
\begin{document}
\end{document}

I would like to make it work if possible. Alternative solutions would also be welcome. E.g., would it work to use \IfFileExists to define some other variable, and use it as argument to a conditional?

nicolae
  • 195

1 Answers1

2

A better test in this case would be to test for the LaTeX format you are using. If you have at least 2020-10-01, then you apply the code, otherwise do nothing. You can test for that with \IfFormatAtLeastTF{2020-10-01}.

Then, you need to write \ExplSyntaxOn/Off outside of the argument, otherwise it will not work (similar to \makeatletter).

The code would be like this:

\RequirePackage{expl3} % required for compatibility with older releases
\makeatletter
\providecommand\IfFormatAtLeastTF{\@ifl@t@r\fmtversion}
\makeatother
\ExplSyntaxOn
\IfFormatAtLeastTF{2020-10-01}
  {
    \cs_new:Npn \expandableinput #1
      { \use:c { @@input } { \file_full_name:n {#1} } }
    \AddToHook{env/tabular/begin}
      { \cs_set_eq:NN \input \expandableinput }
    \AddToHook{env/tabular*/begin}
      { \cs_set_eq:NN \input \expandableinput }
  }
  { }
\ExplSyntaxOff
  • Thanks for this. It turns out, however, that a system running texlive 2019 leads to an error: "Undefined control sequence. \ExplSyntaxOn." Same with \ExplSyntaxOff. Interstingly (?), in 2019 placing \ExplSyntaxOn/Off inside argument fixes the error, but introduces it in 2021. – nicolae Apr 07 '22 at 22:57
  • @nicolae Oh, right, with TeX Live before 2019 you have to load expl3 explicitly (I added it in my answer). Writing \ExplSyntaxOn inside the argument there is wrong (so much that it doesn't work for TL 2021), and it only works for TL 2019 because the False branch is taken – Phelype Oleinik Apr 07 '22 at 23:25
  • Yes, of course the false branch was taken, that was the whole point. I should have seen that right away. – nicolae Apr 10 '22 at 04:07