Skip to content

Instantly share code, notes, and snippets.

@rriemann
Created February 4, 2020 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rriemann/2dfa7f4b1147d7f3fad506cf6f863cd7 to your computer and use it in GitHub Desktop.
Save rriemann/2dfa7f4b1147d7f3fad506cf6f863cd7 to your computer and use it in GitHub Desktop.
Pandoc Lua Filter to convert Some Definition Lists to Latex theorem-like Environments and some references to cref tags
-- #!/usr/bin/env lua
-- Pandoc filter to add recommendation blocks in Latex and recognise cleveref references (cref)
--
-- Markdown Example:
--
-- Recommendation (Use of Pandoc) {#rec:pandoc}
-- : Pandoc is a nice editor for several reasons:
--
-- - fast
-- - lua support
--
-- When choosing your toolchain, please consider @rec:pandoc.
function Cite (elt)
if elt.citations[1].id:match('^rec:') then
local ids = {}
for i,citation in ipairs(elt.citations) do
ids[i] = citation.id
end
if FORMAT:match('latex') then
return pandoc.RawInline('latex', '\\cref{'..table.concat(ids, ',')..'}')
elseif FORMAT:match('html') then
-- TODO
end
end
end
function DefinitionList (elt)
local header = elt.content[1][1]
if header[1].text:match '^Recommendation' then
local headertext = ''
for i,s in ipairs(header) do
headertext = headertext .. (s.text or ' ')
end
local caption = headertext:match('%((.-)%)') or ''
local label = headertext:match('%{#(.-)%}')
local labeltag = ''
if label ~= nil then
labeltag = '\\label{'..label..'}'
end
local content = table.move(elt.content[1], 2, #elt.content[1], 1, {})
content = content[1][1] -- TODO: dismisses 2nd and later def list items
if FORMAT:match('latex') then
table.insert(content, 1, pandoc.RawBlock('latex', '\\begin{recommendation}[' .. caption .. ']' .. labeltag))
table.insert(content, pandoc.RawBlock('latex', '\\end{recommendation}'))
return content
elseif FORMAT:match('html') then
-- TODO
end
end
end
% put this somewhere in the header
% recommendation environment, need to load before cleveref
\usepackage{amsthm}
% cleveref in-document links
\usepackage[
capitalise,
nameinlink,
noabbrev,
]{cleveref}
% recommendation envirnoment must be loaded after cleveref,
% see http://ctan.triasinformatica.nl/macros/latex/contrib/cleveref/cleveref.pdf sec. 14.1
\theoremstyle{definition} % set the style of subsequent theorem-like environments
\newtheorem{recommendation}{Recommendation}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment