I discovered catchfile here and now I would like to understand how works this package.
Below some tests with \foreach
or \@for
\documentclass{article}
\usepackage{filecontents,catchfile,pgffor}
\begin{document}
\def\mylist{1,2,3,4}
Test 1:\foreach \i in \mylist {\hspace{\i cm}\i}
\begin{filecontents*}{mylist.dat}
1,2,3,4
\end{filecontents*}
\CatchFileDef{\tempa}{mylist.dat}{}%
Test 2:\foreach \i in \tempa{\hspace{\i cm}\i}
\makeatletter
Test 3:\@for\i:=\tempa\do{\hspace{\i cm}\i}
\def\tempb{\@@input mylist.dat }
% I know this is not the right way but I always find it difficult to explain
Test 4: this is the content : \tempb
% \foreach \i in \tempb
% {\hspace{\i cm}\i} %
Test 5:\@for\i:=\tempb\do{\hspace{\i cm}\i}
\begin{filecontents*}{myfile.dat}
\def\tempc{1,2,3,4}
\end{filecontents*}
\input{myfile.dat}
% last try ! and it works
Test 6:\foreach \i in \tempc{\hspace{\i cm}\i}
\end{document}
A) Test 4 does not work. but why Test 2 with \CatchFileDef
works ? It would be interesting to have a clean explanation about this kind of problem !
B) Now why Test 6 works ?
\everyeof{\noexpand}
or similar to avoid an error triggered by the end of the file. The package however also allows for catcode adjustments for the input file, IIRC.\def\tempb{\input{mylist.dat}}
but I got an error directly with\edef\tempb{\input{mylist.dat}}
. I need to write\expandafter\def\expandafter\tempb{\input{mylist.dat}}
but in this case the test 5 does not compile\CatchFileDef
has three arguments.\CatchFileDef{\tempa}{mylist.dat}{}
but I forgot why I removed{}
.