Document Classesmoderncv: how to "repair" section definition

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
mychi
Posts: 7
Joined: Tue Mar 02, 2010 4:54 am

moderncv: how to "repair" section definition

Post by mychi »

Unfortunately the sections in the (otherwise great) moderncv package don't behave like normal sections should. For example do section titles not stick to their "bodies", which results in section titles standing alone on the bottom of a page.

Here is an example to see what I mean:

Code: Select all

\documentclass[11pt,a4paper]{moderncv}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\firstname{John}
\familyname{Doe}

% how to cleanly renew this command so that it has most of the properties of a normal
% latex-section (e.g. not leaving a sect.-heading alone on the bottom of a page)?
\makeatletter
\renewcommand*{\section}[1]{%
	\vspace*{2.5ex \@plus 1ex \@minus .2ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{part}{#1}%
  \parbox[m]{\hintscolumnwidth}{%
	\raggedleft\hintfont{\color{sectionrectanglecolor}\rule{\hintscolumnwidth}{1ex}}%
  }%
  \hspace{\separatorcolumnwidth}%
  \parbox[m]{\maincolumnwidth}{\sectionstyle{#1}}\\[1ex]}
\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\section{Lipsum}

\lipsum[2-8]
\section{Test}
\lipsum[1]
\end{document}
The \renewcommand part is copied from moderncv.cls, so you should get the same result regardless of whether this part is there or not. I just put it in so to maybe find out what could be improved to get the desired behaviour.

Does anyone have any clue? Even if I put a \nopagebreak after the \section{Test} it does break the page there.

(Additionally it seems to make a difference whether there is a blank line after the section title or not, as with \section{Lipsum})

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moderncv: how to "repair" section definition

Post by localghost »

You could take measures for the prevention of widows and orphans that should also affect the section headings.

Code: Select all

\clubpenalty10000
\widowpenalty10000
Another very simple measure would be just to insert a page break right before the concerned section.


Best regards and welcome to the board
Thorsten
mychi
Posts: 7
Joined: Tue Mar 02, 2010 4:54 am

Re: moderncv: how to "repair" section definition

Post by mychi »

Thanks for your suggestion. Unfortunately both of them don't work as expected.
Inserting \clubpenalty10000 and \widowpenalty10000 does not change anything in the specified example (does it on your setup?).

Inserting a \pagebreak works, but unfortunately still causes a pagebreak if the first section is short enough to allow the whole following section to be on the same page. (Change \lipsum[2-8] to \lipsum[2] in the example and there's still a pagebreak).

:-/
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: moderncv: how to "repair" section definition

Post by localghost »

Your example doesn't represent the real situation when composing a CV with that class. I didn't face any problems with this class when typesetting my CV. I suggest that you start with the sample documents delivered with the class and see if the problems persist. At the moment I'm not able to comprehend the described problem.
mychi
Posts: 7
Joined: Tue Mar 02, 2010 4:54 am

Re: moderncv: how to "repair" section definition

Post by mychi »

Actually I have this problems with my own CV and I tried to provide a minimalistic example to make it easier to pinpoint the problem. But I'll try to provide an example based on one of the sample documents, or based on my own CV. Thanks so far for your help!

Best regards
Mike
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moderncv: how to "repair" section definition

Post by localghost »

Just to supplement my last reply. I have no problem with the code below. The section »Test« moves to the next page.

Code: Select all

\documentclass[11pt,a4paper]{moderncv}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}
\firstname{John}
\familyname{Doe}

% how to cleanly renew this command so that it has most of the properties of a normal
% latex-section (e.g. not leaving a sect.-heading alone on the bottom of a page)?
\makeatletter
\renewcommand*{\section}[1]{%
   \vspace*{2.5ex \@plus 1ex \@minus .2ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{part}{#1}%
  \parbox[m]{\hintscolumnwidth}{%
   \raggedleft\hintfont{\color{sectionrectanglecolor}\rule{\hintscolumnwidth}{1ex}}%
  }%
  \hspace{\separatorcolumnwidth}%
  \parbox[m]{\maincolumnwidth}{\sectionstyle{#1}}\\[1ex]}
\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\section{Lipsum}

\lipsum[2-8]

\pagebreak
\section{Test}
\lipsum[1]
\end{document}
mychi
Posts: 7
Joined: Tue Mar 02, 2010 4:54 am

Re: moderncv: how to "repair" section definition

Post by mychi »

Yes, that's what I said in my reply, it does break the page at the position of the \pagebreak command. But as soon as the preceding section gets shorter (change \lipsum[2-8] to \lipsum[2]) the page breaks although it doesn't have to.
So \pagebreak is forcing a pagebreak.

From another point of view my question is: where does the redefinition of \section "destroy" the behaviour of a normal \section? Or: could \section be redefined differently, such that it doesn't differ as much from a normal latex-\section?

After all it's just the appearance (blue, with a bar in front of the title and no numbering) that needs to be redefined..
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moderncv: how to "repair" section definition

Post by localghost »

mychi wrote:[...] So \pagebreak is forcing a pagebreak. [...]
Of course it does. Just omit it when not needed. But now it seems that I gradually understand what you mean. (It's been a long day so I'm slow to catch on at the moment.)

The definition of the section command in this class is completely different from that in the standard classes and doesn't rely on the basic structures. That's why it behaves in this way. Perhaps it is possible to merge parts of the code from the class and the usual definition. But at the moment I can't figure out the details. Perhaps somebody else can.

This may be worth a note to the maintainer. But I see no problem to use a \newpage or \pagebreak where applicable. It can be simply commented where it is not useful. If you contact the maintainer, feel free to report new insights here.
mychi
Posts: 7
Joined: Tue Mar 02, 2010 4:54 am

Re: moderncv: how to "repair" section definition

Post by mychi »

Glad to hear that I could make myself clear now :-)

Of course it's not such a big deal in this case, because a CV usually doesn't/shouldn't have too many pages anyway. But it's still something additional you have to care about, that's why I tried to find a solution -- but apparently it's not that easy.

Regarding \pagebreak, I thought that \newpage forces a pagebreak while \pagebreak just "suggests" a pagebreak. But obviously I misremembered that.

I'll contact the maintainer about this eventually this week but I'm still happy about further replies/ideas of course.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

moderncv: how to "repair" section definition

Post by localghost »

mychi wrote:[...] Regarding \pagebreak, I thought that \newpage forces a pagebreak while \pagebreak just "suggests" a pagebreak. But obviously I misremembered that. [...]
The \pagebreak command accepts an optional argument to control its behaviour. Without this option the priority for a page break is set to the highest possible value. The \newpage command forces a page break.
Post Reply