Search code examples
lualatexpandoc

Pandoc re-run latex filter on element after latex+raw_tex and lua filter


I've been writing a custom LaTeX reader Lua filter to convert a bunch of LaTeX source into Pandoc Markdown, which will be my new source for several documents.

I run the usual filter like the following.

pandoc file.tex \ 
  -f latex+raw_tex \
  -t markdown \
  -o file.md \
  --lua-filter myfilter.lua

This works great. I've been able to convert several custom LaTeX environments into Divs and the like.

However, when I have standard LaTeX nested inside the custom environment, the filter output leaves that as-is, unconverted. For example.

\begin{custom_environment}
\begin{itemize}
\item foo
\item bar
\end{itemize}
\end{custom_environment}

The custom environment is handled just fine by my filter, but the internal itemize or tabular or similar are, left unprocessed, as you would expect.

Is there some way to process the contents of the custom environment using the standard latex extension (i.e. filter)? I assume it would be a call from the Lua filter.

An idea I have for doing this that I'm trying to avoid is writing the contents to a temporary file and running another pandoc on that. It seems like such a ubiquitous situation that I'm hoping there's a better way. Thanks!


Solution

  • The best solution I found was pandoc.read: https://pandoc.org/lua-filters.html#helper-functions

    I used it as follows.

    internal_div = pandoc.Div(
        pandoc.read(
            contents_of_custom_environment,
            'latex'
        ).blocks,
        'div_label'
    )
    

    Now internal_div contains the latex-processed contents of the custom environment. Note that contents_of_custom_environment is just a string.

    I tried using walk_block and walk_inline, but couldn't get them to work quite like this. I would welcome an answer that did so.