89

In the following minimal test document, I can have two hyphens appear as two hyphens (which is what I want):

\documentclass[letterpaper]{article}

\begin{document}
A hyphen: \texttt{-}

Two hyphens: \texttt{--}

Three hyphens: \texttt{---}

\end{document}

But in this second test document where I load the fontenc package, I get different behavior; the single hyphen is displayed; the double hyphen is collapsed to a single hyphen, and the triple hyphen is collapsed to a double hyphen:

\documentclass[letterpaper]{article}
\usepackage[T1]{fontenc}

\begin{document}
A hyphen: \texttt{-}

Two hyphens: \texttt{--}

Three hyphens: \texttt{---}
\end{document}

I read that loading fontenc is a Good Idea, so I want to do that; but I want my double hyphen too. Have I missed something terribly obvious?

System: MiKTeX 2.8 on Windows XP Pro.

4 Answers4

101

-- is a so-called TeX-Ligature which is converted into the n-dash. -{}- breaks this combination.

\documentclass[letterpaper]{article}
\usepackage[T1]{fontenc}

\begin{document}
A hyphen: \texttt{-}

Two hyphens: \texttt{-{}-}

Three hyphens: \texttt{-{}-{}-}
\end{document}

or define own macros for this

  • 7
    @Herbert: An explanation would be nice. – Hendrik Vogt Jan 27 '11 at 17:53
  • 2
    It's quite easy. LaTeX sees two hyphens as a single token representing another sort of hyphen. By using a {} token. You say to LaTeX things before and after the {} chars have another context. TeX can only read multiple chars as one token if they are in the same context. The details can be read in "the TeXbook" and "TeX the program" of Donald E. Knuth. – willeM_ Van Onsem Jan 27 '11 at 18:06
  • 1
    I still don't understand why the difference between the first minimal example, which doesn't load fontenc and which does output multiple hyphens, and the second minimal which does load fontenc and collapses the hyphens. – Mike Broschinsky Jan 27 '11 at 18:10
  • 1
    Fontenc specifies the way how it has been read. But for consistent TeX, you should use the '{}' patterns. With '{}' you say to TeX "All right, forget everything about the past (except labels,...), and take a new look at this". When you type "--" TeX will see this as a pattern for a longer hyphen. With '{}' between it, it will have forgotten that there was already a hyphen. TeX knows four different types of hyphens: http://en.wikibooks.org/wiki/LaTeX/Formatting#Dashes_and_Hyphens – willeM_ Van Onsem Jan 27 '11 at 18:18
  • I must be a little slow... I still think fontenc must have something to do with it because I can get the double hyphen using \texttt{--} without the {} in between the hyphens if I don't load fontenc. But the conclusion I draw from what you have said (and I may be wrong; just trying to understand) is that I should always have to insert the {} between two hyphens, even if in the \texttt{} so that the hyphens don't collapse? And it's not that they hyphens are becoming either en- or em-dashes, it's that the second one hyphen seems to disappear. – Mike Broschinsky Jan 27 '11 at 18:30
  • Well I've tested it on my Linux machine (pdfTex version 3.1415926-1.40.10). And the hyphens also collapsed with(out) fontenc. – willeM_ Van Onsem Jan 27 '11 at 18:36
  • Ah, perhaps a version issue then would explain our different experience? When I run pdflatex --version, the version number returned is 2.8.3759. – Mike Broschinsky Jan 27 '11 at 18:48
  • important is only if your current font has the ligatures endash and emdash. If yes, than -- and --- are converted to their ligature, cmtt in OT1 has no such characters –  Jan 27 '11 at 18:51
  • @CommuSoft: and what typewriter font was used, is listed at the end of your logfile. –  Jan 27 '11 at 18:58
  • @Mike: which TeX distribution do you have installed? –  Jan 27 '11 at 19:07
  • This is what I have learned so far: 1) On my box, loading fontenc seemed to make a difference; 2) The hyphens weren't being converted to en-dashes or em-dashes, one hyphen was simply disappearing; and 3) (a test I ran just now) \texttt{} and \verb are not the same thing, for with \verb+--+ I get the two hyphens as I want. So maybe all of this was more of an issue of my using the wrong command for the wrong purpose, but still puzzling to me. Clearly, I need to learn more about (La)TeX. I hope I wasn't a waste of time. I'm grateful for your help. – Mike Broschinsky Jan 27 '11 at 19:12
  • @Herbert: MiKTeX 2.8 – Mike Broschinsky Jan 27 '11 at 19:21
  • 1
    @Mike: \verb is used, when you exactly want the same as the argument in typewriter or any other defined font. \texttt is used, when you want the same characters as in the default text but only in typewriter. 2.8.3759 seems the version number of MiKTeX, but not of pdftex. However, that is anyway not a problem. –  Jan 27 '11 at 19:23
  • @Herbert: Thanks. That's a helpful distinction. I expect I'll have more questions... :) – Mike Broschinsky Jan 27 '11 at 19:37
  • Why is it necessary to use \texttt? Can't I just use -{}-? – tommy.carstensen Apr 23 '15 at 09:42
  • the op wanted to use the typewriter font, that's all ... –  May 18 '16 at 16:17
14

This approach didn't work for me in the bibliography, if you have the same problem simply insert one of the phantom commands:

@article{Foo,
  title = {Hello -\phantom{}- World},
}

This will work as desired.

Fleshgrinder
  • 859
  • 8
  • 14
8

For me, -{}- is not working for breaking the ligature in --. The trick -\phantom{}- works, but it is a bit wordy. I found that the command for the italic correction could be the best choice in this particular case: this -\/- works for me in every case.

Please see difference between {} and / for breaking ligatures for further details.

khyox
  • 181
0

If you have a decent modern Unicode setup, a double Minus Sign character (“−−”) may not get mangled. (In my case LuaLaTeX with fontspec and optionally unicode-math.)

Flash Sheridan
  • 269
  • 3
  • 7