3

This piece of code used to compile just fine with MiKTeX 2.9.6300:

\documentclass{article}

\usepackage{pgffor}

\begin{document}

\input{forEachInput.tex} { test \fruit }

\end{document}

The file forEachInput.tex contains just one line with

\foreach \fruit in {apple,cherry,banana}

with no space at the end.

Now if I try to compile the same file with MiKTeX 23.4, I get an error: Paragraph ended before \pgffor@next was complete.

On the other hand, if I type directly the content of forEachInput.tex inside the main file (not using input anymore), both MiKTeX 2.9.6300 and MiKTeX 23.4 work perfectly.

It seems something changed its behavior between 2.9.6300 and 23.4? In such a case, I can't figure if it's the \input or the pgffor package, nor can I understand how to make my code work again...

Giulio
  • 327
  • 3
    sorry no, that doesn't work in a current LaTeX. you can not start a command in a file and then continue it after it, the file hooks from input will interfere. You need an expandable input, see e.g. https://tex.stackexchange.com/a/639993/2388 – Ulrike Fischer Aug 03 '23 at 10:02
  • Very helpful thank you! I dont fully understand yet: is it expected that this used to work on an older version of miktex (i used 2.9.6300 in the past, also edited my post to specify this)? So it is a problem with \input, not with pgffor, right? – Giulio Aug 03 '23 at 22:03
  • The miktex version is not relevant. You should look at the LaTeX version at the begin of your log file. And yes LaTeX changed here and \input no longer allows such tricks. – Ulrike Fischer Aug 03 '23 at 22:33
  • @UlrikeFischer old working version is "This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6300 64-bit)" whereas new non-working version is "This is pdfTeX, Version 3.141592653-2.6-1.40.25 (MiKTeX 23.4)" – Giulio Aug 06 '23 at 10:57
  • is there a workaround with \protect maybe? – Giulio Aug 06 '23 at 10:58
  • you need an expandable input, adding \protect doesn't help here, it makes things less expandable. Why don't you use the answers your got? – Ulrike Fischer Aug 06 '23 at 11:35
  • I will for sure - I was just trying to understand better, since if I start to have code I dont understand in my files... well.. that s the beginning of the end.... – Giulio Aug 07 '23 at 13:07

2 Answers2

4

This works only with (internal) low-level macro \@input:

\begin{filecontents*}{forEachInput.tex}
\foreach \fruit in {apple,cherry,banana}
\end{filecontents*}
\documentclass{article}

\usepackage{pgffor}

\begin{document}

\csname @input\endcsname{forEachInput.tex} { test \fruit }

\end{document}

With the user interface commands \input, \InputIfFileExists etc. there is code executed before and after reading a file. See ltfilehook-doc.pdf for information.

cabohah
  • 11,455
  • this seems to work just fine (even if i dont understand exactly what this is doing). Thank you very much! – Giulio Aug 06 '23 at 10:59
  • @Giulio \csname @input\endcsname is \@input without using \makeatletter and \makeatother (because of the @ in the macro name). And it works, because \@input does not call, i.e., the file hooks (documented in ltfilehook-doc.pdf). If there is anything else, what you do not understand, you could ask … – cabohah Aug 06 '23 at 11:03
  • Thanks a lot! Let me think about it and see how close can i get to an understanding :) – Giulio Aug 07 '23 at 13:06
  • Btw, the pdf you mention is this one https://www.latex-project.org/help/documentation/ltfilehook-doc.pdf ? – Giulio Aug 07 '23 at 13:06
  • @Giulio This file should be available in the doc tree of every LaTeX distribution. – cabohah Aug 07 '23 at 15:58
  • right found it! thx again!! – Giulio Aug 09 '23 at 15:50
1

You can use the catchfile package to load the contents of the file in a macro and use that, repeatedly if needed.

Code

\begin{filecontents*}{\jobname-data.tex}
\foreach \fruit in {apple, cherry, banana}
\end{filecontents*}
\documentclass{article}
\usepackage{pgffor, catchfile}
\newcommand*\Input[2][\foreachFile]{\CatchFileDef{#1}{#2}{}#1}
\begin{document}
\Input{\jobname-data.tex}{test: \fruit \par}

\foreachFile{again: \fruit \par} \end{document}

Qrrbrbirlbel
  • 119,821