# Inserting every 'n' page of several pdf files

I have 3 pdf files (a.pdf, b.pdf and c.pdf) with 50 images per file. I need to insert every page of each pdf one by one (3 in a row). Not all pages from first pdf and all from another, but one page from a.pdf, one from b.pdf and c.pdf

Desired output would be to have one document with pages: 1a, 1b, 1c, 2a, 2b, 2c, 3a, 3b, 3c....50a, 50b, 50c

MWE

\documentclass[a4paper]{article}
\usepackage{pdfpages}

\begin{document}

\includepdfmerge[nup=1x3]
{a.pdf, 1,2,3,
b.pdf, 1,2,3,
c.pdf, 1,2,3}

\end{document}

• I think it will be easier to simply write a script to generate the 50 simple \includepdfmerge lines. – daleif Aug 11 '15 at 10:07
• good point but how to insert a pdf in each position of nup=1x3 ? Do I need to create a table with \includepdf for 150 images? – Michal Aug 11 '15 at 10:12
• Good question. Another solution, would be to first merge the three documents in the right order so the new doc has a1,b1,c1,a2,b2,c2, etc. And then run the nup on that file. – daleif Aug 11 '15 at 10:35
• See my longer comment below – daleif Aug 11 '15 at 10:38
• Maybe it is better to solve this with a tool like pdftk? – Wouter Donders Aug 11 '15 at 10:54

Just a comment, as it is a bit too long for a normal comment.

Does

\includepdfmerge[nup=1x3]{a.pdf, 1,b.pdf, 1, c.pdf, 1}
\includepdfmerge[nup=1x3]{a.pdf, 2,b.pdf, 2, c.pdf, 2}
...


work? Because that could be done via a simple latex loop

This seems to work

\documentclass[a4paper]{memoir}
\usepackage{pdfpages,pgffor}
\begin{document}

\foreach\n in{1,...,5}{
\includepdfmerge[nup=1x3]{a.pdf,\n,b.pdf,\n,c.pdf,\n}
}
\end{document}

• Do you have an idea to have similar output but with pdf files with different number of pages? For example, a contains 4 pages and b contains only 3. So I'd like to do the loop from 1 to 4. But I got an error since b does not contain page 4. – Sigur Aug 21 '15 at 9:20
• I do not know. Try asking another question and ask for a test to see if a specific page in a PDF exists. – daleif Aug 21 '15 at 9:35

This extends the answer of daleif to add the automatic retrieval of the number of pages of the included document, when pdfTeX in PDF mode is used. The command \pdflastximagepages returns the number of pages of the previously included PDF document right before calling \pdflastximagepages:

\documentclass[a4paper]{memoir}
\usepackage{pdfpages,pgffor}
\begin{document}

\sbox0{\includegraphics{a.pdf}}
\edef\PagesA{\number\pdflastximagepages}

\foreach\n in{1,...,\PagesA}{
\includepdfmerge[nup=1x3]{a.pdf,\n,b.pdf,\n,c.pdf,\n}
}
\end{document}

• Nice! What do you mean by of the latest included? In your example, you choose a to compute the number of pages. But the ideal should be to use the file which has less pages? – Sigur Aug 21 '15 at 10:01
• @Sigur I have changed it to "previously included", maybe it is clearer now. – Heiko Oberdiek Aug 21 '15 at 10:04
• So, it means the one inside the \sbox0? I think so. – Sigur Aug 21 '15 at 10:07
• @Sigur Yes, the purpose of \sbox0 is just to throw away the included graphics, because at this time only the number of pages is interesting. – Heiko Oberdiek Aug 21 '15 at 10:12
• @Sigur As I have written, it's a primitive of pdfTeX. Meta data are not available. Try LuaTeX. – Heiko Oberdiek Aug 21 '15 at 13:56

Here is an extension of the solution proposed by @Heiko Oberdiek.

It works for two files (can be adapted for more) even if they have different number of pages.

\documentclass{report}
\usepackage[landscape,margin=0pt,a3paper]{geometry}
\usepackage{pdfpages,pgffor}

\begin{document}
\pagestyle{empty}
\sbox0{\includegraphics{new.pdf}}
\edef\PagesA{\number\pdflastximagepages}
\sbox1{\includegraphics{old.pdf}}
\edef\PagesB{\number\pdflastximagepages}
\newcounter{Min}
\newcounter{Max}
\setcounter{Min}{\minof{\PagesA}{\PagesB}}
\setcounter{Max}{\maxof{\PagesA}{\PagesB}}

\foreach\n in{1,...,\theMin}{
\includepdfmerge[nup=2x1]{new.pdf,\n,old.pdf,\n}
}
\ifnum\theMin=\PagesA