Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Counters and loops

10 views
Skip to first unread message

Michaël Monerau

unread,
Aug 4, 2005, 8:35:16 AM8/4/05
to
Hello,

I've made photos of the pages a book of mine to put it on my computer.
I'd like to get it on a single PDF file (that is, I took a photo of the
left page, then one of the right, then left, right...).

As there are many pages (more than 300), I'd like to automatize the
process of putting the photos in order. I'm quite a newbie to LaTeX so
it's not really easy to do that stuff for me :p So I have a few
questions that should look rather straightforward for you, LaTeX Gurus ;-)

The JPEG files of the pages are numbered "Right_001.jpg",
"Left_001.jpg", ... Here's what I did :

\newcounter{nbfile}
\setcounter{nbfile}{1}

then, I use \includegraphics from the graphicx package :

\includegraphics[width=15cm]{Left_\thenbfile.jpg}
\includegraphics[width=15cm]{Right_\thenbfile.jpg}

It works fine, except that it outputs "Left_1.jpg", instead of
"Left_001.jpg". How could I get the leading two zeros ?

I tried to use the fmtcount package, but it outputs errors when I use :

\includegraphics[width=15cm]{Left_\decimal{nbfile}.jpg}

so I'm quite stuck there...

And then, I don't know how to make a "for" loop in LaTeX. Could someone
help me to figure it out ? It'd prevent me from doing endless Copy/paste
150 times ;-)

Thanks in advance !
--
Michaël Monerau
mmon...@gmail.com

Morten Høgholm

unread,
Aug 4, 2005, 8:52:58 AM8/4/05
to
On Thu, 04 Aug 2005 14:35:16 +0200, Michaël Monerau <mmon...@gmail.com>
wrote:


> It works fine, except that it outputs "Left_1.jpg", instead of
> "Left_001.jpg". How could I get the leading two zeros ?
>
> I tried to use the fmtcount package, but it outputs errors when I use :
>
> \includegraphics[width=15cm]{Left_\decimal{nbfile}.jpg}

Sounds like the \decimal isn't expandable.

> so I'm quite stuck there...
>
> And then, I don't know how to make a "for" loop in LaTeX. Could someone
> help me to figure it out ? It'd prevent me from doing endless Copy/paste
> 150 times ;-)

Very untested:

\makeatletter
\newcommand*\threedigits[1]{%
\ifnum#1<100 0%
\ifnum#1<10 0\fi
\fi
\number#1}
\newcommand*\insertimages[1]{%
% all images are supposed to be 15cm wide.
\setkeys{Gin}{width=15cm}%
\@tempcnta=0
\@whilenum\@tempcnta<#1 \do{%
\advance\@tempcnta by 1
\includegraphics{Left_\threedigits{\@tempcnta}.jpg}%
% perhaps some separator command here...
\includegraphics{Right_\threedigits{\@tempcnta}.jpg}%
}
}
\makeatother

\insertimages{150} should do the trick. This of course assumes you have an
even number of pages.
--
Morten

Lukas Ruf

unread,
Aug 4, 2005, 10:08:42 AM8/4/05
to
> Michaėl Monerau [Thu, 04 Aug 2005 14:35:16 +0200]:

>
> And then, I don't know how to make a "for" loop in LaTeX. Could
> someone help me to figure it out ? It'd prevent me from doing
> endless Copy/paste 150 times ;-)
>

why not use bash shell scripting (or similar?):

#### BEGIN OF *UNTESTED SCRIPT*
#/bin/sh

#rename your files to make sense:
# results in 001_Left.jpg 001_Right.jpg

for f in Left_*.jpg Right_*.jpg ; do
mv $f `echo $f \
| sed -e 's/\([LR][efigh]t\)_\([0-9][0-9][0-9]\).jpg/\2_\1.jpg/'`
done

#convert your files to eps

for f in *_*.jpg ; do
cat $f | jpegtopnm | pnmtops | ps2eps \
> `echo $f | sed -e 's/\.jpg/.eps/'`
done

#create the latex defintions in pics.tex

rm -f pics.tex
touch pics.tex
for f in *.eps ; do
echo \
"\includegraphics[width=15cm]{`echo $f | sed -e 's/\.eps//'`}"\
>> pics.tex
done
#### END OF *UNTESTED SCRIPT*

then include the pics.tex in your main document:

.....
\input{pics}
.....

Please note:

- I have not tested the script

- I assume you make use of a decent Linux installation.

- I have not tested the script

- Make a backup of your data before trying the script!

- I have not tested the script

But anyway, I would try it with something like that.

HTH

wbr,
Lukas
--
Lukas Ruf <http://www.lpr.ch> | Ad Personam
rbacs <http://wiki.lpr.ch> | Restaurants, Bars and Clubs
Raw IP <http://www.rawip.org> | Low Level Network Programming
Style <http://email.rawip.org> | How to write emails

Michaël Monerau

unread,
Aug 5, 2005, 5:00:55 AM8/5/05
to
Morten Høgholm a écrit :

> \makeatletter
> \newcommand*\threedigits[1]{%
> \ifnum#1<100 0%
> \ifnum#1<10 0\fi
> \fi
> \number#1}
> \newcommand*\insertimages[1]{%
> % all images are supposed to be 15cm wide.
> \setkeys{Gin}{width=15cm}%
> \@tempcnta=0
> \@whilenum\@tempcnta<#1 \do{%
> \advance\@tempcnta by 1
> \includegraphics{Left_\threedigits{\@tempcnta}.jpg}%
> % perhaps some separator command here...
> \includegraphics{Right_\threedigits{\@tempcnta}.jpg}%
> }
> }
> \makeatother
>
> \insertimages{150} should do the trick. This of course assumes you have
> an even number of pages.

Thanks a lot, it works perfectly fine :-)

But the loop process is not very handy... What a mess ! Is there any
documentation about that, or I'll just have to play with it to get
accustomed to it ?


--
Michaël Monerau
mmon...@gmail.com

Michaël Monerau

unread,
Aug 5, 2005, 5:02:55 AM8/5/05
to
Lukas Ruf a écrit :
>>Michaël Monerau [Thu, 04 Aug 2005 14:35:16 +0200]:

>>
>> And then, I don't know how to make a "for" loop in LaTeX. Could
>> someone help me to figure it out ? It'd prevent me from doing
>> endless Copy/paste 150 times ;-)
>>
>
>
> why not use bash shell scripting (or similar?):

Yes, I could, but it is hard to maintain it after the file has been
output... So I'd prefer an other way to achieve it.

Thanks anyway, it would have been the solution if I woudn't have had a
LaTeX solution.

--
Michaël Monerau
mmon...@gmail.com

Morten Høgholm

unread,
Aug 5, 2005, 5:42:01 AM8/5/05
to
On Fri, 05 Aug 2005 11:00:55 +0200, Michaël Monerau <mmon...@gmail.com>
wrote:

> Thanks a lot, it works perfectly fine :-)

Good!

> But the loop process is not very handy... What a mess ! Is there any
> documentation about that, or I'll just have to play with it to get
> accustomed to it ?

That one is mostly for internal use. You might find a command like
\whiledo from the ifthen package handier.
--
Morten

0 new messages