summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-04-29 11:05:44 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-04-29 11:05:44 +0200
commit730796ee314d42477fab216621b8e44539c94656 (patch)
tree023418122afdff27f4595d3edb070f3d9f11db88
parente76b6724144032c62c183f850fe05271aa245fb5 (diff)
LaTeX writer: Fix problem with escaping in lstinline.
Previously the LaTeX writer created invalid LaTeX when `--listings` was specified and a code span occured inside emphasis or another construction. This is because the characters `%{}\` must be escaped in lstinline when the listinline occurs in another command, otherwise they must not be escaped. To deal with this, adoping Michael Kofler's suggestion, we always wrap lstinline in a dummy command `\passthrough`, now defined in the default template if `--listings` is specified. This way we can consistently escape the special characters. Closes #1629.
-rw-r--r--data/templates/default.beamer1
-rw-r--r--data/templates/default.latex1
-rw-r--r--src/Text/Pandoc/Writers/LaTeX.hs7
-rw-r--r--test/command/1629.md10
-rw-r--r--test/command/3422.md2
-rw-r--r--test/lhs-test.latex+lhs1
6 files changed, 20 insertions, 2 deletions
diff --git a/data/templates/default.beamer b/data/templates/default.beamer
index 4f2cae89a..fb02cb058 100644
--- a/data/templates/default.beamer
+++ b/data/templates/default.beamer
@@ -125,6 +125,7 @@ $if(verbatim-in-note)$
$endif$
$if(listings)$
\usepackage{listings}
+\newcommand{\passthrough}[1]{#1}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
diff --git a/data/templates/default.latex b/data/templates/default.latex
index 030ab90f1..899a00aea 100644
--- a/data/templates/default.latex
+++ b/data/templates/default.latex
@@ -129,6 +129,7 @@ $endfor$
$endif$
$if(listings)$
\usepackage{listings}
+\newcommand{\passthrough}[1]{#1}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index f0767c17c..000f4f8fb 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -967,7 +967,12 @@ inlineToLaTeX (Code (_,classes,_) str) = do
let chr = case "!\"&'()*,-./:;?@_" \\ str of
(c:_) -> c
[] -> '!'
- return $ text $ "\\lstinline" ++ listingsopt ++ [chr] ++ str ++ [chr]
+ let str' = escapeStringUsing (backslashEscapes "\\{}%") str
+ -- we always put lstinline in a dummy 'passthrough' command
+ -- (defined in the default template) so that we don't have
+ -- to change the way we escape characters depending on whether
+ -- the lstinline is inside another command. See #1629:
+ return $ text $ "\\passthrough{\\lstinline" ++ listingsopt ++ [chr] ++ str' ++ [chr] ++ "}"
let rawCode = liftM (text . (\s -> "\\texttt{" ++ escapeSpaces s ++ "}"))
$ stringToLaTeX CodeString str
where escapeSpaces = concatMap
diff --git a/test/command/1629.md b/test/command/1629.md
new file mode 100644
index 000000000..34d529b0d
--- /dev/null
+++ b/test/command/1629.md
@@ -0,0 +1,10 @@
+```
+% pandoc -t latex --listings
+bla bla `a % b`
+
+*bla bla `a % b`*
+^D
+bla bla \passthrough{\lstinline!a \% b!}
+
+\emph{bla bla \passthrough{\lstinline!a \% b!}}
+```
diff --git a/test/command/3422.md b/test/command/3422.md
index a010320e9..19f1f4462 100644
--- a/test/command/3422.md
+++ b/test/command/3422.md
@@ -4,6 +4,6 @@ See #3422
% pandoc -t latex --listings
`int main(int argc, const char *argv[]);`{.c}
^D
-\lstinline[language=C]!int main(int argc, const char *argv[]);!
+\passthrough{\lstinline[language=C]!int main(int argc, const char *argv[]);!}
```
diff --git a/test/lhs-test.latex+lhs b/test/lhs-test.latex+lhs
index 3509cb4a6..b0a58ac78 100644
--- a/test/lhs-test.latex+lhs
+++ b/test/lhs-test.latex+lhs
@@ -24,6 +24,7 @@
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{listings}
+\newcommand{\passthrough}[1]{#1}
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}