# What is the ConTeXt or plain TeX equivalent of \nolinebreak?

The command \nolinebreak[4], if placed between two characters, should prevent a line break from appearing in that location. This command does not seem to be defined in ConTeXt. Is there an equivalent for this in ConTeXt, or for plain TeX which is also compatible with ConTeXt?

-

In plain TeX there is \nobreak, equivalent to \penalty 10000. In ConTeXt, it's the same (defined in the file spac-ver.mkiv).

-
This seems to have no effect, if placed after a macro. E.g. \macro{Some text}\nobreak A. How is this done after a macro? –  Village Apr 7 '12 at 8:40
@Village: probably doesn't depend on there being a macro: with \def\macro#1{#1} you get the same as Some text\nobreak A. What macro are you testing with? –  Bruno Le Floch Apr 7 '12 at 9:07
If you \macro ends with e.g. a skip, you might need to either redefine it, or say e.g. \def\reallynobreak{\unskip\penalty 10000} –  mbork Apr 7 '12 at 9:46

The definition of \nolinebreak in LaTeX is, skipping over the details,

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


The first conditional simply issues an error if \nolinebreak is found in vertical mode. In horizontal mode it stores the last skip (if there isn't one the stored value is zero) and removes it (if present). Then a penalty of 10000 is issued and the skip is reinstated.

Just use a temporary skip register instead of \@tempskipa, for instance \skip8. If you need the full syntax \nolinebreak[<number>] then something more complicated has to be used.

\catcode@=11
\def\nolinebreak{\futurelet\next\nolinebreak@a}
\def\nolinebreak@a{%
\ifx\next[\expandafter\nolinebreak@b\else\nolinebreak@b[4]\fi}
\def\nolinebreak@b[#1]{%
\ifvmode\else
\skip8=\lastskip\unskip
\penalty\ifcase#1 0 \or 2500 \or 5000 \or 7500 \or \@M\fi % change values at will
\ifdim\skip8>\z@\hskip\skip8 \fi
\ignorespaces
\fi}
\catcode@=12


The trick is that glue isn't a feasible for line breaking if it follows a penalty. For example, Plain TeX defines ~ as

\def~{\penalty\@M \ }


(where \@M stands for 10000).

-