git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: avoid `{}` after control sequences when escaping.

`\ldots{}.` doesn't behave as well as `\ldots.` with the latex ellipsis package. This patch causes pandoc to avoid emitting the `{}` when it is not necessary. Now `\ldots` and other control sequences used in escaping will be followed by either a `{}`, a space, or nothing, depending on context. Thanks to Elliott Slaughter for the suggestion.

commit633a9ecfecd7a111d6727bd4c44750dbb3c9af23 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 1 Feb 2019, 21:17 (8 years ago) parentb436087
4 files changed+41−32
src/Text/Pandoc/Writers/LaTeX.hs+36−26 test/command/1126.md+2−3 test/command/ascii.md+1−1 test/writer.latex+2−2
src/Text/Pandoc/Writers/LaTeX.hs+36−26
@@ -39,8 +39,8 @@ import Prelude
3939import Control.Applicative ((<|>))
4040import Control.Monad.State.Strict
4141import Data.Aeson (FromJSON, object, (.=))
42−import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isPunctuation, ord,
43− toLower)
42+import Data.Char (isAlphaNum, isAscii, isDigit, isLetter, isSpace,
43+ isPunctuation, ord, toLower)
4444import Data.List (foldl', intercalate, intersperse, isInfixOf, nubBy,
4545 stripPrefix, (\\), uncons)
4646import Data.Maybe (catMaybes, fromMaybe, isJust, mapMaybe, isNothing)
@@ -345,10 +345,20 @@ stringToLaTeX context zs = do
345345 Just cmd -> ((cmd ++ "{" ++ [c] ++ "}") ++)
346346 <$> go opts ctx (drop 1 xs) -- drop combining accent
347347 Nothing -> (c:) <$> go opts ctx xs
348+ let emitcseq cs = do
349+ rest <- go opts ctx xs
350+ case rest of
351+ c:_ | isLetter c
352+ , ctx == TextString
353+ -> return (cs <> " " <> rest)
354+ | isSpace c -> return (cs <> "{}" <> rest)
355+ | ctx == TextString
356+ -> return (cs <> rest)
357+ _ -> return (cs <> "{}" <> rest)
348358 case x of
349359 '{' -> emits "\\{"
350360 '}' -> emits "\\}"
351− '`' | ctx == CodeString -> emits "\\textasciigrave{}"
361+ '`' | ctx == CodeString -> emitcseq "\\textasciigrave"
352362 '$' | not isUrl -> emits "\\$"
353363 '%' -> emits "\\%"
354364 '&' -> emits "\\&"
@@ -358,19 +368,19 @@ stringToLaTeX context zs = do
358368 -- prevent adjacent hyphens from forming ligatures
359369 ('-':_) -> emits "-\\/"
360370 _ -> emitc '-'
361− '~' | not isUrl -> emits "\\textasciitilde{}"
371+ '~' | not isUrl -> emitcseq "\\textasciitilde"
362372 '^' -> emits "\\^{}"
363373 '\\'| isUrl -> emitc '/' -- NB. / works as path sep even on Windows
364− | otherwise -> emits "\\textbackslash{}"
365− '|' | not isUrl -> emits "\\textbar{}"
366− '<' -> emits "\\textless{}"
367− '>' -> emits "\\textgreater{}"
374+ | otherwise -> emitcseq "\\textbackslash"
375+ '|' | not isUrl -> emitcseq "\\textbar"
376+ '<' -> emitcseq "\\textless"
377+ '>' -> emitcseq "\\textgreater"
368378 '[' -> emits "{[}" -- to avoid interpretation as
369379 ']' -> emits "{]}" -- optional arguments
370− '\'' | ctx == CodeString -> emits "\\textquotesingle{}"
380+ '\'' | ctx == CodeString -> emitcseq "\\textquotesingle"
371381 '\160' -> emits "~"
372382 '\x202F' -> emits "\\,"
373− '\x2026' -> emits "\\ldots{}"
383+ '\x2026' -> emitcseq "\\ldots"
374384 '\x2018' | ligatures -> emits "`"
375385 '\x2019' | ligatures -> emits "'"
376386 '\x201C' | ligatures -> emits "``"
@@ -379,22 +389,22 @@ stringToLaTeX context zs = do
379389 '\x2013' | ligatures -> emits "--"
380390 _ | writerPreferAscii opts
381391 -> case x of
382− 'ı' -> emits "\\i "
383− 'ȷ' -> emits "\\j "
384− 'å' -> emits "\\aa "
385− 'Å' -> emits "\\AA "
386− 'ß' -> emits "\\ss "
387− 'ø' -> emits "\\o "
388− 'Ø' -> emits "\\O "
389− 'Ł' -> emits "\\L "
390− 'ł' -> emits "\\l "
391− 'æ' -> emits "\\ae "
392− 'Æ' -> emits "\\AE "
393− 'œ' -> emits "\\oe "
394− 'Œ' -> emits "\\OE "
395− '£' -> emits "\\pounds "
396− '€' -> emits "\\euro "
397− '©' -> emits "\\copyright "
392+ 'ı' -> emitcseq "\\i"
393+ 'ȷ' -> emitcseq "\\j"
394+ 'å' -> emitcseq "\\aa"
395+ 'Å' -> emitcseq "\\AA"
396+ 'ß' -> emitcseq "\\ss"
397+ 'ø' -> emitcseq "\\o"
398+ 'Ø' -> emitcseq "\\O"
399+ 'Ł' -> emitcseq "\\L"
400+ 'ł' -> emitcseq "\\l"
401+ 'æ' -> emitcseq "\\ae"
402+ 'Æ' -> emitcseq "\\AE"
403+ 'œ' -> emitcseq "\\oe"
404+ 'Œ' -> emitcseq "\\OE"
405+ '£' -> emitcseq "\\pounds"
406+ '€' -> emitcseq "\\euro"
407+ '©' -> emitcseq "\\copyright"
398408 _ -> emitc x
399409 | otherwise -> emitc x
400410
test/command/1126.md+2−3
@@ -5,9 +5,8 @@ A&=&B,\\
55C&=&D
66\end{eqnarray}
77^D
8−\textbackslash{}begin\{eqnarray\}
9−A\&=\&B,\textbackslash{}\textbackslash{} C\&=\&D
10−\textbackslash{}end\{eqnarray\}
8+\textbackslash begin\{eqnarray\} A\&=\&B,\textbackslash\textbackslash{}
9+C\&=\&D \textbackslash end\{eqnarray\}
1110```
1211
1312```
test/command/ascii.md+1−1
@@ -9,7 +9,7 @@ pandoc -t html --ascii
99pandoc -t latex --ascii
1010äéıå
1111^D
12−\"{a}\'{e}\i \r{a}
12+\"{a}\'{e}\i\r{a}
1313```
1414
1515```
test/writer.latex+2−2
@@ -719,7 +719,7 @@ Subscripts: H\textsubscript{2}O, H\textsubscript{23}O,
719719H\textsubscript{many~of~them}O.
720720
721721These should not be superscripts or subscripts, because of the unescaped
722−spaces: a\^{}b c\^{}d, a\textasciitilde{}b c\textasciitilde{}d.
722+spaces: a\^{}b c\^{}d, a\textasciitilde b c\textasciitilde d.
723723
724724\begin{center}\rule{0.5\linewidth}{\linethickness}\end{center}
725725
@@ -741,7 +741,7 @@ Some dashes: one---two --- three---four --- five.
741741
742742Dashes between numbers: 5--7, 255--66, 1987--1999.
743743
744−Ellipses\ldots{}and\ldots{}and\ldots{}.
744+Ellipses\ldots and\ldots and\ldots.
745745
746746\begin{center}\rule{0.5\linewidth}{\linethickness}\end{center}
747747