20

On the title page of an article I want to insert a paragraph containing keywords. For indexing purposes (e. g. web search engines), I'd like to have the keywords unhyphenated (temporarily, of course). At the same time, the paragraph should be flushed at both sides, even though inter-word spaces may become larger than usual.

How can I achieve that?

AlexG
  • 54,894
  • 5
    \begin{hyphenrules}{nohyphenation}Text\end{hyphenrules}, if you're using babel. – egreg Jan 11 '13 at 11:41
  • 2
    @egreg: Could you please turn this into an answer? – AlexG Jan 11 '13 at 11:45
  • 1
    @egreg: Placing \begin{sloppypar} and \end{sloppypar} around the non-hyphenated paragraph seems to be necessary, as Herbert and David pointed out. – AlexG Jan 11 '13 at 12:56

4 Answers4

21

Hyphenation can be disabled in different ways. One is to set

\hyphenpenalty=10000
\exhyphenpenalty=10000

that assign "infinite" cost to discretionary and explicit hyphens respectively.

Another way is to choose a language with no hyphenation patterns, which is more efficient, because it spares TeX from computing demerits.

All modern TeX distributions have already such a language in their predefined set, and it's called nohyphenation. If you're using babel, then

\begin{hyphenrules}{nohyphenation}
<text>
\end{hyphenrules}

will allow line breaking only at explicit hyphens. If you want to disable also them, then setting \exhyphenpenalty=10000 is still necessary. However, one has to cope with paragraph setting, allowing larger interword spaces without too many Underfull \hbox messages, which is obtained by saying \sloppy.

A "complete" solution might be to define a nohyphens environment so that one can type

\begin{nohyphens}
Text not to be hyphenated
\end{nohyphens}

in the following way

\makeatletter
\@ifpackageloaded{babel}
  {\newenvironment{nohyphens}
     {\par\sloppy\exhyphenpenalty=\@M
      \@ifundefined{l@nohyphenation}
        {\language=\@cclv}
        {\hyphenrules{nohyphenation}}%
     }
     {\par
      \@ifundefined{l@nohyphenation}
        {}
        {\endhyphenrules}%
     }
  }
  {\newenvironment{nohyphens}
     {\par\sloppy\exhyphenpenalty=\@M
      \@ifundefined{l@nohyphenation}
        {\language=\@cclv}
        {\language=\l@nohyphenation}%
     }
     {\par}
  }
\makeatother

If babel is loaded we use its features, otherwise we simply set the current language to the "no hyphenation" one, if existent; in the worst case we set the language number to 255, which shouldn't be defined (TeX formats usually assign progressive numbers to the enabled languages and having 256 of them is highly unlikely).

Note that assigning \language=-1 is not a solution: the TeXbook says, at p. 455,

If \language is negative or greater than 255, TeX acts as if \language = 0

egreg
  • 1,121,712
13

You can make hyphens infinitely expensive

  \hyphenpenalty=10000

or use a language without any hyphenation patterns loaded

  \newlanguage\nohy
  \language=\nohy

Then you might want to use \sloppy or \begin{sloppypar}...\end{sloppypar} so the word spacing can stretch more than usual.

David Carlisle
  • 757,742
  • Thank you very much for this explanation! I'd prefer @egreg 's approach, though. It is more LaTeX-style. – AlexG Jan 11 '13 at 11:51
  • 1
    Yes but it requires having read the babel manual which is cheating I'm sure:-) Note you may still want to use sloppy(par) to handle white space, the babel declaration just turns off hyphenation I think. – David Carlisle Jan 11 '13 at 11:56
  • afaict from experimenting with hyphenrules, the paragraph is flushed to both sides. This implies larger inter-word space I guess, right? – AlexG Jan 11 '13 at 12:01
  • 2
    Yes it doesn't change the justification but without relaxing the normal rules so TeX may have to stretch space too much or it may stick text into the margin (both of these are infinitely bad) \sloppy tells tex that stretching white space isn't so bad so it will do that in preference to having over-full boxes (and it will not complain about boxes being underfull due to over-stretched space) – David Carlisle Jan 11 '13 at 12:09
  • …and then \selectlanguage{english} when ready to resume hyphenation. – Micah Walter Oct 07 '14 at 15:51
10

it makes sense to put the text into \begin{sloppypar}...\end{sloppypar} when it is not hyphenated.

\listfiles
\documentclass[ngerman]{article}
\usepackage{libertine}
\usepackage{babel}
\usepackage{blindtext}
\textwidth=10cm
\begin{document}
\blindtext

\foreignlanguage{nohyphenation}{\begin{sloppypar}\blindtext\end{sloppypar}}

\blindtext
\end{document}

enter image description here

0

Individual words can be excluded from hyphenation by using the \mbox{} command. This also works for dates or multiple words. For instance:

The following word will not hyphenate: \mbox{keywordnottohyphenate}. Neither will the following date: \mbox{2020-08-23}

\mbox is a standard LaTeX command, so should work without any packages to include in your preamble.

gzalm
  • 1