'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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source