git.iko.soy/typeable/pandoc
on master help sign in
LaTeX writer: Specially escape non-ascii characters in labels.

Otherwise we can get compile errors and other bugs when compiled with pdflatex. Closes #1007. Thanks to begemotv2718 for the fix.

commit1f29f4678e0a1b1272704e5ddafa0bee45e5e426 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 17 Oct 2013, 22:06 (13 years ago) parent80c1967
1 file changed+14−6
src/Text/Pandoc/Writers/LaTeX.hs+14−6
src/Text/Pandoc/Writers/LaTeX.hs+14−6
@@ -39,7 +39,7 @@ import Text.Printf ( printf )
3939import Network.URI ( isURI, unEscapeString )
4040import Data.List ( (\\), isSuffixOf, isInfixOf,
4141 isPrefixOf, intercalate, intersperse )
42−import Data.Char ( toLower, isPunctuation )
42+import Data.Char ( toLower, isPunctuation, isAscii, isLetter, isDigit, ord )
4343import Control.Applicative ((<|>))
4444import Control.Monad.State
4545import Text.Pandoc.Pretty
@@ -222,6 +222,13 @@ stringToLaTeX ctx (x:xs) = do
222222 '\x2013' | ligatures -> "--" ++ rest
223223 _ -> x : rest
224224
225+toLabel :: String -> String
226+toLabel [] = ""
227+toLabel (x:xs)
228+ | (isLetter x || isDigit x) && isAscii x = x:toLabel xs
229+ | elem x "-+=:;." = x:toLabel xs
230+ | otherwise = "ux" ++ printf "%x" (ord x) ++ toLabel xs
231+
225232-- | Puts contents into LaTeX command.
226233inCmd :: String -> Doc -> Doc
227234inCmd cmd contents = char '\\' <> text cmd <> braces contents
@@ -329,7 +336,7 @@ blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do
329336 | writerListings opts -> listingsCodeBlock
330337 | writerHighlight opts && not (null classes) -> highlightedCodeBlock
331338 | otherwise -> rawCodeBlock
332− where ref = text identifier
339+ where ref = text $ toLabel identifier
333340 linkAnchor = if null identifier
334341 then empty
335342 else "\\hyperdef{}" <> braces ref <>
@@ -361,7 +368,7 @@ blockToLaTeX (CodeBlock (identifier,classes,keyvalAttr) str) = do
361368 (key,attr) <- keyvalAttr ] ++
362369 (if identifier == ""
363370 then []
364− else [ "label=" ++ identifier ])
371+ else [ "label=" ++ toLabel identifier ])
365372
366373 else []
367374 printParams
@@ -537,13 +544,13 @@ sectionHeader unnumbered ref level lst = do
537544 let refLabel x = (if ref `elem` internalLinks
538545 then text "\\hyperdef"
539546 <> braces empty
540− <> braces (text ref)
547+ <> braces (text $ toLabel ref)
541548 <> braces x
542549 else x)
543550 let headerWith x y r = refLabel $ text x <> y <>
544551 if null r
545552 then empty
546− else text "\\label" <> braces (text r)
553+ else text "\\label" <> braces (text $ toLabel r)
547554 let sectionType = case level' of
548555 0 | writerBeamer opts -> "part"
549556 | otherwise -> "chapter"
@@ -664,7 +671,8 @@ inlineToLaTeX Space = return space
664671inlineToLaTeX (Link txt ('#':ident, _)) = do
665672 contents <- inlineListToLaTeX txt
666673 ident' <- stringToLaTeX URLString ident
667− return $ text "\\hyperref" <> brackets (text ident') <> braces contents
674+ return $ text "\\hyperref" <> brackets (text $ toLabel ident') <>
675+ braces contents
668676inlineToLaTeX (Link txt (src, _)) =
669677 case txt of
670678 [Str x] | x == src -> -- autolink