TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this command


It works well when I have 1 page pdf but is not including the other pages. Now I decided to change the command to take a second argument which is the number of pages and will repeat \includegraphics that number of times.

I want something similar to (the following is not latex code):

    for index=1 to #2

Example: MyPDF.pdf has 4 pages. I will split it in 4 pdfs with 1 page each: MyPDF1.pdf, MyPDF2.pdf, MyPDF3.pdf, MyPDF4.pdf. The output of the command will be:

Then I will write the command: \pdfappendix{MyPDF}{4}

And it will output:


Is it possible to do that?

PS: I don't want to use \includepdf because I have some problems with section headers.

share|improve this question
What about \includegraphics[scale=0.6,page=2]{...}? I've heard that you can select the page directly in the options. Not 100% sure about this. – topskip Jun 10 '11 at 8:20
@topskip, I use exactly this method and it works. – math Apr 13 '14 at 18:40
up vote 26 down vote accepted

  \foreach \index in {1, ..., #2} {%


The above code will the thing you want. However I haven't tested it extensively. I just tested this minimal example and it works.

share|improve this answer
Thank you very much for your fast and correct answer! – Victor Hurdugaci Jun 9 '11 at 18:05
Is there any way to see (e.g. for copy/paste purposes) the generated latex code? – Vairis May 7 '12 at 16:30

For the sake of completeness, in Luatex:

        for index=1,#2 do 
share|improve this answer
+1 for the LuaTeX solution – topskip Jun 10 '11 at 8:18
share|improve this answer
There are quite a lot of choices. (La)TeX's \loop \repeat, forloop's \forLoop, ifthen's \whiledo, etoolbox's \whileboolexpr, expl3's \prg_stepwise_function, and pgffor and multido. – Leo Liu Jun 9 '11 at 18:17

A solution without any extra packages


% introduce a dummy counter, initially 0    

\newcommand{\pdfappendix}[2]{% need this to prevent extra vertical space
    % #1: image path and core part of name
    % #2: maximum number
        % increment dummy counter
        \advance\tmp by 1
        % include the image
        % repeat the loop provided the counter is within specified bound





share|improve this answer
To avoid the loop, you can try to call your command recursively until #1 equals #2... – Paul Gaborit Jan 3 '13 at 1:29
@PaulGaborit got it :) – cmhughes Jan 3 '13 at 1:39
Good! You can use \aftergroup to avoid too deep recursion... ;-) – Paul Gaborit Jan 3 '13 at 1:41
@PaulGaborit I'll do some reading- haven't used \aftergroup before- thanks for the idea :) – cmhughes Jan 3 '13 at 2:00
The second solution doesn't resolve tail recursion and \includegraphics is heavy enough to consume resources. "Recursive macros tend to clutter up TEX’s memory if too many incarnations of such a macro are active at the same time. However, TEX is able to prevent this in one frequently occurring case of recursion: tail recursion." - TeX by Topic – Ahmed Musa Jan 3 '13 at 18:45

Just to do it differently, by recursion:

  %% initialize the "container" macro
  %% store the common prefix
  %% start the recursion
    %% terminate the recursion
    %% append to \pdfappendixcommand
    %% call \dopdfappendix{#1+1}{#2} (but computing "#1+1" and expanding \fi)
      {\number\numexpr#1+1\expandafter}% First argument to \dopdfappendix
      \expandafter{\number#2\expandafter}% Second argument to \dopdfappendix



will do the required thing. The trick is to build recursively a macro that will contain all the instructions. For instance, \pdfappendix{pdf/MyPDF}{2} will build \pdfappendixcommand expanding to

 \includegraphics[scale=0.6]{pdf/MyPDF1.pdf} \includegraphics[scale=0.6]{pdf/MyPDF2.pdf}

All the \ifnum are evaluated at nesting level 1, because the call to \dopdfappendix is made after the \fi has been expanded.

What happens is that the following commands are called in turn (second argument 5):


and the last one will execute


instead of continuing the recursion.

share|improve this answer
+1 so this resolves the tail recursion that @Ahmed mentioned in his comment to my answer (which my attempt didn't manage) – cmhughes Jan 4 '13 at 2:21
@cmhughes I added it just because you removed your attempt. Not that I advocate using this method over the simple \loop; just another technique made possible by e-TeX extensions. – egreg Jan 4 '13 at 10:45

@cmhughes: Your solution only required some modification, not deletion.

% Eg, \pdfappendix{MyPDF}{4}

With a dedicated counter we don't need an accretion of \includegraphics. Here is it:

share|improve this answer
+1 (when I re-charge my votes) thanks for posting, I'll study it as an exercise in expansion :) – cmhughes Jan 4 '13 at 23:39

Your Answer


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.