1

The following question stinks a bit of crazed micro-optimization. But I had written it, and I was a bit curious, so I thought I might as well post it.

The following loop iterates over inserting some files:

\foreach \n in {1,...,3}
{\includefile{\n}}

But I'd like to insert some blank pages in between those files. So, something like this:

\includefile{1}
\includefile{2}
\addblankpage
\includefile{3}

The question is - can this be done while still preserving a loop structure? Something involving a double loop like in Parallel loop [python-like zip] in Tikz might work, but a graceful way of doing so it not obvious.

See a complete MWE below.

\documentclass[12pt]{article}
\usepackage{filecontents}
\usepackage{shellesc}
\usepackage{tikz}
\usepackage{pdfpages}
\usepackage{listofitems}
\newcommand{\addblankpage}{\clearpage \phantom{} \clearpage}
\begin{document}

\begin{filecontents*}{foo/bar.tex}
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}  % This will be output to foo/bar
  \node [font=\bfseries\Large, align=center] at (10, -10){TEST};(10,10);
\end{tikzpicture}
\end{document}
\end{filecontents*}
\ShellEscape{cd foo; pdflatex -shell-escape bar.tex}

\readlist*\Filenames{
  foo/bar.pdf,
  foo/bar.pdf,
  foo/bar.pdf
}    
\newcommand{\includefile}[1]{
\includepdf[pagecommand=
{\begin{tikzpicture}[remember picture, overlay]
  \node [font=\bfseries\Large, align=center] at (5, -5){WRITE ON TOP};
  \end{tikzpicture}}
,pages=1-]{\Filenames[#1]}
}

% Can this be done
\includefile{1}
\includefile{2}
\addblankpage
\includefile{3}

% using a loop?
\foreach \n in {1,...,3}
{\includefile{\n}}

\end{document}
Faheem Mitha
  • 7,778

1 Answers1

1

It seems that adding an array containing \addblankpage's, and then looping over that simultaneously, works. Like this:

\readlist*\Blankfiles{
  ,
  \addblankpage,
}
And then changing the loop to

\foreach \n in {1,...,3}
{\includefile{\n}\Blankfiles[\n]}

A corresponding MWE follows. Comments welcome.

\documentclass[12pt]{article}
\usepackage{filecontents}
\usepackage{shellesc}
\usepackage{tikz}
\usepackage{pdfpages}
\usepackage{listofitems}
\newcommand{\addblankpage}{\clearpage \phantom{} \clearpage}
\begin{document}

\begin{filecontents*}{foo/bar.tex}
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}  % This will be output to foo/bar
\node [font=\bfseries\Large, align=center] at (10, -10){TEST};
\end{tikzpicture}
\end{document}
\end{filecontents*}
\ShellEscape{cd foo; pdflatex -shell-escape bar.tex}

\readlist*\Filenames{
  foo/bar.pdf,
  foo/bar.pdf,
  foo/bar.pdf
}

\readlist*\Blankfile{
  ,
  \addblankpage,
}  

\newcommand{\includefile}[1]{
\includepdf[pagecommand=
{\begin{tikzpicture}[remember picture, overlay]
 \node [font=\bfseries\Large, align=center] at (5, -5){WRITE ON TOP};
 \end{tikzpicture}}
 ,pages=1-]{\Filenames[#1]}
}

\includefile{1}
\includefile{2}
\addblankpage
\includefile{3}

\foreach \n in {1,...,3}
{\includefile{\n}\Blankfile[\n]}

\end{document}
Faheem Mitha
  • 7,778