Take the 2-minute tour ×

Sometimes I would like to tell latex to do something after it finishes the line currently being typeset. For example, one might want to use the command \needspace{4\baselineskip} (assuming the needspace package is loaded) in the middle of a paragraph to prevent a pagebreak after the current paragraph. If done naively, this could end the current paragraph prematurely.

I know that some standard latex commands like \vspace automatically wait until the end of the current line before executing. Is there a way to mimic this behavior for other commands?

Note: Personally, I don't think a MWE is really helpful in this case. However, since I am sure someone will ask for one if I don't provide one, here is a not-entirely-minimal working example:




This is an exercise that involves an equation, which is printed below.
Please study the equation and somehow do something in the Solution box,
which may or may not be filled in depending on which line is commented out.
Please study the equation and\needspace{4\baselineskip}\noindent somehow do something in the Solution box,
share|improve this question
You'd like that the whole proof is contained in the page or the line after which you put the "adjustment" goes to the next page? –  egreg Dec 10 '12 at 22:39
@egreg The latter. But really, I'd like an answer to this question, not a solution to the example that does not involve answering the question. Other uses for the actual question might include wrapfigure, which by default dictates that the line write before it must break. –  Charles Staats Dec 10 '12 at 22:43
@egreg: If you are interested in how to make this example work, see my answer to the question tex.stackexchange.com/q/28203/484 –  Charles Staats Dec 10 '12 at 23:40
add comment

1 Answer

up vote 7 down vote accepted

Basic answer is no. Donald Arseneau's magaz package goes to heroic lengths to apply different formatting to the first line of a paragraph. see Why does color meant for first typeset line ONLY (with package magaz) affect ENTIRE document? doing that for an arbitrary point in the paragraph would be even harder.

Note that \vspace is not executed after the line, it is executed at the point it appears but re-inserted (after the entire paragraph has been typeset) after the line that contained the original space command. (marks and inserts such as figures work similarly).

The basic point is that that the entire paragraph is set essentially as a single line, by the time the paragraph breaker is invoked to break the paragraph into lines, all macros (and assignments) have been done. The linebreaker just deals with boxes.

Note luatex provides callbacks that probably invalidate the above answer.

Note you can define commands like \vspace that insert vertical material between lines, but these are executed at the wrong place to do any kind of decisions based on the linebreaking, for example \vadjust{\hbox{hello}} will insert hello after the current line.

share|improve this answer
Maybe tex.stackexchange.com/questions/79900/… can help a bit. Check the link on chickenize. –  La Raison Dec 10 '12 at 22:54
@LaRaison yes Thanks that amplifies the luatex comment I made. –  David Carlisle Dec 10 '12 at 22:56
add comment

Your Answer


