# [Tex/LaTex] Insert pdf pages side by side

foreachpdfpages

I have two versions (A4 size) of a document: new.pdf and old.pdf.

I'd like to share with co-authors a pdf with pages side by side, so should be easy to compare the changes in the output.

Here there is a partial solution which works well only if the files have the same number of pages.

An extension of that partial solution is proposed and makes use of \pdflastximagepages to compute the number of pages of a previous inserted pdf file. So this number can be used for the loop.

Since my pdf files could have different numbers of pages, the solutions cited above don't work.

So, my idea is:

1. compute the number of pages for both files.

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

\sbox1{\includegraphics{old.pdf}}
\edef\PagesB{\number\pdflastximagepages}


2. define the numbers \Min=min(\PagesA,\PagesB) and \Max=max(\PagesA,\PagesB)

3. from 1 to \Min insert pages from both files using

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


4. finally, test if \Min=\PagesA or \Min=\PagesB.

4.1 if \Min=\PagesA then use

\foreach\n in{\Min+1,...,\Max}{
\includepdfmerge[nup=2x1]{old.pdf,\n} % old is on the right hand side
}


4.2 if \Min=\PagesB then use

\foreach\n in{\Min+1,...,\Max}{
\includepdfmerge[nup=2x1]{new.pdf,\n} % new is on the left hand side
}


My question is: how to implement (4)?

M(W)E

\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}
%\edef\Min{}
%\edef\Max{}

\foreach\n in{1,...,\Min}{
\includepdfmerge[nup=2x1]{new.pdf,\n,old.pdf,\n}
}
\foreach\n in{\Min+1,...,\Max}{
%% test here to decide if new or old should be used
\includepdfmerge[nup=2x1]{??.pdf,\n}
}
\end{document}


A similar solution than the one mentioned before in the comments. The differences are:

• Only computes the maximum of the page numbers. (The minimum is not used.)
• Tests in every iteration step (in the for loop over the pages) if there are two ore only one page to be included.
• Takes into account if single pages should be placed on the left or the right.

Here the MWE:

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

\begin{document}
\pagestyle{empty}
\sbox0{\includegraphics{new.pdf}}
\edef\PagesNew{\number\pdflastximagepages}
\sbox1{\includegraphics{old.pdf}}
\edef\PagesOld{\number\pdflastximagepages}
\newcounter{Max}
\setcounter{Max}{\maxof{\PagesNew}{\PagesOld}}

\foreach\n in{1,...,\theMax}{
\ifnum\n>\PagesNew
\includepdfmerge[nup=2x1,openright]{old.pdf,\n}
\else
\ifnum\n>\PagesOld
\includepdfmerge[nup=2x1]{new.pdf,\n}
\else
\includepdfmerge[nup=2x1]{new.pdf,\n, old.pdf, \n}
\fi
\fi
}
\end{document}