Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
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! –  Yossi Farjoun Nov 30 '10 at 10:58
    
Thanks, I hadn't thought of that. –  sfranky Nov 30 '10 at 11:05
add comment

1 Answer

up vote 18 down vote accepted

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.

share|improve this answer
    
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 '10 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 '10 at 13:16
    
This will have massive (positive) impact on my latex editing.. It also works within environments! thanks so much! –  sfranky Nov 30 '10 at 13:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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