Change order of pages when using pdfpages

Consider the following example.

File: B.tex

\documentclass[
12pt,
landscape
]{article}

\usepackage[
a6paper
]{geometry}
\usepackage{multido}

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

\pagestyle{empty}

\begin{document}

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

\end{document}


File: C.tex

\documentclass[
12pt,
landscape
]{article}

\usepackage[
a6paper
]{geometry}
\usepackage{multido}

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

\pagestyle{empty}

\begin{document}

\multido{\i = 1+1}{16}{%
\begin{navn}
\thepage
\end{navn}
\newpage}

\end{document}


File: A.tex

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\begin{document}

\includepdf[
nup = 2x4,
pages = -,
frame = true
]{B}

\includepdf[
nup = 2x4,
pages = -
]{C}

\end{document}


(The documents B.tex and C.tex are much larger in reality.)

Question

UPDATED

How do I change the order of the pages in A.pdf from

Bp1, Bp2, ..., Bpn, Cp1, Cp2, ..., Cpn

to

Bp1, ..., Bp8, Cp1, ..., Cp8, [...], Bp(n-8), ..., Bpn, Cp(n-8), Cpn

for arbitrary n?

That is, in A.pdf I want only pages from B.pdf to be on the first page and only pages from C.pdf to be on the second page, and so on.

• Just for the record, your 'Bp(n-8)', ..., Bpn' is wrong (same for C), because if n%8 != 0 the last page doesn't start at n-8 :) – Skillmon likes topanswers.xyz Apr 19 '17 at 22:11

I deleted the previous code, so this answer fits the latest edit of your question.

The below code does what you want (and checks, whether C.pdf and B.pdf have the same pageamount, if not it prints until the last page of the one with lesser (in duplex-kind-of-way with B on one and C on the other page), and after that it prints the remaining of the one with more pages. It accepts an optional argument, which can give a fixed maxpage instead of all pages of both pdfs (though with fixed maxpage it's not checked whether both pdfs have that many pages).

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\newcount\maxpages
\newcount\curpage
\newcount\untilpage
\makeatletter
\newif\if@pdfdingsunbalance
\newcount\@testmaxpages
\newcommand{\@incpdfdings}[3]{\includepdf[nup=2x4,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+7\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{B.pdf}{C.pdf}
\end{document}


EDIT: I'll post the error detection anyway (it throws a warning):

\documentclass[
a4paper
]{article}

\usepackage{pdfpages}

\newcount\maxpages
\newcount\curpage
\newcount\untilpage
\makeatletter
\newif\if@pdfdingsunbalance
\newcount\@testmaxpages
\newcommand{\@incpdfdings}[3]{\includepdf[nup=2x4,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%
\pdfximage{#2}%
\@testmaxpages=\pdflastximagepages%
\ifnum\maxpages>\@testmaxpages%
\@latex@warning{The file #2 you wanted to be printed with
incpdfdings doesn't have enough pages to match your request in
the optional argument}%
\maxpages=\@testmaxpages%
\fi%
\pdfximage{#3}%
\@testmaxpages=\pdflastximagepages%
\ifnum#1>\@testmaxpages%
\@latex@warning{The file #3 you wanted to be printed with
incpdfdings doesn't have enough pages to match your request in
the optional argument}%
\ifnum\maxpages>\@testmaxpages%
\maxpages=\@testmaxpages%
\fi\fi%
\fi%
\curpage=0%
\loop\ifnum\maxpages>\curpage%
\untilpage=\numexpr\curpage+7\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[60]{B.pdf}{C.pdf}
\end{document}

• See my new answer (well this one, but I removed all of the previous answer). It uses pdfpages. – Skillmon likes topanswers.xyz Apr 19 '17 at 22:36
• The unbalanced spacing is not my fault, but the result of pdfpages. If you're interested, I can post an error detection for the optional argument (resulting in only the minimal amount of pages both have, if one or both has less pages than the optional argument) – Skillmon likes topanswers.xyz Apr 19 '17 at 22:47

it could be more efficient to combine the two PDF files into one and then in a second time add it to your document C. This link could be useful if you're used to JavaScript: http://www.planetpdf.com/developer/article.asp?ContentID=collating_pdfs_using_javascrip&rh%20s_fa

or this online service https://www.sejda.com/en/alternate-mix-pdf