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?

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

1
  • Spot on as always! How could I miss that... – Bordaigorl Sep 26 '14 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.