Search code examples
luar-markdownpandoctemplating

Rmarkdown with pandoc templates, apply lua filter on intermediate .tex


I'm trying to use lua filters to capture images in my manuscript and list their caption in a special \section at the end of it. I am working on a rmarkdown document that itself uses a .tex template.

I wasn't able to get anywhere, so I run a very simple filter:

function Header (head) print(pandoc.utils.stringify(head)) end

and noticed that just the headers in the markdown were recognized, not the ones in the ones in the template.

The only way I found to have lua filters recognize the elements in the template was to rerun the produced .tex file with pandoc:

pandoc -f latex -t latex -o test2.tex --lua-filter=my_filters.lua test.tex

but that removed all latex formatting and structure content outside the body, e.g., \documentclass, \usepackage and other custom commands. So it's a no go.

So the question is, is there a way to force lua filter to be applied after the integration of a latex template when knitting a rmarkdown document?


Solution

  • There might be a way, but it most likely won't do what you need.

    When pandoc reads a document, it parses it and converts it into it's internal data structure. That internal structure can then be modified with a filter. LaTeX is a very expressive and complex document format, and any conversion from LaTeX into pandoc's internal format will result in a loss of (layout) information. That's good enough in most cases, but would be a problem in your case.

    There are two possible ways to do this: one is to post-process the output, which is probably tedious and error-prone. The other is to find a way to generate the desired output, e.g. via a pandoc filter, without adding it to the template first.

    I believe your other question is the right way to go.