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.
This commit is contained in:
John MacFarlane 2017-04-29 11:05:44 +02:00
parent e76b672414
commit 730796ee31
6 changed files with 20 additions and 2 deletions

View file

@ -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}}{}

View file

@ -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}}{}

View file

@ -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

10
test/command/1629.md Normal file
View file

@ -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!}}
```

View file

@ -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[]);!}
```

View file

@ -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}