Skip to content

Instantly share code, notes, and snippets.

@tarleb
Last active June 14, 2019 22:43
Show Gist options
  • Select an option

  • Save tarleb/a57366474bbeef4c4c388adc19e85a41 to your computer and use it in GitHub Desktop.

Select an option

Save tarleb/a57366474bbeef4c4c388adc19e85a41 to your computer and use it in GitHub Desktop.

Revisions

  1. tarleb revised this gist Jan 20, 2018. 1 changed file with 24 additions and 31 deletions.
    55 changes: 24 additions & 31 deletions latex-short-captions.lua
    Original file line number Diff line number Diff line change
    @@ -3,48 +3,41 @@ if FORMAT ~= "latex" then
    return {}
    end

    function latex(str)
    return pandoc.RawInline('latex', str)
    end
    local List = require'pandoc.List'

    -- Append `list2` to `list1` by modifying `list1`.
    function append(list1, list2)
    local n1 = #list1
    for i = 1, #list2 do
    list1[n1 + i] = list2[i]
    end
    return list1
    local function latex(str)
    return List:new{pandoc.RawInline('latex', str)}
    end

    function make_caption(long_caption, short_caption)
    local caption = {latex '\\caption['}
    append(caption, short_caption)
    table.insert(caption, latex ']{')
    append(caption, long_caption)
    table.insert(caption, latex '}\n')
    return caption
    end

    function is_titled_figure(para)
    local function is_titled_figure(para)
    return #para.content == 1 and
    para.content[1].t == "Image" and
    para.content[1].title
    end

    local latex_figure_start = latex '\\begin{figure}\n\\centering\n'
    local latex_figure_end = latex '\\end{figure}'
    local function read_inlines(txt)
    return pandoc.read(txt).blocks[1].content
    end

    function Para(para)
    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]
    img.title = img.title:gsub('^fig:', '')
    local title_inlines = pandoc.read(img.title).blocks[1].content

    local figure = {latex_figure_start}
    append(figure, {img, latex '\n'})
    append(figure, make_caption(img.caption, title_inlines))
    append(figure, {latex_figure_end})
    return pandoc.Plain(figure)
    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}}
  2. tarleb created this gist Nov 22, 2017.
    50 changes: 50 additions & 0 deletions latex-short-captions.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    -- don't do anything unless we target latex
    if FORMAT ~= "latex" then
    return {}
    end

    function latex(str)
    return pandoc.RawInline('latex', str)
    end

    -- Append `list2` to `list1` by modifying `list1`.
    function append(list1, list2)
    local n1 = #list1
    for i = 1, #list2 do
    list1[n1 + i] = list2[i]
    end
    return list1
    end

    function make_caption(long_caption, short_caption)
    local caption = {latex '\\caption['}
    append(caption, short_caption)
    table.insert(caption, latex ']{')
    append(caption, long_caption)
    table.insert(caption, latex '}\n')
    return caption
    end

    function is_titled_figure(para)
    return #para.content == 1 and
    para.content[1].t == "Image" and
    para.content[1].title
    end

    local latex_figure_start = latex '\\begin{figure}\n\\centering\n'
    local latex_figure_end = latex '\\end{figure}'

    function Para(para)
    if not is_titled_figure(para) then
    return nil
    end
    local img = para.content[1]
    img.title = img.title:gsub('^fig:', '')
    local title_inlines = pandoc.read(img.title).blocks[1].content

    local figure = {latex_figure_start}
    append(figure, {img, latex '\n'})
    append(figure, make_caption(img.caption, title_inlines))
    append(figure, {latex_figure_end})
    return pandoc.Plain(figure)
    end