# includepdf results in undesired headings

\includepdf[pages={34-40},nup=2x2,frame,landscape,scale=0.8,%
pagecommand=\chapter{fracture}\section{Theoretical}]{Dam-Nonlinear.pdf}
\clearpage
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8,%
pagecommand={\section{blablah}}]{ASCE-Cyclic-Joint-Model.pdf,1,%
ASCE-Cyclic-Joint-Test.pdf,1}
\includepdf[scale=0.8,nup=2x2,pages={9-13},frame,landscape,%
pagecommand=\section{Experimental Work}]{Ageing-Shaking-Cracking.pdf}
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8]{cement-concrete-slowik.pdf,1,%
Optical_Fiber-in-FPZ.pdf,1}


results in

1. Correct header of chapter 1 (Fracture), and section 1.1 (Theoretical) and followed by the pdf file to be included (this takes two pages)
2. First problem, on page 3, I get: Chapter 2 Fracture and 2.1 Theoretical again, followed by blank. Clearly wrong
3. Then I correctly get section 2.2 blah blah followed by the pdf to be included (one page).
4. I get correctly 2.4 Experimental work on the first of two pages, but again the same header of the second (of two) page.
-
Have you tried enclosing the argument(s) of each pagecommand= instruction in curly braces? In the code you provide, you only appear to do so for the first \includepdfmerge macro, but not for the two \includepdf macros. – Mico Apr 23 '12 at 17:36
As mentioned in the pdfpages documentation, the pagecommand key is executed on every page (or sheet of paper). You're interested in something like a pagecommand* key that does not currently exist. As such, you need some other workaround. – Werner Apr 23 '12 at 17:41
Have you tried putting curly braces around -- or, equivalently, inserting \begingroup and \endgroup statements before and afer -- the \includepdfmerge and \includepdf statements? This should keep the scope of the respective pagecommand directives localized. (By default, the pagecommand statements apply to all pages...) – Mico Apr 23 '12 at 19:11

The pagecommand option is only meant for "immaterial" things like \thispagestyle. The way you use it a new chapter will start on every included page.

I assume you are using a book-like class where a new chapter always starts on a right page. The empty page inserted by the second \chapter is what really messes things up.

I advise to move \chapter and \section completely outside of \includepdf:

\chapter{fracture}\section{Theoretical}
\includepdf[pages={34-40},nup=2x2,frame,landscape,scale=0.8,%
pagecommand={}]{texbook.pdf}
\clearpage
\section{blablah}
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8,%
pagecommand={}]{texbook.pdf,%
1,texbook.pdf,1}
\section{Experimental Work}
\includepdf[scale=0.8,nup=2x2,pages={9-13},frame,landscape,%
pagecommand={}]{texbook.pdf}
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8]{texbook.pdf,%
1,texbook.pdf,1}


Of course I can't judge whether this is what you want as you've given very little detail on this.

-
Sure, but then I get a nearly empty blank page (with the headers) which is "ugly". Oh well, I will limit myself to one page inclusion. Thanks – victor Apr 23 '12 at 18:23
@victor Yes, I had assumed as much. Maybe you don't want the sectioning commands themselves but just the page headers? Take a look at the fancyhdr package, it lets you set the headers to anything you want. – Stephan Lehmke Apr 23 '12 at 18:31

You could include the pdf twice. The first time include only the first page. The second time include all the other pages without a title.

\includepdf[pages=34,nup=2x2,frame,landscape,scale=0.8,
pagecommand=\chapter{fracture}\section{Theoretical}]{Dam-Nonlinear.pdf}
\includepdf[pages={35-40},nup=2x2,frame,landscape,scale=0.8
{Dam-Nonlinear.pdf}
\clearpage

-

Maybe you want something like this? Note that it would be a lot easier with a proper minimal example to work with. This is more than half guesswork just in terms of understanding what you are trying to do - never mind thinking about ways to achieve it.

If my guess is correct, you want output something like this:

I don't know why all the landscape stuff. In that, I'm just following the code in the question even though it doesn't make much sense to me as output. (Maybe the included files are specially tailored or something so that the portrait headings don't look odd with the landscaped content.)

Anyway, you can adjust it if you need to.

The key point is that the \chapter and/or \section etc. commands are issued only on the first page of any single PDF inclusion. This is achieved by wrapping these commands in a new command

\dynpage{<stuff for first page>}


in the argument to pagecommand. What this command does is trigger a dynamic sequence which redefines itself to \relax after the first iteration. So the stuff for the first page does not get typeset on anything but the first page.

\def\victor@dynpage{\victor@firstpage\global\let\victor@dynpage\relax}
\newcommand*\dynpage[1]{%
\def\victor@firstpage{#1}%
\victor@dynpage}


The problem with this is that the command is needed more than once. So, we need to reset it each time so it is not just \relax. For this, we use \pretocmd from etoolbox which prepends our dynamic sequence to \includepdf.

\pretocmd{\includepdf}{%
\def\victor@dynpage{\victor@firstpage\global\let\victor@dynpage\relax}%
}


Complete code:

\documentclass[a4paper,openany]{book}
\usepackage{geometry,pdfpages,etoolbox}
\geometry{scale=.85}
\makeatletter
\pretocmd{\includepdf}{%
\def\victor@dynpage{\victor@firstpage\global\let\victor@dynpage\relax}%
}
\newcommand*\dynpage[1]{%
\def\victor@firstpage{#1}%
\victor@dynpage}
\makeatother
\begin{document}
\includepdf[pages={-},nup=2x2,frame,landscape,scale=0.8,%
pagecommand=\dynpage{\chapter{fracture}\section{Theoretical}}]{k}
\clearpage
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8,%
pagecommand={\section{blablah}}]{example-image-a4,1,%
example-image-a4,1}
\includepdf[scale=0.8,nup=2x2,pages={-},frame,landscape,%
pagecommand=\dynpage{\section{Experimental Work}}]{k}
\includepdfmerge[nup=1x2,frame,landscape,scale=0.8]{example-image-a4,1,%
example-image-a4,1}
\end{document}

-