# How to include multiple pdf pages using \foreach as tikzpicture?

I know how to include pdf pages using package pdfpages and do it all the time, but always had problem with getting the page centered and scaling.

I am now trying to learn how to use tikzpicture to do the same, since I'd like to take advantage of overlay option, as in

\begin{tikzpicture}[remember picture,overlay]


Which I found, on a single page, works well. The problem is that I have to use a loop, since when using \includegraphics[page=??]{file2.pdf} the page number has to be a single page. I can't use something as \includepdf[page={2-}] for example, to tell it to read pages 2 to the end of the pdf file.

Here is a MWE to read one page in tickz which works fine:

\documentclass[11pt]{report}
\usepackage{amsmath,mathtools}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt] at (current page.center)
{\includegraphics[page=1]{file2.pdf}};
\end{tikzpicture}
\end{document}


Now, I wanted to do the same, but not just for page 1, but for all the pages. This is what I tried

\documentclass[11pt]{report}
\usepackage{amsmath,mathtools}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}
\pdfximage{file2.pdf}  %find how many pages
There are \the\pdflastximagepages\ pages in the file. Now
we will include them:

\begin{tikzpicture}[remember picture,overlay]
\foreach \index in {1, ... ,\the\pdflastximagepages}
{
\node[inner sep=0pt] at (current page.center)
{\includegraphics[page=\index]{file2.pdf}};
}
\end{tikzpicture}
\end{document}


But this gives error Paragraph ended before \pgffor@dots@stripcontext was complete.

I also tried to put the loop outside, and the tikzpicture inside

\documentclass[11pt]{report}
\usepackage{amsmath,mathtools}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}
\pdfximage{file2.pdf}

\foreach \index in {1, ... ,\the\pdflastximagepages}
{
\begin{tikzpicture}[remember picture,overlay]
\node[inner sep=0pt] at (current page.center)
{\includegraphics[page=\index]{file2.pdf}};
\end{tikzpicture}
}
\end{document}


Same problem. Clearly I am lost and do not know what I am doing so I am asking experts help in getting the syntax right.

references:

"For loop" in newcommand

Including a PDF page in latex with pdfpages without scaling

I would suggest using the background package and a little trickery. The following defines a new command \installbackgrounds[]{}. The first argument is optional and sets the total number of pages in the file of backgrounds. The second, mandatory argument specifies the file. If no total is specified, the number defaults to 1.

\documentclass[a4paper]{report}
\usepackage{graphicx}
\usepackage{background, etoolbox, kantlipsum}
\newcounter{mypagebackground}
\setcounter{mypagebackground}{0}
\newcounter{mypagebackgroundpages}
\setcounter{mypagebackgroundpages}{0}
\newcommand*\myinstallbackground[1]{\relax}
\newcommand*\installbackgrounds[2][1]{%
\setcounter{mypagebackground}{0}%
\setcounter{mypagebackgroundpages}{#1}%
\gdef\mybackgroundfile{#2}%
}
\backgroundsetup{%
contents={%
\ifnumless{\value{mypagebackgroundpages}}{\value{mypagebackground}}{}{%
\begin{tikzpicture}[remember picture, overlay]
\node  at (current page.center) {\includegraphics[page=\themypagebackground]{\mybackgroundfile}};
\end{tikzpicture}}%
}%
}%
}
\begin{document}
\kant[1-5]
\installbackgrounds[7]{mypages.pdf}
\kant[1-30]
\installbackgrounds{example-image-a.pdf}
\kant[1-5]
\end{document}


mypages.pdf is a PDF consisting of 7 pages, one for each colour of the rainbow.

# EDIT: Looping Effects

This a response to the discussion in comments. It is not an answer. It is intended to help understand what is going on. Run this code and compare the different effects.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {10,20,...,100}
\node [minimum width=\i mm, minimum height=\i mm, draw=red!\i] at (current page.center) {};
\end{tikzpicture}
\clearpage
\foreach \i in {10,20,...,100}{%
\begin{tikzpicture}[remember picture, overlay]
\node [minimum width=\i mm, minimum height=\i mm, draw=blue!\i] at (current page.center) {};
\end{tikzpicture}}
\clearpage
\foreach \i in {10,20,...,100}{%
\begin{tikzpicture}
\node [minimum width=\i mm, minimum height=\i mm, draw=green!\i] at (current page.center) {};
\end{tikzpicture}}
\foreach \i in {10,20,...,100}{%
\begin{tikzpicture}
\node [minimum width=\i mm, minimum height=\i mm, draw=orange!\i] {};
\end{tikzpicture}
Some text on page \thepage.
\clearpage}
\end{document}


Notice that the first orange square is on the page with the green squares because there is no \clearpage after the final green square (which is well off the page).

• I tried this \begin{document} test \installbackgrounds[17]{file2.pdf}\end{document} but the pdf file is not loaded? it looks like your solution for putting background on existing pages? But I need to actually include the external pdf pages in the document. Not as background to existing pages, but as new pages. thanks. – Nasser Dec 26 '14 at 2:04
• @Nasser Then I don't understand your question. You are trying to use the overlay option which, well, overlays things rather than putting each thing in a separate space. Why not just use \includepdf[options]{} if you want to include the pages of the PDF as regular pages? – cfr Dec 26 '14 at 2:15
• I did not know that overlay requires an existing page, I thought it will create new pages and load the pdf pages on top from the file. But now I understand that a page have to exist first. I still like to know how to use the LOOP to load pages as pictures. But without the use of overlay confusion I have. May be I will make separate question on this later for that to make it more clear. – Nasser Dec 26 '14 at 2:31
• @Nasser You might want to ask a new question but why isn't \includepdf{} what you want? – cfr Dec 26 '14 at 3:07
• I am using includepdf{} but thought I can get more control by using tickzpicture. But mainly I wanted to know why the loop did not work using foreach. I'll play more with it to see. I am newbie in programing in Latex. – Nasser Dec 26 '14 at 3:23

I found the cause of the error.

The error I was getting Paragraph ended before \pgffor@dots@stripcontext was complete. was due to having a space between the comma and the 3 dots.

Now this works (I removed the overlay option as not needed)

\documentclass[11pt]{report}
\usepackage{amsmath,mathtools}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{tikz}
\usepackage{pgffor}

\begin{document}
\pdfximage{file2.pdf}
\foreach \index in {1,...,\the\pdflastximagepages}
{
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] at (current page.center)
{\includegraphics[page=\index]{file2.pdf}};
\end{tikzpicture}
}
\end{document}


So the problem was writing

 {1, ... ,\the\pdflastximagepages}


 {1,...,\the\pdflastximagepages}