Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

\newread\myfile
\newcommand*\readfile{%
\immediate\openin\myfile=titles.txt\relax
\loop\unless\ifeof\myfile
    \read\myfile to\mybtitle
    \read\myfile to\mycategory
    \read\myfile to\myfname

    \ifeof\myfile\else
       \index{\mycategory}
       \includepdf[addtotoc={1, section, 1 , \mybtitle, \numexos },fitpaper=true,pagecommand={}]{\myfname.pdf}
       \addtocounter{numexos}{1}
    \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

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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

\newread\myfile
\newcommand*\readfile{%
  \begingroup\endlinechar=-1
  \immediate\openin\myfile=titles.txt\relax
  \loop\unless\ifeof\myfile
    \read\myfile to\mybtitle
    \ifx\mybtitle\empty\else
      \read\myfile to\mycategory
      \read\myfile to\myfname
      \index{\mycategory}
      \includepdf[addtotoc={1, section, 1 , \mybtitle, \thenumexos },fitpaper=true,pagecommand={}]{\myfname.pdf}
      \addtocounter{numexos}{1}
    \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.

share|improve this answer
    
Thank you so much for the solution and the suggestion of the inner test ! –  OlivierM Jul 11 '12 at 14:45
add comment

You can remove the space like so:

\loop\unless\ifeof\myfile
    \read\myfile to\mybtitle
    \read\myfile to\mycategory
    \read\myfile to\myfname

\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...
l.26 \readfile

? 
> \myfname=macro:
->filename1.
share|improve this answer
    
Thank you for your reply and the proposed solution. I find yours simpler in my case. –  OlivierM Jul 11 '12 at 14:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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