# Include pdf documents from a list written in a text file

In a text file titles.txt, I have the title, category and filenames of all the documents I wish to insert in TeX file. I can properly insert the title and the category (used with \index) but unfortunately I can not use the filename since \includepdf added extra space that I can't remove. The structure of the text file is the following:

title1
category1
filename1
tilte2
category2
filename2
etc..

Here is the code:

\newcounter{numexos}
\setcounter{numexos}{1}

\immediate\openin\myfile=titles.txt\relax
\loop\unless\ifeof\myfile

\ifeof\myfile\else
\index{\mycategory}
\includepdf[addtotoc={1, section, 1 , \mybtitle, \numexos },fitpaper=true,pagecommand={}]{\myfname.pdf}
\fi
\repeat
\immediate\closein\myfile
}

Instead of using the filename stored in the TXT file I can use the counter if possible: I've tried but I failed for the same reasons : additionnal space in the filename: 01 .pdf

-

I'd get rid of the final spaces to begin with:

\begingroup\endlinechar=-1
\immediate\openin\myfile=titles.txt\relax
\loop\unless\ifeof\myfile
\ifx\mybtitle\empty\else
\index{\mycategory}
\includepdf[addtotoc={1, section, 1 , \mybtitle, \thenumexos },fitpaper=true,pagecommand={}]{\myfname.pdf}
\fi
\repeat
\endgroup
\immediate\closein\myfile
}

There is no problem in doing the reading in a group, because all operations you need are "global".

I've used a different inner test: if \myfile is empty I assume that either there is a blank line separating titles or that the file has ended.

-
Thank you so much for the solution and the suggestion of the inner test ! –  OlivierM Jul 11 '12 at 14:45

You can remove the space like so:

\loop\unless\ifeof\myfile

\show\myfname
\edef\myfname{\expandafter\zap@space\myfname \@empty}
\show\myfname

with the \show lines just for debugging of course:

Note how the space at the end is gone on the second \show

> \myfname=macro:
->filename1 .
\iterate ...myfile to\myfname \par \show \myfname
\edef \myfname {\expandaft...