A Mutable Log

A blog by Devendra Tewari

Project maintained by tewarid Hosted on GitHub Pages — Theme by mattgraham

Pandoc Lua filter to convert math block type

I’ve been using Markdown for writing text, and converting it to different formats such as HTML and PDF. As a result, I am having to learn to live with differences between different converters such as Pandoc, which I use to render HTML and PDF, and Kramdown, which is used by Jekyll to render this page.

One particular difference is related to how you insert math using \(\LaTeX\). Whereas Pandoc expects you to insert inline math between $ signs, Kramdown expects you to use $$. Pandoc understands math between $$ as block math syntax and it will add a line break before and after the math. The math still gets rendered nicely but messes with the vertical flow of the text.

Since all the math I use is inline, I wrote a simple Pandoc filter in Lua to force any math block to be inline

function Math(meta)
    --print(meta.mathtype.." "..meta.text)
    -- force InlineMath
    meta.mathtype = "InlineMath"
    return meta

Assuming the code above is saved to a file called filter.lua, you can tell Pandoc to use the filter by adding --lua-filter filter.lua to its command line.

If you do want to display math in its own block, you can create a paragraph containing just the math


Pandoc filters to the rescue!