Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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,

  1. The 2x2 pages are not displayed on the \chapter-page
  2. On a regular page my header and footer are not respected, and
  3. The order should be 2-1-4-3 on two consecutive 1x2 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}

enter image description here

This serves as an illustration, but has the following flaws

  • If the \chapter page is modified with titlesec, 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 1x2 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?

share|improve this question
2  
Could you clarify (3) with a picture? Also, will there be other contents on the page other than the header/footer? –  Werner Feb 15 at 19:56
    
@Werner - If the pdf-pages are not shown on a \chapter page, there will be only header and footer –  D.Roepo Feb 15 at 21:50
    
The trouble is you are now asking for a command which will fit 4 images into an unknown area since you want to be able to make arbitrary changes to the styling of the chapter title. That greatly increases the complexity of what would be involved. Moreover, it isn't clear whether there is even any guarantee that you will be using titlesec 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. –  cfr Feb 16 at 17:12
    
By the way, none of the solutions suggested require the number of pages to be a multiple of 4 as far as I can tell. –  cfr Feb 16 at 17:44
    
The third of your 'shortcomings' can be dealt with using the 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. –  cfr Feb 16 at 17:57

4 Answers 4


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

Example illustrating use of <code>\headerspdf</code>, <code>\chappdf</code> and <code>\chappdf*</code>


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:

  1. 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.
  2. 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

Do NOT try this at home!


share|improve this answer
    
This works very nicely. However, \chappdf* still adds 'Chapter \thechapter' above the title. Can you tell me how to suppress that, as \chapter* only displays the chapter's title. –  D.Roepo Feb 17 at 17:37
    
It's a bit odd. If I add a * after the first \chappdf in your code, It says: 'Chapter 1 \\ 2x2 PDF on Chapter Page', where it should just say '2x2 PDF on Chapter Page'. However, if I call it in my document it works fine... –  D.Roepo Feb 17 at 18:10
    
@D.Roepo You are quite right. There was an error in my code. Please see my revised answer above. I'm not sure why it would have worked fine in your document. (It shouldn't!) Maybe you've redefined something else somewhere which counteracts the effect? Anyway, my testing suggests this now works as it should in the sense that it reflects the behaviour of chapter*. The page header needs to be adjusted just as it does for \chapter* but the erroneous 'Chapter...' before the title should be gone. –  cfr Feb 17 at 18:24
    
I was indeed changing secnumdepth to accidentally prevent \chappdf* from displaying 'Chapter \thechapter'. Now, as I do not fully understan the wrking of your solution: How do I keep all pages portait-oriented in my final document? –  D.Roepo Feb 18 at 9:26
    
@D.Roepo Do you want them to print portrait-orientated? That should happen already. Or do you want them to appear that way in the viewer as well? –  cfr Feb 18 at 15:09

What is wrong with \includegraphics command?

\documentclass{book}

\usepackage{fancyhdr}
\usepackage{pdfpages}

\begin{document}

\chapter{includepdf}
\centering
\fboxsep=0pt
\fbox{\includegraphics[page=1,width=.37\linewidth,height=.4\textheight,keepaspectratio]{pgfmanual}} \hfill
\fbox{\includegraphics[page=2,width=.37\linewidth,height=.4\textheight,keepaspectratio]{pgfmanual}}
\vfill
\fbox{\includegraphics[page=3,width=.37\linewidth,height=.4\textheight,keepaspectratio]{pgfmanual}}
\hfill
\fbox{\includegraphics[page=4,width=.37\linewidth,height=.4\textheight,keepaspectratio]{pgfmanual}}
\clearpage
\noindent
\fbox{\includegraphics[page=5,width=.48\linewidth,height=.45\textheight,keepaspectratio]{pgfmanual}}
\hfill
\fbox{\includegraphics[page=6,width=.48\linewidth,height=.45\textheight,keepaspectratio]{pgfmanual}}
\vfill
\fbox{\includegraphics[page=7,width=.48\linewidth,height=.45\textheight,keepaspectratio]{pgfmanual}}
\hfill
\fbox{\includegraphics[page=8,width=.48\linewidth,height=.45\textheight,keepaspectratio]{pgfmanual}}


\end{document}

enter image description here

share|improve this answer

The following should extract the first four pages of your pdf into pg_0001.pdf up to pg_0004.pdf:

pdftk A=numbered_pages.pdf cat A1-4 output new.pdf
pdftk new.pdf burst

Now you can use

\includegraphics{...}
in order to include these pdf files into your document individually. You can also use packages like TikZ on these graphics.

share|improve this answer
    
Alternative for pdftk: PDF Split and Merge (PDFsam). –  Speravir Feb 15 at 21:32

Just for fun without PSTricks.

\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{multido}
\def\filename{status-lua.pdf}
\def\scale{.1}
\pdfximage{\filename}
\begin{document}
\noindent\multido{\i=1+1}{\the\pdflastximagepages}{\fbox{\includegraphics[scale=\scale,page=\i,angle=90]{\filename}} }
\end{document}

enter image description here

share|improve this answer
    
As far as I know, status-lua.pdf is the only PDF file accessible for testing. –  Who is crazy first Feb 16 at 14:39
    
I don't understand this answer since none of the other answers use PSTricks either. And what do you mean that only status-lua.pdf is 'the only PDF file accessible for testing'? Also, it doesn't really seem to answer the question in any of the several forms it has taken. –  cfr Feb 16 at 17:03
    
@TheLastError: The package mwe has this name for reasons. ;-) See in …/tex/latex/mwe. –  Speravir Feb 17 at 0:42
    
@cfr: Well, the answer by our user with the monthly changing name shows, that it is generally possible with pstricks –  Speravir Feb 17 at 0:44
    
@Speravir I'm obviously deeply confused about some basic aspect of reality. Or virtual reality, at least. –  cfr Feb 17 at 1:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.