jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.7k stars 3.39k forks source link

Including an _ in a latex block causes pandoc to escape the entire block #4473

Closed MageJohn closed 3 years ago

MageJohn commented 6 years ago

It seems that when an underscore (_) is found inside a block of raw latex in a markdown document, Pandoc escapes the block, which causes an error with pdflatex.

This code compiles fine:

---
header-includes:
    - \usepackage{circuitikz}
---

\begin{figure}
    \centering
    \begin{circuitikz} \draw
        (0,0) to[battery] (3,0)
            -- (3,2)
            to[C, l^=$C$] (0,2)
            -- (0,0)
        ;
    \end{circuitikz}
\end{figure}

This is the output test1.pdf

However, this gives an error:

---
header-includes:
    - \usepackage{circuitikz}
---

\begin{figure}
    \centering
    \begin{circuitikz} \draw
        (0,0) to[battery] (3,0)
            -- (3,2)
            to[C, l_=$C$] (0,2)
            -- (0,0)
        ;
    \end{circuitikz}
\end{figure}

The only difference is in the line that starts to[C, l...; in the first (which compiles) there's a ^ and in the other there's a _. The error is:

Error producing PDF.
! Undefined control sequence.
l.61 \textbackslash{}begin\{circuitikz\} 

The expected output (made by converting to latex and then editing that) is this: test2.pdf

My pandoc version is:

pandoc 2.1.1 Compiled with pandoc-types 1.17.3, texmath 0.10.1, skylighting 0.6

This is the latest in my repositories.

The command line used to compile the examples to pdf was: pandoc -o test.pdf test.md

jgm commented 6 years ago

This is because pandoc doesn't know that this environment redefines the normal behavior of _. But we might be able to make pandoc more forgiving in such cases.

MageJohn commented 6 years ago

Sorry for the late reply. I think it might be reasonable to not process anything inside a raw latex block. Either that, or have a whitelist of blocks where it's useful to still process markdown, like inside of a \caption{}.

reneknuvers commented 6 years ago

I ran into the same problem and found out, combining solutions to similar problems that I found on the web, that you can have markdown parse the latex verbatim by enclosing it in the following lines:

---
header-includes:
    - \usepackage{circuitikz}
---
```{=latex}
\begin{figure}
    \centering
    \begin{circuitikz} \draw
        (0,0) to[battery] (3,0)
            -- (3,2)
            to[C, l_=$C$] (0,2)
            -- (0,0)
        ;
    \end{circuitikz}
\end{figure}
```

This works for me, so probably also for you

fedelibre commented 5 years ago

I ran into the same issue using the lyluatex package. See above reference for details. ```{=latex} solved the problem.

fedelibre commented 5 years ago

I'm using Pandoc 2.1.2. I guess that this commit (in version 2.2.2) will solve my problem.

tarleb commented 3 years ago

This seems to have been fixed, pandoc now parses the whole figure block as raw TeX.