diff --git a/CHANGELOG b/CHANGELOG index a10c6b0..223f7bb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,13 @@ version next - Fix spacing between first and last name again (#220) - Make header details flush against sides for casual, classic, and contemporary styles (#229) +- partially fix orphaned section headers for singualar use of \section and \subsection + commands and give users the new adjustable lengths \cvsectionstretchability + and \subsectionstretchability. In case \section and \subsection are used + directly one after the other, an orphaned section header might still occur. + This is structural and should be solved by \newpage instead of increasing + \cvsectionstretchability (#10/#226). + version 2.4.1 (18 Jul 2024) - Fix commons/colors.tex not found in package (#194) diff --git a/manual/moderncv_userguide.pdf b/manual/moderncv_userguide.pdf index 0468a22..ff1a48d 100644 Binary files a/manual/moderncv_userguide.pdf and b/manual/moderncv_userguide.pdf differ diff --git a/manual/moderncv_userguide.tex b/manual/moderncv_userguide.tex index feb6f31..ab0d8c0 100644 --- a/manual/moderncv_userguide.tex +++ b/manual/moderncv_userguide.tex @@ -937,7 +937,7 @@ If \Latex breaks pages just after \code{\\section} or \code{\\subsection} comman \setlength{\cvsubsectionstretchability}{100pt} \end{lstlisting} These two lengths tell \Latex how much extra length it needs after \code{\\section} and \code{\\subsection} commands. -By default, \Moderncv sets both lengths to \code{0.9\\baselineskip}. +By default, \Moderncv sets both lengths to \code{0pt} by default. This should solve orphaned \code{\\section} and \code{\\subsection} commands that are used alone for most users. However, \Latex does not check for content. diff --git a/moderncv.cls b/moderncv.cls index 20cedc8..810e7c6 100644 --- a/moderncv.cls +++ b/moderncv.cls @@ -479,6 +479,36 @@ \newcommand*{\@moderncvstrut}[2]{% \rule[-#1]{0pt}{#2}} +% custom needspace command to avoid orphaned sections +% adapted from the explanations given on +% https://tex.stackexchange.com/questions/348994/understanding-needspace +% the stretchabilities are zero by default and are only there to be changed +% by the user if needed. +\@initializelength{\cvsectionstretchability} +\@initializelength{\cvsubsectionstretchability} +% set the default stretchability. +\setlength{\cvsectionstretchability}{0pt} +\setlength{\cvsubsectionstretchability}{0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +\NewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +\NewDocumentCommand\poststretchpenalty{}{9999} +% command intended to use with section definitions to avoid orphaned sections. (Pagebreak bewteen +% \section{} and cv content e.g. \cvitem{}. +% usage \@cvneedspace{} preferably \cvneedspace{\cvstretchability} to allow the user +% to adapt as needed. +\NewDocumentCommand\@cvneedspace{m}{% + \begingroup + \setlength{\dimen@}{#1}% + \vskip\z@\@plus\dimen@ + \penalty \withinstretchpenalty\vskip\z@\@plus -\dimen@ + \vskip\dimen@ + \penalty \poststretchpenalty% + \vskip -\dimen@ + \vskip\z@skip % hide the previous |\vskip| from |\addvspace| + \endgroup +} %------------------------------------------------------------------------------- % resume design commands definitions diff --git a/moderncvbodyi.sty b/moderncvbodyi.sty index d3db954..39172e7 100644 --- a/moderncvbodyi.sty +++ b/moderncvbodyi.sty @@ -74,7 +74,17 @@ \@initializelength{\baseletterheight} \settoheight{\baseletterheight}{\sectionstyle{o}} \setlength{\baseletterheight}{\baseletterheight-0.95ex} + +% leave the stretchability zero by default +% \setlength{\cvsectionstretchability}{0.0pt} +% \setlength{\cvsubsectionstretchability}{0.0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +% \RenewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% \RenewDocumentCommand\poststretchpenalty{}{9999} \RenewDocumentCommand{\section}{sm}{% + \@cvneedspace{\cvsectionstretchability}% avoid orphaned sections \par\addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{section}{#2}% @@ -82,6 +92,7 @@ \par\nobreak\addvspace{1ex}\@afterheading}% to avoid a pagebreak after the heading \RenewDocumentCommand{\subsection}{sm}{% + \@cvneedspace{\cvsubsectionstretchability}% avoid orphaned sections \par\addvspace{1ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{subsection}{#2}% diff --git a/moderncvbodyiii.sty b/moderncvbodyiii.sty index 37d8d13..dc1d132 100644 --- a/moderncvbodyiii.sty +++ b/moderncvbodyiii.sty @@ -107,7 +107,16 @@ % regular lengths \setlength{\parskip}{0\p@}} +% leave the stretchability zero by default +% \setlength{\cvsectionstretchability}{0.0pt} +% \setlength{\cvsubsectionstretchability}{0.0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +% \RenewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% \RenewDocumentCommand\poststretchpenalty{}{9999} \RenewDocumentCommand{\section}{sm}{% + \@cvneedspace{\cvsectionstretchability}% avoid orphaned sections \par\addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{section}{#2}% @@ -123,6 +132,7 @@ \par\nobreak\addvspace{1ex}\@afterheading} \RenewDocumentCommand{\subsection}{sm}{% + \@cvneedspace{\cvsubsectionstretchability}% avoid orphaned sections \par\addvspace{1ex}% \phantomsection{}% \addcontentsline{toc}{subsection}{#2}% diff --git a/moderncvbodyiv.sty b/moderncvbodyiv.sty index 0d9fe6f..039a0ef 100644 --- a/moderncvbodyiv.sty +++ b/moderncvbodyiv.sty @@ -69,7 +69,16 @@ % regular lengths \setlength{\parskip}{0\p@}} +% leave the stretchability zero by default +% \setlength{\cvsectionstretchability}{0.0pt} +% \setlength{\cvsubsectionstretchability}{0.0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +% \RenewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% \RenewDocumentCommand\poststretchpenalty{}{9999} \RenewDocumentCommand{\section}{sm}{% + \@cvneedspace{\cvsectionstretchability}% avoid orphaned sections \par\addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{section}{#2}% @@ -77,6 +86,7 @@ \par\nobreak\addvspace{1ex}\@afterheading} \RenewDocumentCommand{\subsection}{sm}{% + \@cvneedspace{\cvsubsectionstretchability}% avoid orphaned sections \par\addvspace{1ex}% \phantomsection{}% \addcontentsline{toc}{subsection}{#2}% diff --git a/moderncvbodyv.sty b/moderncvbodyv.sty index f29e577..1e4a417 100644 --- a/moderncvbodyv.sty +++ b/moderncvbodyv.sty @@ -72,8 +72,18 @@ % regular lengths \setlength{\parskip}{0\p@}} +% leave the stretchability zero by default +% \setlength{\cvsectionstretchability}{0.0pt} +% \setlength{\cvsubsectionstretchability}{0.0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +% \RenewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% \RenewDocumentCommand\poststretchpenalty{}{9999} + \@initializeif{\if@aftersection}\@aftersectionfalse% \RenewDocumentCommand{\section}{sm}{% + \@cvneedspace{\cvsectionstretchability}% avoid orphaned sections \addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{section}{#2}% @@ -83,6 +93,7 @@ %\@initializeif{\if@aftersubsection}\@aftersubsectionfalse% \RenewDocumentCommand{\subsection}{sm}{% + \@cvneedspace{\cvsubsectionstretchability}% avoid orphaned sections %\addvspace{1ex}% \vspace*{-\arrayrulewidth}% HACK; I don't understand where the space is coming from, nor what it's exact value is :( \if@aftersection% diff --git a/moderncvbodyvi.sty b/moderncvbodyvi.sty index 76e1a15..74a5ce9 100644 --- a/moderncvbodyvi.sty +++ b/moderncvbodyvi.sty @@ -79,7 +79,18 @@ \setlength{\baseletterheight}{\baseletterheight-0.95ex} % The optional argument can be used to place a small icon near the section name. % E.g. `\section[\faBookmark]{Education}` +% +% +% leave the stretchability zero by default +% \setlength{\cvsectionstretchability}{0.0pt} +% \setlength{\cvsubsectionstretchability}{0.0pt} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% This is where the page break should occur, so this number should not bee too high +% \RenewDocumentCommand\withinstretchpenalty{}{0} +% must be between -100 and 9999. The higher the less likely a page break will occur. +% \RenewDocumentCommand\poststretchpenalty{}{9999} \RenewDocumentCommand{\section}{sO{}m}{% + \@cvneedspace{\cvsectionstretchability}% avoid orphaned sections \tl@resetchain% \par\addvspace{2.5ex}% \phantomsection{}% reset the anchor for hyperrefs @@ -89,6 +100,7 @@ \par\nobreak\addvspace{1ex}\@afterheading}% to avoid a pagebreak after the heading \RenewDocumentCommand{\subsection}{sm}{% + \@cvneedspace{\cvsubsectionstretchability}% avoid orphaned sections \par\addvspace{1ex}% \phantomsection{}% reset the anchor for hyperrefs \addcontentsline{toc}{subsection}{#2}%