jgm / pandoc

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

Support lstnewenvironment #4770

Open h4ck3rm1k3 opened 6 years ago

h4ck3rm1k3 commented 6 years ago

Found originally on this file algorithms.tex https://github.com/cplusplus/draft/blob/master/source/algorithms.tex#L334

Error at "source" (line 334, column 9):
unexpected _
expecting \end{codeblock}
std::for_each(std::execution::par, std::begin(a), std::end(a), [&](int i) {
        ^

Test case now here https://github.com/h4ck3rm1k3/pandoc/blob/master/test/tex/tex-code-block-escape.tex Error message https://github.com/h4ck3rm1k3/pandoc/blob/master/test/tex/tex-code-block-escape.tex.err

There are other errors like this in that project

Error at "source" (line 45, column 22):
unexpected _
expecting \end{codeblock}
#include <initializer_list>     // see \ref{initializer_list.syn}
                     ^
xrefdelta.tex
jgm commented 6 years ago

codeblock isn't a standard LaTeX macro. Presumably this is some kind of custom verbatim environment? In any case, pandoc doesn't know it's a verbatim environment, and that's the issue here.

You can work around this by adding something like this before your content:

\renewenvironment{codeblock}{\begin{verbatim}}{\end{verbatim}}
h4ck3rm1k3 commented 6 years ago

codeblock is defined here in the source https://github.com/cplusplus/draft/blob/master/source/macros.tex#L390

have tried to extract a test case with the workaround https://github.com/h4ck3rm1k3/pandoc/blob/master/test/tex/codeblocksetup.tex

So applying this idea

\renewenvironment{codeblock}{\begin{verbatim}}{\end{verbatim}}
\begin{codeblock}
std::for_each
\end{codeblock}

gives

Error at "source" (line 4, column 17):
unexpected end of input
expecting \end{verbatim}
\end{codeblock}

Errors here https://github.com/h4ck3rm1k3/pandoc/blob/master/test/tex/codeblocksetup.tex.err

Now using verbatim directly works.

\begin{codeblock}
\begin{verbatim}                                                                                                         
std::for_each                                                                                                            
\end{verbatim}
\end{codeblock}

PS: this is also occurring in itemdecl and bnf the macro definitions for these are

h4ck3rm1k3 commented 6 years ago

This did not occur in 1.19.2.4 on debian

jgm commented 6 years ago

Looks like we need better support for \lstnewenvironment from listings.

mppf commented 5 years ago

I'm running into this issue trying to convert https://github.com/chapel-lang/chapel/blob/master/spec/spec.tex with pandoc.

Is there a workaround for this issue? I'm happy to modify the definitions for things like \begin{codeblock} in the example above, but I really don't want to modify all of the locations where it is used because there are rather a lot of them. (Sure, I could probably modify those with pandoc somehow... but not if it just dies with a syntax error.)

But, at the end of the day, I also want to have these listing environments turn into rst code blocks specifying a language. How can I modify the LaTeX input so that it is reasonable to accomplish that (maybe with a filter)? The example4.tex example doesn't seem to use code syntax highlighting.

I have never used pandoc before trying to do this task, so forgive me if the answer is somehow obvious. Thanks!

mppf commented 5 years ago

I tried to use the verbatim package as a workaround:

\newenvironment{codeblock}%
  {\endgraf\verbatim}%
  {\endverbatim}

while with this I am able to get LaTeX to render the document without using lstnewenvironment, pandoc still gets tripped up when the code includes an _.

jonsneyers commented 2 years ago

Any update on this? I'm also running into this issue...

jgm commented 2 years ago

@jonsneyers what version of pandoc are you using? With 2.19.2, it handles this fine (with the small workaround suggested by @mppf - @mppf, I don't think underlines cause a problem any more, see below):

% pandoc -f latex -t native
\newenvironment{codeblock}%
  {\endgraf\verbatim}%
  {\endverbatim}

\begin{codeblock}
\begin{verbatim}                                                                                                         
std::for_each
\end{verbatim}
\end{codeblock}
^D

[ CodeBlock
    ( "" , [] , [] )
    "std::for_each"
]