# Unwanted page break between a section heading and its subsection heading

My scenario consists of some sections and subsections. A slice of the aforementioned group has been presented, as below:

\tableofcontents

\clearpage

\section{parent}

%\subsection{child}
\includepdf[trim=0 0 0 -7cm,pages=1,pagecommand=\subsection{child}]{"D:/A/A_"}
\includepdf[trim=0 0 0 -3cm, pages=2-,pagecommand={}]{"D:/A/A_"}


Each section acts just as an empty parent container for its child subsection. So, one might infer that the subsection must be placed right after its corresponding section heading. But the result after compilation would be:

\section{parent}
% page break does occur here
\subsection{child}


I have not modified the default section nor subsection, nor any other command in this file.

• I have just tried a solution using the samepage environment, but that did lead to nothing promising.

• Using \nobreak & \nopagebreak commands did not help, too.

• Plus useless utilization of \let\clearpage\relax.

• if you use pagecommand=\subsection{child} the subsection heading will be in the included page so new page after \section. – touhami Jan 25 '16 at 19:25
• @touhami: So, what kind of modification should I apply to resolve that new page?! – Roboticist Jan 25 '16 at 19:28

I created the following 10-page lipsum:

\documentclass{article}
\usepackage{lipsum}
\pagestyle{empty}
\begin{document}
\sloppy
\vspace*{5cm}
\begin{center}
\bfseries This is \verb|lipsum[1-50]|
\end{center}
\lipsum[1-50]
\end{document}


This is how I would include it in the way you may be after (which avoids a page-break:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\section{Parent}

%\includepdf[pages=1,pagecommand={\subsection{Child}}]{lipsum50}
\subsection{Child}
\raisebox{-\height}[0pt][0pt]{\makebox[\textwidth]{%
\includegraphics[page=1,trim=0 0 0 9cm,clip]{lipsum50}%
}}

\includepdf[pages=2-,pagecommand={\thispagestyle{plain}}]{lipsum50}
\end{document}


The main approach is to include the first page using \includegraphics[page=1,...]{<file>} from graphicx (loaded by pdfpages).

• We trim (and clip) the first page so only the relevant content is included. Based on your example, it seems like you have some blank space at the top of the first page.
• The included first page is "raised" into position with a 0pt height and depth so it doesn't affect any overflow (vertically).
• Subsequent pages are included in the usual way using \includepdf, with an accompanying \thispagestyle that matches the rest of your document.