Skip to content

Instantly share code, notes, and snippets.

@tarleb
Last active June 14, 2019 22:43
Show Gist options
  • Save tarleb/a57366474bbeef4c4c388adc19e85a41 to your computer and use it in GitHub Desktop.
Save tarleb/a57366474bbeef4c4c388adc19e85a41 to your computer and use it in GitHub Desktop.
Filter to turn figure titles into short captions in LaTeX
-- don't do anything unless we target latex
if FORMAT ~= "latex" then
return {}
end
local List = require'pandoc.List'
local function latex(str)
return List:new{pandoc.RawInline('latex', str)}
end
local function is_titled_figure(para)
return #para.content == 1 and
para.content[1].t == "Image" and
para.content[1].title
end
local function read_inlines(txt)
return pandoc.read(txt).blocks[1].content
end
local function make_caption(long_caption, short_caption)
return latex'\\caption['
.. short_caption .. latex']{'
.. long_caption .. latex'}\n'
end
local function figure_with_short_caption(para)
if not is_titled_figure(para) then
return nil
end
local img = para.content[1]
local title_inlines = read_inlines(img.title:gsub('^fig:', ''))
return pandoc.Plain(
latex'\\begin{figure}\n\\centering\n'
.. {img}
.. latex'\n'
.. make_caption(img.caption, title_inlines)
.. latex'\\end{figure}'
)
end
return {{Para = figure_with_short_caption}}
@LukasCBossert
Copy link

Your filter works great with single figures. But it will throw errors having subfigures.

See pandoc/lua-filters#62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment