# “For loop” in newcommand

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.

-
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

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

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

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

-
+1 for the LuaTeX solution –  topskip Jun 10 '11 at 8:18
\usepackage{multido}
\newcommand\pdfappendix[2]{%
\multido{\I=1+1}{#2}{%
\includegraphics[scale=0.6]{pdf/#1\I.pdf}}}

-
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

\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
% 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}

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

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

-
+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}
\makeatletter
\newcommand*\pdfappendix{\begingroup\def\x{}\@tempcnta\z@\dopdfappendix}
\newcommand*\dopdfappendix[2]{%
\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]{%