[Tex/LaTex] How to include multiple pdf pages using \foreach as tikzpicture

foreachpdfpagestikz-pgf

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

Best Answer

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}%
}
\AddEverypageHook{\stepcounter{mypagebackground}}
\backgroundsetup{%
  contents={%
    \AddEverypageHook{% adapted from pp. 6-7 of background manual
      \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.

rainbow Kant

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}}
  Some text on this page.
  \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}

Different looping effects

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