问题
如果中间没有任何东西,我可以强制 LaTeX 始终将一个部分和一个子部分保留在同一页面上吗?
情况
假设我有一个名为的部分\section{All About Monkeys}
,后面跟着一个subsection{Introduction}
。我怎样才能确保两者之间永远不会有分页符?
例子
这是我的情况的一个例子。也许可以进一步最小化。tikz, tikzlibrary, anyfontsize
全部由 所用titlesec
。感谢 Werner,我还包括了hyperref
,因为它也会影响部分间距。
\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}
输出
下面的例子是绝对不应该发生的事情:
答案1
命令\needspace
是问题的一部分,但真正的罪魁祸首是\color
插入 whatsit 的 -command。请添加\nopagebreak
或使用 egreg 的解决方案: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}