bookdown-equations, Convert LaTeX equation labels to bookdown compatible labels

GitHub build status GitHub Workflow Status Pandoc Version Lua Filter MIT

bookdown-equations, is a Pandoc Lua filter for Converting LaTeX equation labels to Bookdown Style equation labels in MathJax environment. This filter also updates the equation references to bookdown style references.

Usage

LaTeX –> Markdown/R-markdown

The filter modifies the internal document representation to bookdown specified labelling/numbering system. Hence it will work with documentation formats using bookdown.

Plain pandoc

Pass the filter to pandoc via the --lua-filter (or -L) command line option.

pandoc --lua-filter bookdown-equations.lua ...

License

This pandoc Lua filter is published under the MIT license, see file LICENSE for details.

Example

input.tex

\documentclass[a4paper,12pt]{article} % The document class with options
\usepackage[T1]{fontenc}
\author{Abhi-1U}
\title{Using bookdown equations Lua filter}
% A comment in the preamble
\begin{document}
\begin{equation}\label{eq:emc.2}
    e = m c^2
\end{equation}
This is a new equation \ref{eq:emc.2}
\end{document}

output.md

---
author:
- Abhi-1U
title: Using bookdown equations Lua filter
---

$$\label{eq:emc.2}
    e = m c^2 (\#eq:emc-2)$$ This is a new equation \@ref(eq:emc-2)

Code

bookdown-equations.lua

--- bookdown-equation.lua - A Pandoc Lua filter for Converting LaTeX equation
--- labels to Bookdown Style equation labels in MathJax environment.
--- pandoc generated sample : $$\label{eq:EB} LE = R_n - G - H$$
--- filter generated equation : $$\label{eq:EB} LE = R_n - G - H (\#eq:EB)$$
--- Copyright: © 2021-2022 Abhishek Ulayil
--- License: MIT – see LICENSE for details

-- Makes sure users know if their pandoc version is too old for this
-- filter.
PANDOC_VERSION:must_be_at_least '2.9'


-- table to store all equation labels
equation_labels = {}

--[[
Converts LaTeX label to bookdown label format in the Math element
--]]
function Math(el)
    if el.text:match('label') then
        local text = pandoc.utils.stringify(el.text)
        s, e, l =string.find(text,"\\label{(.-)}")
        table.insert(equation_labels,l)
        -- Bookdown does not support period in equations hence substituting them as hyphen
        l = string.gsub(l, "%.", "-")
        el.text = text .. [[ (\#]] .. l .. [[)]]
    else
        --pass
    end
    return(el)
end

--[[
Converts Links to equations to modified format
--]]
function Link(el)
    local is_bkdwn = false
    for _,label in pairs(equation_labels) do
        if ("#"..label) == el.target then
            local link_text = el.target
            -- Bookdown does not support period in equations hence substituting them as hyphen
            link_text = string.gsub(link_text, "%.", "-")
            label = string.gsub(label, "%.", "-")
            el.target = link_text
            --bookdown type reference constructor
            bkdwn = [[\@ref(]] .. label .. [[)]]
            is_bkdwn = true
            break
        end
    end
    if is_bkdwn then
        return pandoc.RawInline('markdown', bkdwn)
    else
        return(el)
    end
end