How can I prevent LaTeX from putting line-breaks into a phrase?
I could replace dashes with "~ and spaces with non-breaking spaces, but I'm looking for a more general solution in case I need other characters in such phrases.
How can I prevent LaTeX from putting line-breaks into a phrase?
I could replace dashes with "~ and spaces with non-breaking spaces, but I'm looking for a more general solution in case I need other characters in such phrases.
Telling TeX not to break lines in long phrases will almost certainly produce bad breaks, with overfull or underfull lines. However, here's a possible hack.
\usepackage[italian]{babel}
\def\phrase{\begin{otherlanguage*}{nohyphenation}%
\dononbreakablespace
\dononbreakablehyphen
\dophrase}
\def\dophrase#1{#1\end{otherlanguage*}}
{\catcode`\ =\active
\gdef\dononbreakablespace{\catcode`\ =\active\def {\nobreakspace}}}
{\catcode`\-=\active
\gdef\dononbreakablehyphen{\catcode`-=\active\def-{\nobreakhyphen}}}
\def\nobreakhyphen{\hbox{-}\nobreak}
Here's an example:
Testo per vedere se gli spazi sono uniformi \phrase{anche in una %
\emph{phrase} che non deve essere spezzata neanche dove-si-potrebbe %
ma solo dopo}, quando finisce.
If you need also en-dashes or em-dashes it's more complicated. With babel we can use its nohyphenation feature. It's handy but not necessary, as the same effect can be obtained in different ways.
The definitions can be read as follows.
The macro \phrase will only have a "user level argument" that actually is an argument to \dophrase; the purpose of \phrase is to open a group and do some initializations, in particular to inhibit hyphenation.
\dophrase reads the argument and closes the otherlanguage* environment, so that also all the other assignments will be undone.
\donobreakablespace activates the space and makes it translate to \nobreakspace. See note below.
\donobreakablehyphen activates the hyphen so that it typesets a hyphen which is "masked" and will not be a permissible break point.
Note. In the argument of \phrase (actually of \dophrase) consecutive spaces will not be reduced to one and will not be ignored at the start of an input line. Moreover line breaks in the input will be admissible break points for TeX. Mask them with % (preceded by a space).
If using babel is not a choice, one can do with some changes
\def\phrase{\begingroup\phrasenohyphen
\dononbreakablespace
\dononbreakablehyphen
\dophrase}
\makeatletter
\def\phrasenohyphen{%
\language\@ifundefined{l@nohyphenation}{\@cclv}{\l@nohyphenation}}
\makeatother
\def\dophrase#1{#1\endgroup}
The other macros can be the same. The "no hyphenation" feature is obtained by choosing explicitly the "nohyphenation" pseudolanguage or, if it's not defined (which may be the case on some old distributions) by choosing \language255 that is probably not defined (so TeX will use an empty set of hyphenation rules).
\nohyphen; moreover in the argument of \nohyphen, explicit hyphens are admissible break points, so there is not really much advantage in using it.
– egreg
Sep 23 '11 at 12:47
The \mbox command does the job.
\mbox prevents internal glue from stretching or shrinking.
– Norman Ramsey
Dec 01 '15 at 20:29
mbox– Marco Daniel Sep 23 '11 at 08:58\mboxyou can't use any linebreaks ;-) – Marco Daniel Sep 23 '11 at 09:08\mboxis that it freezes the spaces, so they won't be stretched or shrinked along with the others on the same line. – egreg Sep 23 '11 at 09:11