7

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}
\newread\reader

\begin{document}
\makeatletter
\openin\reader=\jobname.list\relax
\advance\endlinechar \@M
\loop
    \read\reader to \x
    \unless\ifeof\reader
    \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{../../Images/\x}%
\repeat
\advance\endlinechar -\@M
\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.

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

3 Answers3

6

without grouping:

\newread\reader
[...]
\begin{document}
\makeatletter
\advance\endlinechar \@M
\openin\reader=\jobname.list\relax
\loop
    \read\reader to \x
    \unless\ifeof\reader
    \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}%
\repeat
\closein\reader
\advance\endlinechar -\@M
\makeatother
[...]

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

[...]
\edef\filename{/tmp/\x}
\unless\ifeof\reader
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\filename}%
[...]
koppor
  • 3,252
6

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}
\newread\reader

\begin{document}
\def\stripend#1 \stop{\unexpanded{#1}}
\openin\reader=\jobname.list\relax
\loop
    \read\reader to \x
    \unless\ifeof\reader
    \edef\x{%
      \unexpanded{%
        \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]%
       }%
       {\expandafter\stripend\x \stop}%
     }%
     \x
\repeat
\closein\reader
\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • @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
3

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}
\newread\reader

\begin{document}

\openin\reader=\jobname.list\relax
\begingroup\endlinechar=-1
  \loop
    \read\reader to \x
    \unless\ifeof\reader
    \edef\x{../Images/\x}
    \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{\x}
\repeat
\endgroup
\closein\reader
\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 }
 }
\ior_new:N \g__garbcoll_read_ior
\cs_new_protected:Npn \garbcoll_include:nnn #1 #2 #3
 {
  \ior_open:Nn \g__garbcoll_read_ior { #3 }
  \ior_map_inline:Nn \g__garbcoll_read_ior
   {
    \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}
egreg
  • 1,121,712