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!

  • Welcome to TeX & CO! I hope you enjoy your stay. Just remember: there's no need to sign your name, as your badge appears automatically on the lower right! Nov 30, 2010 at 10:58

1 Answer 1


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

  • this looks great! I ll try it today.. only the thing is, I've already written the whole thing, so it's a little late for writing a newcommand now. Also, does it matter that my solution line is between two \medskip's ?!
    – sfranky
    Nov 30, 2010 at 13:10
  • The \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.
    – Stefan Kottwitz
    Nov 30, 2010 at 13:16
  • This will have massive (positive) impact on my latex editing.. It also works within environments! thanks so much!
    – sfranky
    Nov 30, 2010 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .