Skip to content

Instantly share code, notes, and snippets.

@const-ae
Last active August 2, 2024 15:08
Show Gist options
  • Select an option

  • Save const-ae/752ad85c43d92b72865453ea3a77e2dd to your computer and use it in GitHub Desktop.

Select an option

Save const-ae/752ad85c43d92b72865453ea3a77e2dd to your computer and use it in GitHub Desktop.
Pandoc LUA filter to replace equation references with numbers
-- Based on https://github.com/openjournals/inara/blob/f47cfd4e87d5d1afda9db353966b5102f9c941f2/data/filters/resolve-references.lua
-- Bail if we are converting to LaTeX.
if FORMAT:match('latex') then
return {}
end
local nequations = 0
local equation_labels = {}
local function find_label (txt)
local before, label, after = txt:match '(.*)\\label%{(.-)%}(.*)'
return label, label and before .. after
end
return {
{
Math = function (m)
if m.mathtype == pandoc.DisplayMath then
nequations = nequations + 1
local label, stripped = find_label(m.text)
if label then
equation_labels[label] = "(" .. tostring(nequations) .. ")"
end
end
return m
end
}, {
Link = function (link)
local ref = link.attributes.reference
if not ref then
return nil
end
if link.attributes['reference-type'] == 'eqref' and equation_labels[ref] then
link.content = equation_labels[ref]
end
return link
end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment