# How to trim the end of line character for each line read from an external file?

How to trim the end of line character for each line read from an external \jobname.list file below? Note that the real scenario does not use filecontents package to generate the list. I use a batch file to prepare the list. The following MWE is just for the sake of simplicity.

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
a.jpg
b.pdf
c.eps
d.png
\end{filecontents*}

\parindent=0pt
\usepackage{graphicx}

\begin{document}
\makeatletter
\loop
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{../../Images/\x}%
\repeat
\makeatother
\end{document}


The code above cannot be compiled because \includegraphics does not like the end of line charcter appended in the file path. I wasted much time to find the source of problem.

## Edit:

If the image files a.jpg and b.pdf are put in ../../Images/ then the proposed solutions no longer work. How to solve it?

I mainly use xelatex to accommodate 4 image formats.

-
Add \begingroup\endlinechar=-1 before \loop and \endgroup after \repeat. Also put a space (or \par) after {\x}. –  egreg Sep 25 '12 at 9:39

An alternative approach to Herbert's is to remove the space at the end of the line, which is what TeX converts the end-line to (assuming no blank lines, of course)

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.pdf
example-grid-100x100pt.png
\end{filecontents*}

\parindent=0pt
\usepackage{graphicx}

\begin{document}
\def\stripend#1 \stop{\unexpanded{#1}}
\loop
\edef\x{%
\unexpanded{%
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]%
}%
{\expandafter\stripend\x \stop}%
}%
\x
\repeat
\end{document}

-
@GarbageCollector I've edited to expand \x once before using \includegraphics. That should I hope solve any issues (which are nothing to do with the line end: it's the way \includegraphics does a file search that's important). –  Joseph Wright Sep 25 '12 at 12:04

without grouping:

[...]
\begin{document}
\makeatletter
\loop
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}%
\repeat
\makeatother
[...]


if the images are saved in another directory then use (example for images in /tmp/):

[...]
\edef\filename{/tmp/\x}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\filename}%
[...]

-
What if XeTeX is used and \endlinechar equals 20000? Just joking. ;-) –  egreg Sep 25 '12 at 9:45
defined in latex.ltx the main source for LaTeX: \mathchardef\@M=10000 –  Herbert Sep 25 '12 at 9:47
see my edit, which works for me. –  Herbert Sep 25 '12 at 11:44
It does not work with xelatex/latex when we import EPS file. –  please stop stalking Sep 25 '12 at 11:55
LuaTeX throws an error "Invalid \endlinechar" if \endlinechar is set to a value greater than 127. Therefore it is more portable and safe to assign a negative value (-1) either in a group or with saving/restoring the previous value. –  Heiko Oberdiek Sep 25 '12 at 12:43

The following approach works also when the file name in \includegraphics has some prefix (the problem mentioned in the question probably has to do with some setting made to \endlinechar in graphics.sty)

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.jpg
example-grid-100x100pt.png
\end{filecontents*}

\parindent=0pt
\usepackage{graphicx}

\begin{document}

\begingroup\endlinechar=-1
\loop
\edef\x{../Images/\x}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}
\repeat
\endgroup
\makeatother
\end{document}


Here is an expl3 implementation

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.list}
example-image-a.jpg
example-grid-100x100pt.png
\end{filecontents*}

\usepackage{graphicx}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\includefromfile}{O{} m m}
{
\garbcoll_include:nnn { #1 } { #2 } { #3 }
}
\cs_new_protected:Npn \garbcoll_include:nnn #1 #2 #3
{
{
\tl_set:Nx \l_tmpa_tl { #2 \tl_trim_spaces:n { ##1 } }
\includegraphics[ #1 ]{ \l_tmpa_tl } \par
}
}
\ExplSyntaxOff

\parindent=0pt

\begin{document}

\includefromfile
[width=\textwidth,height=\textheight,keepaspectratio]
{../Images/}{\jobname.list}

\end{document}

-
@GarbageCollector Yes, it does. Check the simplified version and also the expl3 one. –  egreg Sep 25 '12 at 13:34
It does not work with xelatex/latex when we import EPS file. –  please stop stalking Sep 25 '12 at 15:32
@GarbageCollector It's a problem in xetex.def, I believe. The expl3 version works. –  egreg Sep 25 '12 at 15:41