36

I have this command

\newcommand{\pdfappendix}[1]{
    \includegraphics[scale=0.6]{pdf/#1.pdf}
}

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):

\newcommand{\pdfappendix}[2]{
    for index=1 to #2
    {
       \includegraphics[scale=0.6]{pdf/#1index.pdf}
    }
}

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:

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

Is it possible to do that?

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

2
  • 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

38
\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}

\def\pdfappendix#1#2{%
  \foreach \index in {1, ..., #2} {%
    \includegraphics[scale=0.6]{#1\index.pdf}\par%
  }}

\begin{document}
  \pdfappendix{pdf/test}{4}
\end{document}

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
  • 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
21

For the sake of completeness, in Luatex:

\newcommand{\pdfappendix}[2]{
    \directlua{
        for index=1,#2 do 
            tex.print("\includegraphics[scale=0.6]{pdf/#1"..index..".pdf}")
        end}
0
17

A solution without any extra packages

\documentclass{article}
\usepackage{graphicx}

% introduce a dummy counter, initially 0    
\newcount\tmp

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

\begin{document}

\pdfappendix{pdf/test}{5}

\newpage
\pdfappendix{pdf/test}{3}

\newpage
\pdfappendix{pdf/test}{2}

\newpage
\pdfappendix{pdf/test}{1}
\end{document}
5
  • 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
15
\usepackage{multido}
\newcommand\pdfappendix[2]{%
  \multido{\I=1+1}{#2}{%
    \includegraphics[scale=0.6]{pdf/#1\I.pdf}}}
1
  • 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
8

Just to do it differently, by recursion:

\newcommand\pdfappendix[2]{%
  %% initialize the "container" macro
  \def\pdfappendixcommand{}
  %% store the common prefix
  \def\pdfappendixcommon{#1}%
  %% start the recursion
  \dopdfappendix{1}{#2}}
\newcommand\dopdfappendix[2]{%
  \ifnum#1>#2
    %% terminate the recursion
    \expandafter\pdfappendixcommand
  \else
    %% append to \pdfappendixcommand
    \edef\pdfappendixcommand{%
      \unexpanded\expandafter{\pdfappendixcommand}\space
      \noexpand\includegraphics[scale=0.6]{\pdfappendixcommon#1.pdf}}%
    %% call \dopdfappendix{#1+1}{#2} (but computing "#1+1" and expanding \fi)
    \expandafter\dopdfappendix\expandafter
      {\number\numexpr#1+1\expandafter}% First argument to \dopdfappendix
      \expandafter{\number#2\expandafter}% Second argument to \dopdfappendix
  \fi}

Then

\pdfappendix{pdf/MyPDF}{5}

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):

\dopdfappendix{1}{5}
\dopdfappendix{2}{5}
\dopdfappendix{3}{5}
\dopdfappendix{4}{5}
\dopdfappendix{5}{5}
\dopdfappendix{6}{5}

and the last one will execute

\pdfappendixcommand

instead of continuing the recursion.

2
  • +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
5

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

% Eg, \pdfappendix{MyPDF}{4}
\makeatletter
\newcommand*\pdfappendix{\begingroup\def\x{}\@tempcnta\z@\dopdfappendix}
\newcommand*\dopdfappendix[2]{%
  \advance\@tempcnta\@ne
  \ifnum\@tempcnta>#2\relax
    \expandafter\@firstoftwo
  \else
    \edef\x{%
      \unexpanded\expandafter{\x}%
      \noexpand\includegraphics[scale=0.6]{#1\the\@tempcnta.pdf}%
    }%
    \expandafter\@secondoftwo
  \fi
  {\expandafter\endgroup\x}{\dopdfappendix{#1}{#2}}%
}
\makeatother

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

\makeatletter
\newcount\mycount
\newcommand*\pdfappendix{\mycount\z@\dopdfappendix}
\newcommand*\dopdfappendix[2]{%
  \advance\mycount\@ne
  \ifnum\mycount>#2\relax
    \expandafter\@gobble
  \else
    \edef\x{\noexpand\includegraphics[scale=0.6]{#1\the\mycount.pdf}}%
    \expandafter\@iden
  \fi
  {\x\dopdfappendix{#1}{#2}}% 
}
\makeatother
1
  • +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
0

With functional package, you can write this:

\documentclass{article}

\usepackage{functional}

\newcommand{\pdfappendix}[2]{%
  \IntStepInline{1}{1}{#2}{%
    \includegraphics[scale=0.6]{pdf/#1##1.pdf}%
  }%
}

\begin{document}

\pdfappendix{MyPDF}{4}

\end{document}

You must log in to answer this question.

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