0

I'm among many it seems that is trying to prevent an inappropriate pagebreak. In this case, I'm getting a pagebreak between my section and subsequent subsection. I know if I remove the \hrule and \hfill then LaTeX will pull them together, but with the line between, the subsection is orphaned to the next page.

Here's a MWE:

\documentclass{article}
\begin{document}
Lots\\of\\text\\here\\to\\fill\\space\\so\\that\\the\\new\\section\\is\\right\\near\\the\\bottom.\\
Almost\\there.\\
I\\bet\\there's\\a\\better\\way\\to\\do\\this...\\
Hopefully\\someday\\I'll\\know\\how\\but\\for\\now\\I'm\\stuck\\with\\this\\mess.\\

\section*{Section title}
\hrule
\hfill
\subsection*{\underline{Subsection title}}

\end{document}

break

I don't want a hard-coded \pagebreak right before the section because the amount of text above it will vary. I tried wrapping all four lines in \begin{samepage} and \end{samepage} but it didn't seem to help.

How can I make sure the subsection is not orphaned on the next page?

  • 1
    Why the \hfill? And is the \hrule part of the section layout? – Ulrike Fischer Jul 23 '16 at 16:59
  • When \hfill is absent and the whole thing is not straddling the page break, then the subsection is too close to the \hrule line and it looks ugly. I have the \hrule line in the first place to make the underline across the whole page under all the section headers. It's aesthetically pleasing to me. – CephBirk Jul 23 '16 at 18:28
1

Your approach is not what I’d recommend: if you want to customize the appearance of sectioning titles, use, for example, the titlesec package. However, even if you insist for a do-it-yourself solution, there’s no need to have recourse to the minipage environment: it suffices to insert penalty items at the right spots, which can be done by means of the \nobreak commands. There is one further point to note, though: the fact that you say \hfill causes TeX to start a new paragraph, and this resets the \if@nobreak switch to \iffalse, so that a penalty of -300 is inserted above the \subsection title. To put it simply, you make TeX think that a paragraph intervene between the \section and the \subsection, so it will no longer treat the two as consecutive sectioning titles, between which a page break is prohibited. If you are saying \hfill in order to get an empty line in the output, please note that this is not the way to go.

Here is an example of how your code could be fixed:

\documentclass{article}
\begin{document}
Lots\\of\\text\\here\\to\\fill\\space\\so\\that\\the\\new\\section\\is\\right\\near\\the\\bottom.\\
Almost\\there.\\
I\\bet\\there's\\a\\better\\way\\to\\do\\this...\\
Hopefully\\someday\\I'll\\know\\how\\but\\for\\now\\I'm\\stuck\\with\\this\\mess.\\

\makeatletter

\section*{Section title}
\hrule
% \hfill % why do you use "\hfill" here?
\nobreak\vspace{\baselineskip} % is it this what you want?
\subsection*{\underline{Subsection title}}
Some text after the title.

% \showboxbreadth = 1000
% \showboxdepth = 10
% \showlists

\end{document}

But, I repeat, don’t do that: use titlesec.

3

You try putting the items you do not want to be on separate pages inside minipage since minipage do not break across pages, all its content is forced to be on one page.

\documentclass{article}
\begin{document}
Lots\\of\\text\\here\\to\\fill\\space\\so\\that\\the\\new\\section\\is\\right\\near\\the\\bottom.\\
Almost\\there.\\
I\\bet\\there's\\a\\better\\way\\to\\do\\this...\\
Hopefully\\someday\\I'll\\know\\how\\but\\for\\now\\I'm\\stuck\\with\\this\\mess.\\

\begin{minipage}{\textwidth}
\section*{Section title}
\hrule
\subsection*{\underline{Subsection title}}
\end{minipage}
text    
\end{document}

Here is the difference. The left uses minipage, the right does not.

Mathematica graphics

You could also try samepage but I had problems using samepage before and do not use it any more.

Below is the output using \hrule\hfill also to compare, using minipage also

Mathematica graphics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.