From a31e3af59ef0d14d944ec2ade6df1a05394582b4 Mon Sep 17 00:00:00 2001 From: Patrick Neumann Date: Wed, 23 Sep 2026 19:46:00 +0200 Subject: [PATCH] Fix orphaned \section and \subsection headings (#226) In the body variants that typeset headings with \cvitem (i/ii, used by the casual and classic styles, and vi, used by the contemporary style), \cvitem ends with \par\addvspace{0ex}. That contributes glue right after the heading box and *before* the \nobreak of the heading definition. Glue following a box is a legal page breakpoint, so LaTeX was free to break there and leave the heading alone at the bottom of a page while its first \cventry moved to the next page. Add \@cvheadingitem to moderncv.cls, which calls \cvitem with \addvspace gobbled, and use it in the affected heading definitions. The \nobreak is then the first item after the heading box, so the page can only break before the heading. The heading's own \par\nobreak\addvspace{...} still supplies the space below it, so output is unchanged whenever no break occurs. Verified with pdflatex on all six shipped styles: renderings are pixel-identical to the previous ones for documents without an orphan, and casual/classic (\section and \subsection) plus contemporary (\subsection) no longer orphan headings. banking, oldstyle and fancy were never affected, as their headings do not go through \cvitem. Co-Authored-By: Claude Opus 5 --- CHANGELOG | 4 ++++ moderncv.cls | 10 ++++++++++ moderncvbodyi.sty | 4 ++-- moderncvbodyvi.sty | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 50ddd43..d1c6737 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,10 @@ version next - Fix "Underfull \hbox (badness 10000)" warning in banking \subsection dotted rules by adding \hss glue as in \dotfill (#307) - Update "circle" option to allow for half-filled circles in skill matrix (#309) +- Fix orphaned \section and \subsection headings in the casual, classic and + contemporary styles: the trailing \addvspace of the \cvitem used to typeset + the heading allowed a page break between the heading and its first entry + (#226) version 2.6.1 (24 Jun 2026) - Allow the customisation of the contemporary style (#297) diff --git a/moderncv.cls b/moderncv.cls index 1bc93cf..7cfaafd 100644 --- a/moderncv.cls +++ b/moderncv.cls @@ -567,6 +567,16 @@ % usage: \cvitem[spacing]{header}{text} \newcommand*{\cvitem}[3][.25em]{} +% typesets a (sub)section heading as a \cvitem, without the trailing vertical +% space \cvitem normally adds +% usage: \@cvheadingitem{header}{text} +% the trailing \addvspace of \cvitem would contribute glue right after the +% heading box and before the \nobreak of the heading definition; glue after a +% box is a legal page breakpoint, so LaTeX could leave the heading orphaned at +% the bottom of a page. Gobbling it makes \nobreak the first item following +% the heading box, so the page can only break before the heading +\newcommand*{\@cvheadingitem}[2]{{\let\addvspace\@gobble\cvitem[0ex]{#1}{#2}}} + % makes a resume line 2 headers and their corresponding text % usage: \cvdoubleitem[spacing]{header1}{text1}{header2}{text2} \newcommand*{\cvdoubleitem}[5][.25em]{} diff --git a/moderncvbodyi.sty b/moderncvbodyi.sty index b1155d2..3de93c6 100644 --- a/moderncvbodyi.sty +++ b/moderncvbodyi.sty @@ -78,14 +78,14 @@ \par\addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{section}{#2}% - \cvitem[0ex]{\strut\raggedleft\raisebox{\baseletterheight}{\color{bodyrulecolor}\rule{\hintscolumnwidth}{0.95ex}}}{\strut\sectionstyle{#2}}% + \@cvheadingitem{\strut\raggedleft\raisebox{\baseletterheight}{\color{bodyrulecolor}\rule{\hintscolumnwidth}{0.95ex}}}{\strut\sectionstyle{#2}}% \par\nobreak\addvspace{1ex}\@afterheading}% to avoid a pagebreak after the heading \RenewDocumentCommand{\subsection}{sm}{% \par\addvspace{1ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{subsection}{#2}% - \cvitem[0ex]{}{\strut\subsectionstyle{#2}}% + \@cvheadingitem{}{\strut\subsectionstyle{#2}}% \par\nobreak\addvspace{.5ex}\@afterheading}% to avoid a pagebreak after the heading \renewcommand*{\cvitem}[3][.25em]{% diff --git a/moderncvbodyvi.sty b/moderncvbodyvi.sty index 79b9d3f..58ab97a 100644 --- a/moderncvbodyvi.sty +++ b/moderncvbodyvi.sty @@ -92,7 +92,7 @@ \par\addvspace{1ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{subsection}{#2}% - \cvitem[0ex]{}{\strut\subsectionstyle{#2}}% + \@cvheadingitem{}{\strut\subsectionstyle{#2}}% \par\nobreak\addvspace{.5ex}\@afterheading}% to avoid a pagebreak after the heading \renewcommand*{\cvitem}[3][.25em]{%