# How to reorder included pdf-pages for bulk cutting ? [for paper saving]

### Situation and Problem

I receive continuously updated lecture scripts as (A4) PDFs that I need to print. The text area apparently fits onto A5 paper due to the large margins (that are usual with default LaTeX-documents). In order to save some paper, I would like to print these A4 files as A5 but without resizing them (things become too small), but rather cutting edges. Since I don't know how many pages this will be in the end, I can not use signature.

I know that this is bad from an aesthetical standpoint.

In the MWE below you can see, that I am trimming the included pages (try to use not nup=1x2, landscape but frame, noautoscale) from margins I found with \layout command. However, I am unable to rearrange the order of the nup=1x2 pages such that I could cut them in bulk and get ordered usual A5 sheets. Later on I want to cut the pages and manually either glue- or hole-punch-bind them together.

### Question

How to reorder included pdfpages with nup=1x2, landscape to get bulk cut-able A5 pages?

Let me show the desired order with an adapted scheme from here (please give Village credit). It would be more practical for processing bigger volumes if the order was like this:

(front of 1st paper)  (back of 1st paper)     (stacking order)
________ ________     ________ ________           /|======>>/|  ...==>>/|
|        |        |   |        |        |     1 ->/ |   3 ->/ |   h-1->/ |
|        |        |   |        |        |        /  |      /  |       /  |
|        |        |   |        |        |       / 2 |     / 4 |      / h |
|_____1__|___h+1__|   |__2_____|__h+2___|       |\  |     |\  |      |\  |
| \ |     | \ |  ... | \ |
(front of 2nd paper)  (back of 2nd paper)       |  \      |  \       |  \
________ ________     ________ ________        |   |     |   |      |   |
|        |        |   |        |        |       |h+1|     |h+3|      |n-1|
|        |        |   |        |        |        \  |      \  |       \  |
|        |        |   |        |        |         \ |<-h+2  \ |<-h+4   \ | <- n
|_____3__|___h+3__|   |__4_____|__h+4___|          \|======>>\|  ...==>>\|

...

(front of last paper) (back of last paper)
________ ________     ________ ________
|        |        |   |        |        |
|        |        |   |        |        |
|        |        |   |        |        |
|___h-1__|___n-1__|   |__h_____|__n_____|


... for n pages with h := n/2.

MWE
\documentclass[
12pt,
twoside
]{article}
\usepackage[a4paper]{geometry}
\usepackage{pdfpages}

% Border space as numbered in layout scheme ( https://tex.stackexchange.com/a/376643/ )
% top = 2 - 4 + 5 + 6
\newlength{\distup}

% % left = 1+3
\newlength{\distleft}
\setlength{\distleft}{\dimexpr1in+\hoffset+\oddsidemargin\relax}
% \addtolength{\distleft}{20pt} % doesnt work due to twosided import

% % bottom = 11 + ? // = paperheight - oben - textheight
\newlength{\distdown}
\setlength{\distdown}{\dimexpr\paperheight-\distup-\textheight\relax}
% \addtolength{\distdown}{5pt} % we can cut a little more

% % right = 9 + 10 + ? // = paperwidth - link - textwidth
\newlength{\distright}
\setlength{\distright}{\dimexpr\paperwidth-\distleft-\textwidth\relax}
% \addtolength{\distright}{20pt} % doesnt work due to twosided import
% %

\begin{document}
\includepdf[
pages=-,
nup=1x2,
landscape,
clip,
trim={{\distleft} {\distdown} {\distright} {\distup}},
offset={-9pt 0pt}, % -9pt centers // works with twoside but not nup=1x2, landscape
% noautoscale,
frame
]{testfile.pdf}

\end{document}


I am using a given PDF but this should work for the MWE (though I noticed, that my margins don't seem to fit here?):

testfile.tex
\documentclass[
12pt
]{article}
\usepackage{pdfpages}

\usepackage{lipsum}

\begin{document}
\lipsum
\end{document}


notes:

The first case "(might be easier to implement ?)" is solved in my own answer below, but not the remaining above. I will keep this question up in order to focus on and accept answers for that part.

• I just found pgfmorepages which should be able to cover the first layout. May 19, 2020 at 13:56
• Why don't you just use the Printing wizard (e.g. in Adobe Reader) and choose Multiple or Booklet? May 19, 2020 at 17:02
• @ivankokan Where would be the fun in that? Also I want to do this with LaTeX just because I don't like having to have a thousand and one programs for every little thing. May 20, 2020 at 17:32
• Certainly possible with pgfmorepages. The issue would be that the number of pages could vary from document to document so there's not a fixed layout that would do the trick. But a dynamic layout should be fine with a bit of LaTeX3 magic. Might take a day or two, but I can think of a couple of ways to do this. May 24, 2020 at 8:05
• @AndrewStacey good to hear! What do you think is necessary for this to work? (Should I add some tags to the question?) May 24, 2020 at 14:39

So I found this solved question that answers one part of the question:

either (might be easier to implement ?):
(front of 1st paper)    (back of 1st paper)
________ ________       ________ ________
|        |        |     |        |        |
|        |        |     |        |        |
|        |        |     |        |        |
|_____1__|_____3__|     |__2_____|__4_____|


which should stack like that

          /|=====>>/|
1 ->/ |  5 ->/ |
/  |     /  |
/ 2 |    / 6 |
|\  |    |\  |
| \ |    | \ |  ...
|  \     |  \
|   |    |   |
| 3 |    | 7 |
\  |     \  |
\ |<- 4  \ |<- 8
\|=====>>\|


Usage of pgfpmorepages like this, creates the desired layout:

\documentclass[
12pt,
% a4paper
twoside
]{article}
\usepackage[a4paper]{geometry}

\usepackage{pgfmorepages} % before pdfpages !
\usepackage{pdfpages}

%
\newlength{\distup}
\newlength{\distleft}
\setlength{\distleft}{\dimexpr1in+\hoffset+\oddsidemargin\relax}
\newlength{\distdown}
\setlength{\distdown}{\dimexpr\paperheight-\distup-\textheight\relax}
\newlength{\distright}
\setlength{\distright}{\dimexpr\paperwidth-\distleft-\textwidth\relax}
%

\pgfpagesuselayout{4 on 2, book format}[
a4paper,
landscape,
% border code=\pgfusepath{draw}
]

\begin{document}
\includepdf[
pages=-,
clip,
trim={{\distleft} {\distdown} {\distright} {\distup}},
frame
]{testfile.pdf}

\end{document}


### edit:

1. This apparently does not allow to flip every other page (or I could not figure out how; reverse second threw a compilation error). Feel free to improve this answer or add an improved answer.
2. book format and short page edge binding printing-option does the trick though!

Well, you can always calculate the correct value for signature. All you need to know is the total number of pages of the pdf, which is accessible through TeX-engine specific commands, like \lastsavedimageresourcepages from luaTeX. Other engines name it differently.

In the following example I use some internal macros from pdfpages which are independent of the TeX-engine, like \AM@pagecount, to get the total number of pages. After a few short calculation the counter mysig contains the correct value for signature.

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

\makeatletter
\newcounter{mysig}
\newcommand\myincludepdf[2][]{%
\def\AM@currentdocname{#2}%
\AM@getpagecount
\setcounter{mysig}{\AM@pagecount-\AM@pagecount/4*4}%
\ifnum\c@mysig>0
\setcounter{mysig}{\AM@pagecount/4*4+4}%
\else
\setcounter{mysig}{\AM@pagecount/4*4}%
\fi
\includepdf[#1,landscape, pages=-, signature=\c@mysig]{#2}%
}
\makeatother

\begin{document}

\myincludepdf{dummy.pdf}

\end{document}

• Thank you for your answer. However, I don't think that it answers the question, as I was specifically asking for non-signature solutions, because "I don't know how many pages [there] will be in the end, I can not use signature.". Testing the code yielded a nice booklet for the part I had, but how would I incorporate future parts? (That is basically why I want it to be ordered as shown in the scheme.) Jun 6, 2020 at 16:30
• Do it in 2 steps: First, create a temporary pdf by concatenating all your pdfs. Second, use \myincludepdf as shown in the example. Jun 6, 2020 at 20:19
• The point of this question was, to have an option to print it along the way, in order to follow the current lectures. If that was no constraint, your solution was perfectly fine. Jun 7, 2020 at 22:20
• Of course, you can do it in one step: Instead of retrieving the number of pages of just one pdf, you sum up in a loop the number of pages over all pdfs. The calculation of the correct signature value is the same (but now with the total number of pages). And eventually you use \includepdfmerge instead of \includepdf. Jun 8, 2020 at 8:01