Is there a way I can have LaTeX insert a pagebreak, if the last line of the page is a certain string? e.g.
\noindent \textsl{Solution}:
thanks very much!
Is there a way I can have LaTeX insert a pagebreak, if the last line of the page is a certain string? e.g.
\noindent \textsl{Solution}:
thanks very much!
For such cases I like to use the needspace
package. It provides commands that request customizable amount of space left on the page, and if there's not enough space then a page break would be inserted.
You could define a macro:
\usepackage{needspace}
\newcommand*{\solution}{%
\Needspace{2\baselineskip}%
\noindent\textsl{Solution}:%
}
This will have the effect, that a page break would be inserted before "Solution" if this would be the last line on the page, like you desired. You may increase the value in the argument of \Needspace
if more space seems more appropriate.
The package defines further command variants \needspace
(a bit more relaxed regarding the space amount) and \Needspace*
, which respects a \flushbottom
configuration in contrary to the unstarred command \Needspace
, which causes a ragged bottom.
In every case I recommend to create a macro \solution
which you may use later in your document. The reason is logical formatting - if you later decide to use \textbf
instead of \textsl
or a command like \section*
or \subsection*
, you need to change just the macro definition, not the whole text.
\medskip
commands make no big difference, the macro works the same. I just would request more space then, like \Needspace{3\baselineskip}
. I guess search&replace of your editor would work, replacing all occurences of \noindent \textsl{Solution}:
by \solution
.