4

Consider the following MWE, extracted from my solution to this question:

\begin{filecontents*}{demolist1.dat}
1,2,3
\end{filecontents*}
\begin{filecontents*}{demolist2.dat}
{1,0,2},{0,3},{1,1}
\end{filecontents*}
\documentclass{article}
\usepackage{catchfile}
\usepackage{pgffor}

\CatchFileDef{\mylist}{demolist1.dat}{}
\CatchFileDef{\mynestedlist}{demolist2.dat}{}

\begin{document}
\foreach \x in \mylist {
  ``\x'' \quad
}

\hrulefill

\foreach \x in \mynestedlist {
  \foreach \y in \x {
    ``\y'' \quad
  }
  \par
}
\end{document}

it produces

preview

Note the extra space in the last element of the first list and the collapse of the last element of the second list.

I cannot understand why the last element of each list is treated differently.

Any idea on how to fix this?

Bordaigorl
  • 15,135

1 Answers1

6

The problem is the endline automatically implied at the end of input files.

I removed all you spurious spaces to make the output clearer.

\begin{filecontents*}{\jobname1.dat}
1,2,3
\end{filecontents*}
\begin{filecontents*}{\jobname2.dat}
{1,0,2},{0,3},{1,1}
\end{filecontents*}
\documentclass{article}
\usepackage{catchfile}
\usepackage{pgffor}

\CatchFileDef{\mylist}{\jobname1.dat}{\endlinechar=-1 }
\CatchFileDef{\mynestedlist}{\jobname2.dat}{\endlinechar=-1 }

\begin{document}
\foreach \x in \mylist {``\x''\quad}

\hrulefill

\foreach \x in \mynestedlist {\foreach \y in \x {``\y''\quad}\par}
\end{document}

enter image description here

egreg
  • 1,121,712