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}
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?
Best Answer
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 theminipage
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:
But, I repeat, don’t do that: use
titlesec
.