[Tex/LaTex] “For loop” in newcommand

loopsmacros

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.

Best Answer

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

Related Question