I would like to include multiple pages from a pdf document on a single page im my document.
\includepdf
can do that using the 'nup' option.
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\begin{document}
\chapter{includepdf}
\includepdf[pages={1-4},nup=2x2,pagecommand={\thispagestyle{fancy}}]{numbered_pages.pdf}
\includepdf[pages={1-4},nup=1x2,pagecommand={\thispagestyle{fancy}},angle=90]{numbered_pages.pdf}
\end{document}
However,
- The 2×2 pages are not displayed on the
\chapter
-page - On a regular page my header and footer are not respected, and
- The order should be 2-1-4-3 on two consecutive 1×2 pages, so that I can turn my printed document after binding 90 deg clockwise.
Responding to @Werner's request, I added the following code that produces roughly what I have in mind, using
\includegraphics
.
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{graphicx}
\begin{document}
\pagestyle{fancy}
\chapter{Desired Output}
\centering\null\hfill
\includegraphics[page=1,width=.35\linewidth,height=.4\textheight,keepaspectratio]{numbered_pages}
\hfill
\includegraphics[page=2,width=.35\linewidth,height=.4\textheight,keepaspectratio]{numbered_pages}
\hfill\null\vfill\null\hfill
\includegraphics[page=3,width=.35\linewidth,height=.4\textheight,keepaspectratio]{numbered_pages}
\hfill
\includegraphics[page=4,width=.35\linewidth,height=.4\textheight,keepaspectratio]{numbered_pages}
\hfill\null\clearpage
\null\vfill\noindent
\includegraphics[page=5,width=.48\linewidth,height=.45\textheight,keepaspectratio]{numbered_pages}
\hfill
\includegraphics[page=6,width=.48\linewidth,height=.45\textheight,keepaspectratio]{numbered_pages}
\vfill
\includegraphics[page=7,width=.48\linewidth,height=.45\textheight,keepaspectratio]{numbered_pages}
\hfill
\includegraphics[page=8,width=.48\linewidth,height=.45\textheight,keepaspectratio]{numbered_pages}
\vfill\null
\noindent
\includegraphics[page=2,width=.45\textheight,angle=90]{numbered_pages}
\vfill
\includegraphics[page=1,width=.45\textheight,angle=90]{numbered_pages}
\includegraphics[page=4,width=.45\textheight,angle=90]{numbered_pages}
\vfill
\includegraphics[page=3,width=.45\textheight,angle=90]{numbered_pages}
\end{document}
This serves as an illustration, but has the following flaws
- If the
\chapter
page is modified withtitlesec
, the code does not adapt itself to it - If the total number of pages is not a multiple of 4, the last pages are not typeset as they were otherwise
- On 1×2 pages, if a larger amount of pages is to be included, the pages need to be included individually by 2,1,4,3,6,5,8… (i.e. 4n-2,4n-3,4n,4n-1), can't a loop do that for me?
I do not have any preference for either \includepdf
or \includegraphics
. All I want to show on these pages are header, footer and the included pages.
How would I need to define \mycommand
such that the listed shortcomings are addressed and that
\begin{document}
\chapter{Desired Output}
\mycommand[pages={1-8},nup=2x2]{numbered_pages.pdf}
\mycommand[pages={1-4},nup=1x2,]{numbered_pages.pdf}
\end{document}
yields my desired output as depicted above?
Best Answer
For Previous Readers
If reading this answer for the first time, you will be none the wiser for reading the next paragraph and may safely skip to the Introduction below.
This solution is significantly simpler than the previous ones. It is stunningly obvious but I failed to see it. The syntax of
\chappdf
,\chappdf*
and\headerspdf
is unchanged. The command\headerspdf
is also unchanged. But the code for\chappdf
and\chappdf*
is significantly simpler. (Actually, the code doing the work may be essentially identical. However, you no longer need to include it in your document.) This also avoids unnecessarily duplicating code from the standard class. Moreover, it is compatible with some customisation of chapter titles.Introduction
As I explain in the comments on the original question, it is really not going to be possible to provide a solution which fully meets the criteria since the requirement to adjust to arbitrary redefinitions of the page layout through the use of
titlesec
makes it impossible to know how much space is available for images on the first page of chapters. Even if it is known thattitlesec
is used, the package allows entirely free-form layout which again makes it impossible to calculate how much space remains on the page.In addition, it is not possible to use
pdfpages
in a way which allows you to put the chapter title on the same page as pages included from PDFs if the\chapter
command is to be issued separately from the call to\includepdf
(without redefining\chapter
in a way which would break other uses of the command).Moreover, the differences in scaling etc. would make it much more complex to use a single command to include pages both on the first page of a chapter and on subsequent pages. So a separate solution is required for the start of chapters.
Overview
This solution uses
pdfpages
. It provides two custom commands:\chappdf
(plus a starred variant) and\headerspdf
.This solution will need to be adjusted if any of the following conditions hold:
A chapter title runs to several lines for a chapter on which you want to include PDF pages on the first page of the chapter. [Definitely needs adjustment but you should be able to tweak the code, I think. Plus you should probably rethink your chapter titles if they are that long!]
Either your document or the included PDF pages are not A4. [Possibly needs slight adjustment though I'm not sure. May be OK so long as size of included pages matches size of document. More likely to need adjustment if the two diverge, obviously.]
I have added
frame
to the options for the included PDFs just to show how things are laid out on the page. Obviously you can remove this option if you prefer. I've also adjusted the positioning of the included pages usingdelta
(for spacing between the pages) andoffset
(for spacing around the block of pages). The latter is necessary to get the images properly centred with the defaulttwoside
option ofbook.cls
. I usedgeometry
with theverbose
option to figure out the correct horizontal offset.I've used
xparse
as it makes it easy to handle the starred and optional arguments. The definition of\chappdf
and\chappdf*
depend only on features ofxparse
considered stable.The definition of
\headerspdf
depends on code fromxparse
marked as experimental. For this reason, should something break unexpectedly following an update,\ProcessList
should be considered the prime suspect. I am not entirely clear about the status of\SplitArgument
and\SplitList
. On the one hand, they are not included in the list of commands deemed stable. On the other, they are not marked as experimental either.New Commands
Two basic commands are provided for the inclusion of pages from PDFs. (Obviously the usual commands provided by
pdfpages
remain available as well.)\chappdf
The solution uses a custom command,
\chappdf
, to include pages from a PDF on the first page of a chapter. No more than 4 pages should be specified with this command. If you wish to include further PDF pages, use\headerspdf
to include them.\chappdf
is based on the definition of\chapter
. Like that command, it accepts an optional short title for use in headers and table of contents. It also accepts a*
if you do not want an entry in the ToC. Like\chapter*
it is up to you to adjust the page headers as appropriate in that case. With the regular command, as with\chapter
, this is automatic.The syntax is as follows
or
Both commands start a new page as if for a new chapter and set
chapter title
appropriately. If the second optional argument is not specified,-
will be assumed and all pages fromfilename.pdf
will be included. It is important the file not contain more than 4 pages in that case else the chapter will start over again on every new page. The syntax to specify the pages is identical to the value you would pass to thepages
key usingpdfpages
. E.g.1-2,4,89
or2-5
.\headerspdf
\headerspdf
is designed to include pages from a PDF in either 2x2 or 1x2 layout with the standard header and footer of thefancy
pagestyle supported byfancyhdr
. This means it is easy to redefine the headers and footers to your liking for the whole document and pages included with\headerspdf
will respect your configuration.\headerspdf
can automatically adjust the layout fornup=2x2
andnup=1x2
. The code will automatically sequence the included pages correctly in the latter case e.g. by putting the first page at the bottom with the second page above so that they read left-to-right when orientated upright. If you would also like PDF viewers to automatically rotate these pages for readers, remove theturn=false
option to\includepdf
which suppresses this behaviour. (The rotation should not affect printing either way.)If no optional argument is provided, the default specifies inclusion of all pages in 2x2 format, along with other standard options. In the case that
nup=1x2
is specified,\headerspdf
will also adjust the layout by requesting\includepdf
to uselandscape=true
. As explained above, this avoids the need to specify the correct sequence of pages manually. You can say something like:or:
and the command should do the right thing. The latter will include all pages of the PDF in 1x2 format.
Code
To make use of this solution, set up your document as follows:
Example
kantlipsum
is included to provide some filler text in the example, which also usesmwe
's documentation as a source PDF for the included pages.Code
Output
Styling Chapters
It is possible to redefine the standard
\chapter
and\chapter*
commands to alter the layout of chapter titles. If you do this, be aware of the effect this will have on\chappdf
and \chappdf*`. Two things are crucial:\includepdf
by these commands.pagecommand
option of\includepdf
. The code must also be compatible with the use ofxparse
, including the experimental features this code invokes.I ran several examples from
titlesec
's documentation through and they all broke things badly.That said, if you insist, it is possible to make judicious use of
titlesec
. If you try this and it breaks, you get to keep both pieces. I have personally never found something I could do withtitlesec
which I couldn't do more easily withouttitlesec
or simply without. I therefore do not recommend using it with my code. I know my code is characteristically allergic totitlesec
. Caveat emptor.Dis-Recommended Example
Here is an example using
titlesec
which does work.Crucial Caveats
titlesec
is used for nothing especially exotic. It does not attempt to include pictures, play animations or exit with a musical flourish. It does not try to start new paragraphs, pages or crazes.About all I can say for this example, really, is that it compiles without error which is more than I can say for any of the other examples I tried (which, I hasten to add, were clearly not intended to be misused and abused in the way I attempted). Caveat emptor.
Code (Which I Do Not Recommend)
Output