# Change order of pages when using pdfpages, Part II

First: This is a follow-up question to the one here.

Consider the following example.

File: f1.tex

\documentclass[
12pt,
landscape,
danish
]{article}

\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[
a6paper,
margin = 0.8cm
]{geometry}
\usepackage{lmodern}
\usepackage[sc]{mathpazo}
\usepackage{multido}
\usepackage{microtype}

\newenvironment{navn}{%
\setlength{\topskip}{0pt}%
\centering
\vspace*{\fill}%
\nointerlineskip
\fontsize{40}{48}\selectfont
% \sbox0{Forside}%
% \showthe\ht0
}{%
\par
\vspace*{\fill}%
% \setlength{\showboxdepth}{\maxdimen}%
% \tracingonline=1 %
% \showlists
}

\DisableLigatures[f]{encoding=T1}

\pagestyle{empty}

\begin{document}

\multido{\i = 1+1}{18}{%
\begin{navn}
Front
\end{navn}
\newpage}

\end{document}


File: f2.tex

\documentclass[
12pt,
landscape,
danish
]{article}

\usepackage{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[
a6paper,
margin = 1.2cm
]{geometry}
\usepackage{lmodern}
\usepackage[sc]{mathpazo}
\usepackage{mathtools}
\usepackage{microtype}

\newenvironment{navn}{%
\setlength{\topskip}{0pt}%
\centering
\vspace*{\fill}%
\nointerlineskip
\LARGE
% \sbox0{XYZ}%
% \showthe\ht0
}{%
\par
\vspace{\fill}%
% \setlength{\showboxdepth}{\maxdimen}%
% \tracingonline=1 %
% \showlists
}

\DisableLigatures[f]{encoding=T1}

\pagestyle{empty}

\begin{document}

\begin{navn}
A\\[\baselineskip]
B
\end{navn}
\newpage

\begin{navn}
C\\[\baselineskip]
D
\end{navn}
\newpage

\end{document}


File: f3.tex

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\newcount\maxpages
\newcount\curpage
\newcount\untilpage
\makeatletter
\newif\if@pdfdingsunbalance
\newcount\@testmaxpages
\newcommand{\@incpdfdings}[3]{%
\includepdf[
nup = 3x6,
pages = {#1-#2},
frame = true
]{#3}}
\newcommand{\incpdfdings}[3][]{%
\ifx\relax#1\relax%
\pdfximage{#2}%
\maxpages=\pdflastximagepages%
\pdfximage{#3}%
\@testmaxpages=\pdflastximagepages%
\ifnum\maxpages<\@testmaxpages%
\@pdfdingsunbalancetrue%
\def\@pdfdingsmore{#3}%
\else\ifnum\@testmaxpages<\maxpages%
\@pdfdingsunbalancetrue%
\def\@pdfdingsmore{#2}%
\maxpages=\@testmaxpages%
\fi\fi%
\else%
\maxpages=#1%
\fi%
\curpage=0%
\loop\ifnum\maxpages>\curpage%
\untilpage=\numexpr\curpage+17\relax%
\ifnum\maxpages>\untilpage\else%
\untilpage=\maxpages%
\fi%
\@incpdfdings{\the\curpage}{\the\untilpage}{#2}%
\@incpdfdings{\the\curpage}{\the\untilpage}{#3}%
\repeat%
\if@pdfdingsunbalance%
\@incpdfdings{\the\maxpages}{}{\@pdfdingsmore}%
\fi%
}
\makeatother

\begin{document}

\incpdfdings%
{f1.pdf}%
{f2.pdf}


With the following example compiled, it produces three pages in f3.pdf; it puts 18 pages from f1.pdf on the third page in f3.pdf and then moves each page from the third page to the first page whenever a new page from f2.pdf is put on the second page in f3.pdf.

Question

How do I move 18 pages from f1.pdf to the front of the assembled document in f3.pdf right from the beginning, then 18 pages from f2.pdf, then another 18 pages from f1.pdf and so on?

That is, I would like to have the same as with pdfpages but with the pages in f3.pdf altered between the outputs from f1.pdf and f2.pdf.

I hope it makes sense.

• Why don't you just use \includepdf in that case? – cfr May 16 '17 at 1:41
• My code from the original question checked, whether both documents have the same amount of pages. Since originally your question was asking for alternating f1 and f2 on one page, I thought it was logically the correct behaviour to print the number of pages both have and then the rest of the file with more pages. I'll try some stuff... – Skillmon May 16 '17 at 19:18
• Also: In the current case, f2 has only 2 pages. What should happen in this case? 18 pages of f1 then 2 from f2 and then 18 from f1? If f1 has more than 36 pages should there be an empty page in f3 for the missing pages of f2? – Skillmon May 16 '17 at 19:22

This f3.tex should do what you want (I hope):

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\newcount\firstmaxpages
\newcount\secondmaxpages
\newcount\curpage
\newcount\untilpage
\makeatletter
\newif\if@pdfdingsSecondFin
\newcommand{\@incpdfdings}[4][]{%
\includepdf[%
nup = 3x6,
pages = {#2-#3},
#1
]{#4}}
\newcommand{\incpdfdings}[3][]{%
\ifx\relax#1\relax%
\pdfximage{#2}%
\firstmaxpages=\pdflastximagepages%
\pdfximage{#3}%
\secondmaxpages=\pdflastximagepages%
\ifnum\firstmaxpages<\secondmaxpages%
\@latex@error{incpdfdings: Second file has more pages than first}{}
\fi%
\else%
\firstmaxpages=#1%
\fi%
\curpage=0%
\loop\ifnum\firstmaxpages>\curpage%
\untilpage=\numexpr\curpage+17\relax%
\ifnum\untilpage>\firstmaxpages%
\untilpage=\firstmaxpages%
\fi%
\@incpdfdings[frame=true]{\the\curpage}{\the\untilpage}{#2}%
\ifnum\secondmaxpages>\untilpage\else%
\untilpage=\secondmaxpages%
\fi%
\if@pdfdingsSecondFin\else%
\@incpdfdings{\the\curpage}{\the\untilpage}{#3}%
\fi%
\ifnum\secondmaxpages=\untilpage%
\@pdfdingsSecondFintrue%
\fi%
\repeat%
}
\makeatother

\begin{document}
\incpdfdings%
{f1.pdf}%
{f2.pdf}
\end{document}


EDIT: For the 2x4 pages:

• Change in the definition of \@incpdfdings: nup = 3x6 to nup = 2x4

• In the definition of \incpdfdings: Change all (2) occurences of the number 17 to 7

EDIT2: I changed everything a bit, so you don't have to change anything for different options for nup as long as they follow the <num>x<num> scheme:

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\newcount\firstmaxpages
\newcount\secondmaxpages
\newcount\curpage
\newcount\untilpage
\makeatletter
\newif\if@pdfdingsSecondFin
\newcount\@pdfdingsPages
\newcommand{\@incpdfdings}[5][]{%
\includepdf[%
nup = #5,
pages = {#2-#3},
#1
]{#4}}
\newcommand{\@@incpdfdings}{}%
\def\@@incpdfdings#1x#2;{%
\@pdfdingsPages=#1%
\multiply\@pdfdingsPages by #2%
}
\newcommand{\incpdfdings}[4][]{%
\ifx\relax#1\relax%
\pdfximage{#3}%
\firstmaxpages=\pdflastximagepages%
\pdfximage{#4}%
\secondmaxpages=\pdflastximagepages%
\ifnum\firstmaxpages<\secondmaxpages%
\@latex@error{incpdfdings: Second file has more pages than first}{}%
\fi%
\else%
\firstmaxpages=#1%
\fi%
\curpage=0%
\@@incpdfdings#2;%
\loop\ifnum\firstmaxpages>\curpage%
\untilpage=\numexpr\curpage+\@pdfdingsPages\relax%
\ifnum\untilpage>\firstmaxpages%
\untilpage=\firstmaxpages%
\fi%
\@incpdfdings[frame=true]{\the\curpage}{\the\untilpage}{#3}{#2}%
\ifnum\secondmaxpages>\untilpage\else%
\untilpage=\secondmaxpages%
\fi%
\if@pdfdingsSecondFin\else%
\@incpdfdings{\the\curpage}{\the\untilpage}{#4}{#2}%
\fi%
\ifnum\secondmaxpages=\untilpage%
\@pdfdingsSecondFintrue%
\fi%

Now the command takes an additional argument specifying the nup option.