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.

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.

share|improve this question
5  
Put the phrase in a mbox –  Marco Daniel Sep 23 '11 at 8:58
    
Thanks. Does it effect anything else that I should be aware of? –  Tim N Sep 23 '11 at 9:00
2  
Inside a \mbox you can't use any linebreaks ;-) –  Marco Daniel Sep 23 '11 at 9:08
5  
The effect of \mbox is 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 9:11
1  
@egreg: Than that's not what I'm looking for. The only effect I'm looking for is non-breaking; they should still be formatted nicely. –  Tim N Sep 23 '11 at 9:18

2 Answers 2

up vote 10 down vote accepted

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

share|improve this answer
    
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

The \mbox command does the job.

share|improve this answer

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.