# Preventing hyphenation of specific short, repeated, hyphenated words

I'm writing a document where I frequently refer to some experimental cases which are called things like "M-1" and "P-2". It's always a single number, a hyphen, and a single number. In several places, I'm finding these case names broken across two lines with the number and hyphen on the first line and the number on the second line. Is there a way to tell the document never to break these words across two lines aside from using \mbox or \nobreakdash at every instance?

My hope would be to define something globally, like the opposite of the \hyphenation command, specifically for these case names (there's 7 of them). I'd prefer to avoid changing setting which would affect other words in the document, like \exhyphenpenalty=10000.

If using \mbox or \nobreakdash is the best option, what's the difference between the two?

As suggested by Dan and David I tried setting up

\renewcommand{\P}{P\nobreakdash}

(and the same for the other cases, except M, which used \newcommand) So that I would still have the text P-1 in the text for searching purposes. In the text of the document, it works fine. However, sometimes I name the cases in figure captions, and the document has to include a list of figures. The case name shows up fine in the figure caption, but in the list of figures, I get a missing control sequence error. With a caption of:

\caption[For case P-1.]{For case P-1, more words.},

the lof output file reads

\contentsline {figure}{\numberline {4.1}{\ignorespaces For case P\unhbox \voidb@x \toks@ \@emptytoks \def 1{\toks@ {--}\futurelet \@let@token \setbox \z@ \hbox {-\penalty \@M }\unhbox \z@ }\def \setbox \z@ \hbox {-\penalty \@M }\unhbox \z@ {\setbox \z@ \hbox {-\penalty \@M }\unhbox \z@ }\futurelet \@let@token \setbox \z@ \hbox {-\penalty \@M }\unhbox \z@ -1.}}{64}{figure.caption.62}.

The text of the document says "For case P------1" (the first hyphen is sometimes an different character that's the same length, but gray and slightly lower, and sometimes it's a regular hyphen). This is happening for all of the cases, except the L cases. All of which occur before this error in the list of figures. Although I tried changing one of the references to P-1 that was giving this error to L-1, and the error went away for that caption.

• Welcome to TeX.sx! – masu Oct 16 '13 at 20:58
• I don't think there is really anything other than you already suggest, I'd just give a more meaningful name eg \newcommand\expcase[1]{\mbox{#1}} ... \expcase{M-1} – David Carlisle Oct 16 '13 at 21:05
• If there are not too many of these, I'd define commands like \newcommand{\M}[1]{\mbox{M-#1}} and similar for \P. Used like \M{1} and \P{2}. I'm assuming there might be several with the same prefix and just different numbers. – Dan Oct 16 '13 at 21:14
• I like your suggestion, Dan, but there's one problem. One of the cases is S-1 (I use L, M, P, and S). I had tried defining it so I could type \S-1, so I could still search the document for "S-1, if needed. The problem is that \S is already defined as the section sign, §. – David Oct 16 '13 at 21:37
• Probably you are supposed to let @egreg answer, he needs the extra rep:-) – David Carlisle Oct 17 '13 at 0:08

## 2 Answers

You can't have really have P-1 free in the text if you want to avoid line breaks at the explicit hyphen.

You might follow the suggestions in comments to (re)define \P, \M and \S, but I wouldn't recommend it, preferring a prefix:

\usepackage{amsmath} % for \nobreakdash
\DeclareRobustCommand{\hM}{M\nobreakdash}
\DeclareRobustCommand{\hP}{P\nobreakdash}
\DeclareRobustCommand{\hS}{S\nobreakdash}


Note that \nobreakdash doesn't print a hyphen by itself and it must be followed by one, two or three - characters meaning ‘hyphen’, ‘en-dash’ and ‘em-dash’ as usual. So P-1 will be searchable in your LaTeX code.

Here's the test showing that those macros don't cause line breaks at the hyphen.

\documentclass{article}
\usepackage{amsmath} % for \nobreakdash
\DeclareRobustCommand{\hM}{M\nobreakdash}
\DeclareRobustCommand{\hP}{P\nobreakdash}
\DeclareRobustCommand{\hS}{S\nobreakdash}

\begin{document}
\parbox{0pt}{
\hspace{0pt}%
\hM-1 \hP-2 \hS-3

\hspace{0pt}%
\hM--1 \hP--2 \hS--3

\bigskip

\mbox{Compare}

\hspace{0pt}%
M-1 P-2 S-3

\hspace{0pt}%
M--1 P--2 S--3
}
\end{document}


The \hspace{0pt} bits are just to allow TeX try hyphenation on the first word of a paragraph. In a \parbox with zero width, hyphenation will be forced at all feasible places.

You can use \mbox{} for this cause. You might want to define a command or commands to use it (\def or \newcommand).

See the example below:

\documentclass[14pt]{article}

\usepackage[english]{babel}
\usepackage[a6paper]{geometry} % demo only

\begin{document}

\noindent
question question question question question M-3 question question question question

\noindent
question question question question question M-3 question question question question

\noindent
question question question question question \mbox{M-3} question question question question

\noindent
question question question question question M-3 question question question question

\end{document}


PS: if the word did not contain a dash or a number you could have defined a \hyphenation{} without real hyphenation possibilities.

PS: You can see some other methods here.