15

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.

Tim N
  • 10,219
  • 13
  • 63
  • 88

2 Answers2

13

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.

  1. 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.

  2. \dophrase reads the argument and closes the otherlanguage* environment, so that also all the other assignments will be undone.

  3. \donobreakablespace activates the space and makes it translate to \nobreakspace. See note below.

  4. \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).

egreg
  • 1,121,712
  • You code is very nice but you should also recommend the package hyphenat. – Marco Daniel Sep 23 '11 at 12:39
  • @Marco hyphenat doesn't provide an environment form of \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 point isn't the advantages or disadvantages. It is a further possibility ;-) (You know what I meean?) – Marco Daniel Sep 23 '11 at 12:49
  • @Marco IMO it doesn't fit at all with the problem, which is mainly not to break at spaces. – egreg Sep 23 '11 at 12:57
11

The \mbox command does the job.

Leonardo
  • 1,772