# How does TeX's mechanism for sorting out quotation marks work?

When I enter hello'' in TeX, it very nicely reformats it to some nicer version of "hello". The mechanism for this seems to be buried deep down in the depths of TeX (and I can't find it in my usual resource: TeXbyTopic).

It seems like a font thing because if I do \ttfamily (in LaTeX) to get a monospaced font then this automatic replacement is turned off. I'd really like to turn it back on again, so that with \ttfamily I still get "hello". Defining ' and  as active characters seems a little dangerous for this task (though I'm happy to be proven wrong).

-

use a font which supports such ligatures

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

\begin{document}
foo'' and \ttfamilybar''

\end{document}

-
Works like a charm! (I guess from the word "ligatures", and the form of your answer, that the conversion doubled-up single quotes to double quotes is handled by the same mechanism that makes ff look nice. Am I right?) – Loop Space Jul 27 '11 at 12:15
yes, you are right – Herbert Jul 27 '11 at 12:21

It's the same mechanism that from the input fi produces a ligature. It's indeed font dependent. If you do

tftopl cmr10


you get, among other things,

(LIGTABLE
...
(LABEL O 140)
(LIG O 140 O 134)
(STOP)
...


which means: when character '140 (octal, the backquote) is followed by another character '140, substitute them with character '134. If the current font doesn't have this information, no substitution is made (and cmtt10 hasn't such an entry). Such ligatures are defined in the Metafont sources. For non Metafont produced fonts, the afmtotfm program or the package fontinst add the programs for the ligatures, if so desired, when producing the .tfm files.

When TeX is instructed to load a font, say cmr10, it actually reads cmr10.tfm that contains, in a compact form, what is seen in "human" form via the tftopl program. The two characters must be in the same font:

\bfseries


won't give a ligature.

XeTeX uses another method. Some ligatures are defined by the font itself (fi, for instance), but things such as '' aren't. In order to get the "same" output from the same source, you specify a "map file" that uses the TecKit technology. The instruction that's added to the font name is mapping=tex-text which loads, together with the font, a file tex-text.tec which is a compact form of tex-text.map:

; TECkit mapping for TeX input conventions <-> Unicode characters

LHSName "TeX-text"
RHSName "UNICODE"

pass(Unicode)

; ligatures from Knuth's original CMR fonts
U+002D U+002D                   <>      U+2013  ; -- -> en dash
U+002D U+002D U+002D    <>      U+2014  ; --- -> em dash

U+0027                  <>      U+2019  ; ' -> right single quote
U+0027 U+0027   <>      U+201D  ; '' -> right double quote
U+0022                   >      U+201D  ; " -> right double quote

U+0060                  <>      U+2018  ;  -> left single quote
U+0060 U+0060   <>      U+201C  ;  -> left double quote

U+0021 U+0060   <>      U+00A1  ; ! -> inverted exclam
U+003F U+0060   <>      U+00BF  ; ? -> inverted question

; additions supported in T1 encoding
U+002C U+002C   <>      U+201E  ; ,, -> DOUBLE LOW-9 QUOTATION MARK
U+003C U+003C   <>      U+00AB  ; << -> LEFT POINTING GUILLEMET
U+003E U+003E   <>      U+00BB  ; >> -> RIGHT POINTING GUILLEMET


The mechanism is similar: for instance, the combination U+0060 U+0060 is transformed into U+201C.

LuaTeX uses another different method.

-

As Herbert already implied this is done using ligatures and therefore is indeed done by the font, not in form of (La)TeX macro definitions. I agree with using lmodern incl. the fontencoding.

In addition I also like to point out the nice csquotes package which provides a lot of features for quoting, e.g. \enquote{<text>}. It supports different languages and their quoting style! Especially in my package documentations I like to use the back-ticks for a special verbatim mode and then such a macro is very welcome. However, it doesn't seem to ensure proper quotation marks for the used font, so loading a correct one manually as Herbert has shown is still required.

-
+1 for csquotes. – domwass Jul 27 '11 at 13:12
csquotes looks very nice, and I originally looked in there, but in this particular case I'm writing something to be used by others, so I want as few restrictions as possible. – Loop Space Jul 27 '11 at 15:38

The quotation marks and the en or em dashes use the same mechanism as ligatures do. That is, a font defines certain combinations of input characters to be replaced with a specific glyph. For example, this is from the METAFONT sources of the original Computer Modern fonts:

ligtable "": ""=:oct"134";
ligtable "'": "'"=:oct"042", "?" kern 2u#, "!" kern 2u#;


That said, if the font does not define a ligature for '', the characters will not be replaced. In the same Computer Modern sources we can verify that the cmtt fonts set ligs:=0, which skips ligature generation. Herbert already showed that Latin Modern's monospaced font, in contrast, supports these ligatures.

It should be also noted that this particular convention is found mainly in TeX-related fonts. Many TrueType or OpenType fonts won't include these ligatures. Engines supporting such fonts will implement their own solutions to keep the functionality. For example, see the XeTeX FAQ entry on this topic.

-