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.

What is the difference between \nobreak and \nolinebreak?

I noticed that a lot of solutions in TeX.SE use the former, but I thought the latter is LaTeX while the former is plain TeX. And LaTeX macros are often more fleshed-out versions of the more rudimentary TeX equivalents. Which is better for which situations?

share|improve this question
add comment

2 Answers

up vote 30 down vote accepted

\nobreak is defined in plain TeX (and given the same definition in LaTeX) as

\penalty \@M

which is \penalty 10000 which is an infinitely bad place to break (a line if in horizontal mode or a page if in vertical mode).

\nolinebreak Is a LaTeX command that tests that it is in horizontal mode, and gives an error message if not, and then can give one of 5 different penalty values giving different strengths of hint \nolinebreak[0] to ... \nolinebreak[4] 4 is the default and gives \@M the same as \nobreak. The actual penalties inserted for values 0–4 are set by the class file.

The values are [0] is penalty 0, 4 is penalty 10000 and 1–3 are set (in article class) by

\@lowpenalty   51
\@medpenalty  151
\@highpenalty 301

There is also some code to fix white space so that

a \nolinebreak[0] b
a\nolinebreak[0] b
a \nolinebreak[0]b

each get exactly one word space between a and b if no break happens.

So in the usual case the two commands do the same thing. The LaTeX one takes more time and because of the optional argument testing is fragile. So since most LaTeX2e class files use quite low level code you usually see \nobreak or even just \penalty\@M in class files but in documents it is better to use the LaTeX form usually for the extra error checking and the interface to standard levels of penalty hint set by the class.

share|improve this answer
add comment

The \nobreak command is not documented in the LaTeX manual nor in the "LaTeX Companion". Its meaning is just \penalty10000 which makes it useful both in horizontal and vertical mode, but this ubiquity makes it a bit dangerous.

The meaning of \nolinebreak is "look if there's an optional argument and, if not, call the inner macro \@no@lnbk with argument 4", which means doing

\ifvmode
  \@nolnerr
\else
  \@tempskipa\lastskip
  \unskip
  \penalty\@M
  \ifdim\@tempskipa >\z@
    \hskip\@tempskipa\ignorespaces
  \fi
\fi

which checks whether we are in horizontal mode and does a bit of juggling: if \nolinebreak is preceded by a space (a normal interword space or an explicit \hspace), it is removed, the penalty is issued and the space is reinserted.

A bad habit (of which I admit to be guilty) is to use the shorter command when it's sure that it will do the right thing. For instance,

$a=\nobreak b$

is faster than $a=\nolinebreak b$ because there's no check to be performed and no space to be shifted.

One can also find \nobreak just after a \par, meaning that a vertical space after \par\nobreak will never be used for a page break.

Of course it's better to use \par\nopagebreak in this case. But, alas, bad habits prevail. Use \nopagebreak.

(I promise I'll be more LaTeX savvy in the future.)

share|improve this answer
8  
+1 (even though I like my answer better, you need the rep:-) I am sure the community will hold you to that promise. –  David Carlisle Jan 17 '13 at 13:33
1  
@DavidCarlisle Yeah, let's report user egreg to the TeX police for every offense spotted! (just kidding) –  Lover of Structure Jan 18 '13 at 2:39
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.