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 that titlesec
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 using delta
(for spacing between the pages) and offset
(for spacing around the block of pages). The latter is necessary to get the images properly centred with the default twoside
option of book.cls
. I used geometry
with the verbose
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 of xparse
considered stable.
The definition of \headerspdf
depends on code from xparse
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
\chappdf[short title]{chapter title}[page specification]{filename.pdf}
or
\chappdf*{chapter title}[page specification]{filename.pdf}
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 from filename.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 the pages
key using pdfpages
. E.g. 1-2,4,89
or 2-5
.
\headerspdf
\headerspdf
is designed to include pages from a PDF in either 2x2 or 1x2 layout with the standard header and footer of the fancy
pagestyle supported by fancyhdr
. 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 for nup=2x2
and nup=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 the turn=false
option to \includepdf
which suppresses this behaviour. (The rotation should not affect printing either way.)
\headerspdf[pdf inclusion options]{filename.pdf}
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 use landscape=true
. As explained above, this avoids the need to specify the correct sequence of pages manually. You can say something like:
\headerspdf[pages=56-67,nup=1x2]{greatdoc.pdf}
or:
\headerspdf[nup=1x2]{fantasypdfs.pdf}
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:
\documentclass[a4paper]{book}
\usepackage{geometry}% optional
\usepackage{fancyhdr}% required (or change fancy to another pagestyle)
\usepackage{pdfpages}% required
\usepackage{xparse}% required
\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-,nup=2x2} m }{%
\gdef\@myland{false}%
\@splitpdfopts{#1}%
\includepdf[%
pages=-,
nup=2x2,
#1,
delta=5pt 5pt,
landscape=\@myland,
turn=false,
pagecommand={\thispagestyle{fancy}},
scale=.65,
frame,
offset=-15pt 15pt]{#2}}
\NewDocumentCommand\@splitpdfopts{ >{\SplitList{,}}m }{%
\ProcessList{#1}{\@splitpdfarg}}
\NewDocumentCommand\@splitpdfarg{ >{\SplitArgument{1}{=}}m }{%
\@tweakpdfopts #1}
\newcommand\@tweakpdfopts[2]{%
\def\tempa{nup}%
\def\tempb{#1}%
\def\tempc{1x2}%
\def\tempd{#2}%
\ifx\tempa\tempb
\ifx\tempd\tempc
\gdef\@myland{true}%
\fi
\fi}
\NewDocumentCommand\chappdf{somO{-}m}{% include all pages of PDF by default
\cleardoublepage
\includepdf[%
pages={#4},
pagecommand={%
\IfBooleanTF{#1}{%
\chapter*{#3}}{%
\IfNoValueTF{#2}{%
\chapter{#3}}{%
\chapter[#2]{#3}}}},
nup=2x2,
scale=.5,
frame,
delta=5pt 5pt,
offset=-15pt -45pt]%
{#5}}
\makeatother
\pagestyle{fancy}% optional but probably what you want unless you change fancy in the definition of \headerspdf above
\begin{document}
% Document here as usual
\end{document}
Example
kantlipsum
is included to provide some filler text in the example, which also uses mwe
's documentation as a source PDF for the included pages.
Code
\documentclass[a4paper]{book}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{xparse}
\usepackage{kantlipsum}
\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-,nup=2x2} m }{%
\gdef\@myland{false}%
\@splitpdfopts{#1}%
\includepdf[%
pages=-,
nup=2x2,
#1,
delta=5pt 5pt,
landscape=\@myland,
turn=false,
pagecommand={\thispagestyle{fancy}},
scale=.65,
frame,
offset=-15pt 15pt]{#2}}
\NewDocumentCommand\@splitpdfopts{ >{\SplitList{,}}m }{%
\ProcessList{#1}{\@splitpdfarg}}
\NewDocumentCommand\@splitpdfarg{ >{\SplitArgument{1}{=}}m }{%
\@tweakpdfopts #1}
\newcommand\@tweakpdfopts[2]{%
\def\tempa{nup}%
\def\tempb{#1}%
\def\tempc{1x2}%
\def\tempd{#2}%
\ifx\tempa\tempb
\ifx\tempd\tempc
\gdef\@myland{true}%
\fi
\fi}
\NewDocumentCommand\chappdf{somO{-}m}{% include all pages of PDF by default
\cleardoublepage
\includepdf[%
pages={#4},
pagecommand={%
\IfBooleanTF{#1}{%
\chapter*{#3}}{%
\IfNoValueTF{#2}{%
\chapter{#3}}{%
\chapter[#2]{#3}}}},
nup=2x2,
scale=.5,
frame,
delta=5pt 5pt,
offset=-15pt -45pt]%
{#5}}
\makeatother
\pagestyle{fancy}
\begin{document}
\frontmatter
\tableofcontents
\chappdf*{2x2 PDF on Starred Chapter Page}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[2]
\mainmatter
\chapter{Include 2x2 PDF after Chapter Page}
\kant[1]
\headerspdf[pages=1-8,nup=2x2]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\chappdf{2x2 PDF on Chapter Page}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[2]
\chappdf[Short Title]{2x2 PDF before 1x2 PDF}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[3]
\headerspdf[pages=1-4,nup=1x2]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\end{document}
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:
- The title must not consume more space than is allowed above the included PDF pages. You can alter this, if necessary, by editing the options passed to
\includepdf
by these commands.
- The redefined command should not do anything too strange or it will break stuff when it is passed to the
pagecommand
option of \includepdf
. The code must also be compatible with the use of xparse
, including the experimental features this code invokes.
The injudicious use of titlesec
or similar will break
stuff. This is expected.
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 with titlesec
which I couldn't do more easily without titlesec
or simply without. I therefore do not recommend using it with my code. I know my code is characteristically allergic to titlesec
. 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.
- In addition to introducing what I consider an unknown amount of
volatility, this also looks dreadful. (I prefer not to encourage the
use of a package I'm apparently allergic to.)
- Caveat emptor.
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)
\documentclass[a4paper]{book}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{xparse}
\usepackage{titlesec}
\renewcommand{\thechapter}{\Roman{chapter}}
\titleformat{\chapter}[display]{\bfseries\large}{\MakeUppercase{\chaptertitlename} \thechapter}{2ex}{\Huge}[\vspace{2ex}]
\usepackage{kantlipsum}
\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-,nup=2x2} m }{%
\gdef\@myland{false}%
\@splitpdfopts{#1}%
\includepdf[%
pages=-,
nup=2x2,
#1,
delta=5pt 5pt,
landscape=\@myland,
turn=false,
pagecommand={\thispagestyle{fancy}},
scale=.65,
frame,
offset=-15pt 15pt]{#2}}
\NewDocumentCommand\@splitpdfopts{ >{\SplitList{,}}m }{%
\ProcessList{#1}{\@splitpdfarg}}
\NewDocumentCommand\@splitpdfarg{ >{\SplitArgument{1}{=}}m }{%
\@tweakpdfopts #1}
\newcommand\@tweakpdfopts[2]{%
\def\tempa{nup}%
\def\tempb{#1}%
\def\tempc{1x2}%
\def\tempd{#2}%
\ifx\tempa\tempb
\ifx\tempd\tempc
\gdef\@myland{true}%
\fi
\fi}
\NewDocumentCommand\chappdf{somO{-}m}{% include all pages of PDF by default
\cleardoublepage
\includepdf[%
pages={#4},
pagecommand={%
\IfBooleanTF{#1}{%
\chapter*{#3}}{%
\IfNoValueTF{#2}{%
\chapter{#3}}{%
\chapter[#2]{#3}}}},
nup=2x2,
scale=.5,
frame,
delta=5pt 5pt,
offset=-15pt -45pt]%
{#5}}
\makeatother
\pagestyle{fancy}
\begin{document}
\frontmatter
\tableofcontents
\chappdf*{2x2 PDF on Starred Chapter Page}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[2]
\mainmatter
\chapter{Include 2x2 PDF after Chapter Page}
\kant[1]
\headerspdf[pages=1-8,nup=2x2]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\chappdf{2x2 PDF on Chapter Page}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[2]
\chappdf[Short Title]{2x2 PDF before 1x2 PDF}[1-4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[3]
\headerspdf[pages=1-4,nup=1x2]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\end{document}
Output

\chapter
page, there will be only header and footertitlesec
or whether that is just a possibility or, if you do, what changes you will make with it. Moreover, requiring that answers adhere to the given syntax is extremely restrictive. As is ruling out a specially adapted form of\chapter
.landscape
option for\includepdf
. I've edited my answer to use this option. The second is not a problem as far as I can tell. If I ask for 5 pages to be included, I get 5 pages included. Perhaps you could add an example of the problem you're seeing as I can't reproduce it here. That leaves the first, which I don't think would be at all easy to solve given that you might change the page layout in arbitrary ways. It would not be hard to write a\newcommand
if you didn't require it to work straight after\chapter
.