latexpandoc

pandoc: fenced code block does not work within LaTeX environment


If I use fenced code block within pandoc markdown it produces reasonable LaTeX ouptut:

\pagebreak
```python
foo = 1
```
pandoc input.md -o output.tex
\pagebreak

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{foo }\OperatorTok{=} \DecValTok{1}
\end{Highlighting}
\end{Shaded}

But if I replace \pagebreak LaTeX command with an environment command, fenced code block will not work anymore:

\begin{minipage}{\textwidth}
```python
foo = 1
```
\end{minipage}
pandoc input.md -o output.tex
\begin{minipage}{\textwidth}
```python
foo = 1
```
\end{minipage}

It seems that pandoc has some special handling for LaTeX environments which breaks things. Does anyone know where can I search for a solution? I want environment commands to be inserted into output as-is, like all other LaTeX commands, without any special handling.

The solution I know about is to insert LaTeX code as a fenced block themselves, but that adds lots of unnecessary lines of code which I prefer to avoid:

```{=latex}
\begin{minipage}{\textwidth}
```
`\begin{minipage}{\textwidth}`{=latex}

Solution

  • You can't use any markdown commands inside latex environments. However if you use the underling macros, which latex internally uses for the environments, markdown won't know that you are in a latex environment and will happily do its job:

    ---
    output: pdf_document
    ---
    
    \minipage{\textwidth}
    ```python
    foo = 1
    ```
    \endminipage