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.

  • 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
    Commented Jun 10, 2011 at 8:20
  • @topskip, I use exactly this method and it works.
    – math
    Commented Apr 13, 2014 at 18:40

7 Answers 7


  \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.

  • 1
    Is there any way to see (e.g. for copy/paste purposes) the generated latex code?
    – Vairis
    Commented May 7, 2012 at 16:30

For the sake of completeness, in Luatex:

        for index=1,#2 do 

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





  • 1
    To avoid the loop, you can try to call your command recursively until #1 equals #2... Commented Jan 3, 2013 at 1:29
  • Good! You can use \aftergroup to avoid too deep recursion... ;-) Commented Jan 3, 2013 at 1:41
  • @PaulGaborit I'll do some reading- haven't used \aftergroup before- thanks for the idea :)
    – cmhughes
    Commented Jan 3, 2013 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
    Commented Jan 3, 2013 at 18:45
  • @AhmedMusa thanks for pointing that out... another problem with the recursive definition was that it wouldn't work as expected if called more than once in a document- have deleted
    – cmhughes
    Commented Jan 3, 2013 at 23:20
  • 12
    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
    Commented Jun 9, 2011 at 18:17

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.

  • +1 so this resolves the tail recursion that @Ahmed mentioned in his comment to my answer (which my attempt didn't manage)
    – cmhughes
    Commented Jan 4, 2013 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
    Commented Jan 4, 2013 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:

  • +1 (when I re-charge my votes) thanks for posting, I'll study it as an exercise in expansion :)
    – cmhughes
    Commented Jan 4, 2013 at 23:39

With functional package, you can write this:







You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .