Search code examples
lualatexpandoc

Latex to HTML with Pandoc, how do I include the Lua script output to the conversion?


I'm using pandoc to convert LaTeX to HTML. However, I have a lua script included in the latex file (which pulls some data from a JSON file and formats the data to LaTeX). As I convert to HTML, the script is not executed but appears as lua in the output.

Is there either a way to get a pure latex output for the conversion or to run the script during the conversion?


Solution

  • Unfortunately, the answer is "yes, but actually: no".

    What I mean is that you can run the Lua code, but it is very likely to contain code which is luatex specific and will not work in pandoc.

    Let's look at an example:

    \documentclass{article}
    \usepackage{luacode}
    \begin{document}
    You are runnig:
    \begin{luacode}
    tex.print(_VERSION)
    \end{luacode}
    \end{document}
    

    The script, when run through lualatex, will report the Lua version used to execute the code (currently "Lua 5.3"). The tex.print command is provided by lualatex.

    To see how pandoc handles this, we can convert it into pandoc's internal format with pandoc --to=native. Pandoc doesn't know the luacode environment, so it treats it like normal text.

    [Para [Str "You",Space,Str "are",Space,Str "runnig:"]
    ,Div ("",["luacode"],[])
     [Para [Str "tex.print(_VERSION)"]]]
    

    We see that the block becomes a div with class luacode. It's possible to run a Lua filter and execute it's content:

    -- file: run-luacode.lua
    function Div(d)
      local code = pandoc.utils.stringify(d)
      load(code)()
    end
    

    Using this with

    pandoc my-test.latex --to=html --lua-filter=run-luacode.lua
    

    Will lead to an error, because tex.print is undefined in pandoc's Lua.

    Error running filter run-luacode.lua:
    [string "tex.print(_VERSION)"]:1: attempt to index a nil value (global 'tex')
    stack traceback:
            [string "tex.print(_VERSION)"]:1: in main chunk
            run-luacode.lua:3: in function 'Div'
    

    Of course, we could define tex.print in the pandoc filter. E.g., setting

    tex = {['print'] = print}
    

    will at least print the result to the console. You could devise a mechanism that actually converts it to pandoc's internal document format. See https://pandoc.org/lua-filters.html for details.

    It might also be beneficial to call pandoc with --from=latex+raw_tex, which makes pandoc keep the unknown luacode environment verbatim in a RawBlock element. This can be easier to process in the filter.