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:
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
\subsection*, you need to change just the macro definition, not the whole text.