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:
parent
e76b672414
commit
730796ee31
6 changed files with 20 additions and 2 deletions
|
@ -125,6 +125,7 @@ $if(verbatim-in-note)$
|
||||||
$endif$
|
$endif$
|
||||||
$if(listings)$
|
$if(listings)$
|
||||||
\usepackage{listings}
|
\usepackage{listings}
|
||||||
|
\newcommand{\passthrough}[1]{#1}
|
||||||
$endif$
|
$endif$
|
||||||
$if(lhs)$
|
$if(lhs)$
|
||||||
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
||||||
|
|
|
@ -129,6 +129,7 @@ $endfor$
|
||||||
$endif$
|
$endif$
|
||||||
$if(listings)$
|
$if(listings)$
|
||||||
\usepackage{listings}
|
\usepackage{listings}
|
||||||
|
\newcommand{\passthrough}[1]{#1}
|
||||||
$endif$
|
$endif$
|
||||||
$if(lhs)$
|
$if(lhs)$
|
||||||
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
||||||
|
|
|
@ -967,7 +967,12 @@ inlineToLaTeX (Code (_,classes,_) str) = do
|
||||||
let chr = case "!\"&'()*,-./:;?@_" \\ str of
|
let chr = case "!\"&'()*,-./:;?@_" \\ str of
|
||||||
(c:_) -> c
|
(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 ++ "}"))
|
let rawCode = liftM (text . (\s -> "\\texttt{" ++ escapeSpaces s ++ "}"))
|
||||||
$ stringToLaTeX CodeString str
|
$ stringToLaTeX CodeString str
|
||||||
where escapeSpaces = concatMap
|
where escapeSpaces = concatMap
|
||||||
|
|
10
test/command/1629.md
Normal file
10
test/command/1629.md
Normal 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!}}
|
||||||
|
```
|
|
@ -4,6 +4,6 @@ See #3422
|
||||||
% pandoc -t latex --listings
|
% pandoc -t latex --listings
|
||||||
`int main(int argc, const char *argv[]);`{.c}
|
`int main(int argc, const char *argv[]);`{.c}
|
||||||
^D
|
^D
|
||||||
\lstinline[language=C]!int main(int argc, const char *argv[]);!
|
\passthrough{\lstinline[language=C]!int main(int argc, const char *argv[]);!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
breaklinks=true}
|
breaklinks=true}
|
||||||
\urlstyle{same} % don't use monospace font for urls
|
\urlstyle{same} % don't use monospace font for urls
|
||||||
\usepackage{listings}
|
\usepackage{listings}
|
||||||
|
\newcommand{\passthrough}[1]{#1}
|
||||||
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
|
||||||
\IfFileExists{parskip.sty}{%
|
\IfFileExists{parskip.sty}{%
|
||||||
\usepackage{parskip}
|
\usepackage{parskip}
|
||||||
|
|
Loading…
Reference in a new issue