0

Question

Can I force LaTeX to always keep a section with a subsection on the same page if there is nothing in between?

Situation

Let's suppose I have a section called \section{All About Monkeys} and a subsection{Introduction} following it. How can I ensure that there will never be a page break between the two?

Example

Here is an example of my situation. Perhaps it could be further minimized. tikz, tikzlibrary, anyfontsize are all used by titlesec. Thanks to Werner, I also included hyperref, because it also affects the section spacing.

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{anyfontsize}
\usepackage{needspace}
\usepackage{hyperref}
\usepackage[explicit]{titlesec}

\newcommand{\tikzsectitle}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={inner sep=0pt,outer sep=0pt}]
    \path let \p1=($ (titlenumber.east)-(titlenumber.west) $)
    in
    node [text width=\textwidth-\x1,align=left] (titletext) {#1\strut};
\end{tikzpicture}%
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={inner sep=0pt,outer sep=0pt}]
    \node [font=\fontsize{60}{70}\selectfont,align=left] (titlenumber) {\makebox[1em][l]{#1}};
\end{tikzpicture}%
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
    \draw ([yshift=-0ex]titletext.south west) -- ([yshift=-0ex]titletext.south east);
\end{tikzpicture}%
}%

% TITLE FORMATTING
\titleformat{\section}[hang]{\needspace{8cm}\sffamily\color{blue}\Huge\bfseries}{}{0pt}{\tikzsecnumber{\thesection}\tikzsectitle{#1}\tikztitleline{}}
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titleformat{\subsection}[hang]{\needspace{6cm}\color{blue!85}\Large\bfseries}{\thesubsection\quad}{0pt}{#1}
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}


\begin{document}
\lipsum[1-2]
Monkeys are haplorhine (dry-nosed) primates, a group generally possessing tails and consisting of approximately 260 known living species. Many monkey species are tree-dwelling (arboreal), although there are species that live primarily on the ground, such as baboons. 
\section{All About Monkeys}
\subsection{Introduction}
\lipsum[1]
\end{document}

Output

The following is an example of what should never occur: enter image description here enter image description here

8
  • 1
    Let's suppose we have a minimal example... yes. You're talking about sectional units, influenced by many packages. Please provide us with something to work with, even if it doesn't use any packages...
    – Werner
    Feb 11, 2015 at 7:54
  • 2
    ...note that, by default, this doesn't happen.
    – Werner
    Feb 11, 2015 at 8:02
  • I am working on the example now...I have several things to consider. I am using titlesec, needspace, and I have some weird tikz things going on in the sectioning commands. I think I will leave out the tikz if that is ok. Feb 11, 2015 at 8:03
  • I think latex never break page with just section title in the last line.
    – touhami
    Feb 11, 2015 at 8:05
  • Just as long as we can reproduce your problem. Don't forget to also mention if you're using hyperref - it also plays a part in sectional units.
    – Werner
    Feb 11, 2015 at 8:06

1 Answer 1

2

The \needspace commands are part of the problem but the real culprit is the \color-command which inserts a whatsit. Either add a \nopagebreak or use the solution from egreg here: https://tex.stackexchange.com/a/101643/2388

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{anyfontsize}
\usepackage{needspace}
\usepackage{hyperref}
\usepackage[explicit]{titlesec}

\newcommand{\tikzsectitle}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={inner sep=0pt,outer sep=0pt}]
    \path let \p1=($ (titlenumber.east)-(titlenumber.west) $)
    in
    node [text width=\textwidth-\x1,align=left] (titletext) {#1\strut};
\end{tikzpicture}%
}%
\newcommand{\tikzsecnumber}[1]{%
\begin{tikzpicture}[remember picture,baseline,every node/.style={inner sep=0pt,outer sep=0pt}]
    \node [font=\fontsize{60}{70}\selectfont,align=left] (titlenumber) {\makebox[1em][l]{#1}};
\end{tikzpicture}%
}%
\newcommand{\tikztitleline}{%
\begin{tikzpicture}[remember picture,overlay,every node/.style={inner sep=0pt,outer sep=0pt}]
    \draw ([yshift=-0ex]titletext.south west) -- ([yshift=-0ex]titletext.south east);
\end{tikzpicture}%
}%

% TITLE FORMATTING
\titleformat{\section}[hang]{\sffamily\color{blue}\Huge\bfseries}{}{0pt}{\tikzsecnumber{\thesection}\tikzsectitle{#1}\tikztitleline{}}
\titlespacing{\section}{0pt}{40pt}{5ex plus .2ex}
% SUBSECTION
\titleformat{\subsection}[hang]{\color{blue!85}%\nopagebreak %<--uncomment
\Large\bfseries}{\thesubsection\quad}{0pt}{#1}
\titlespacing*{\subsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}


\begin{document}
\lipsum[1-3]a\\b\\c\\d\\e\\f
\section{All About Monkeys}
\subsection{Introduction}
\lipsum[1]
\end{document}
2
  • needspace is an essential part of my workflow by the way. Feb 11, 2015 at 9:02
  • Well needspace inserts break points. So if you use it in the \subsection command is will allow a break before the \subsection. If you really think you need it in the \subsection try \if@nobreak\else\needspace{6cm}\fi (don't forget \makeatletter) so that it doesn't is inserted if there is a \section directly before. Feb 11, 2015 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .