pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
614 stars 167 forks source link

short-captions Lua filter not working in list of figures #273

Open leowill01 opened 12 months ago

leowill01 commented 12 months ago

im trying to render an R Markdown document using the short-captions.lua Lua filter. the render is using bookdown references and is using the pandoc version that comes with RStudio. THE PROBLEM = the figure caption in the list of figures (as inserted with \listoffigures) is the long caption, not the short caption specified in the {} attributes after the image link.

here's a minimal reprex of the .rmd doc:

---
title: "_TEST pandoc lua filter short-captions"

output:
  bookdown::pdf_document2:
    latex_engine: xelatex
    pandoc_args:
    - "--lua-filter=/PATH/TO/short-captions.lua"
---

\listoffigures

![(ref:my-fig)](assets/test-img.png){#fig:my-fig short-caption="short fig caption"}

(ref:my-fig) LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION LONG FIG CAPTION.

here is the rendering command used by RStudio:

==> rmarkdown::render('/******/_TEST-pandoc-lua-filter-short-captions/_TEST-lua-short-captions-minreprex.rmd',  encoding = 'UTF-8');

/Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc +RTS -K512m -RTS _TEST-lua-short-captions-minreprex.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output _TEST-lua-short-captions-minreprex.tex --lua-filter /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/bookdown/rmarkdown/lua/custom-environment.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --embed-resources --standalone --table-of-contents --toc-depth 2 --number-sections --highlight-style tango --pdf-engine lualatex --variable graphics --wrap preserve --lua-filter=/******/short-captions.lua --variable 'geometry:margin=1in' --variable tables=yes --standalone -Mhas-frontmatter=false 

processing file: _TEST-lua-short-captions-minreprex.rmd
output file: _TEST-lua-short-captions-minreprex.knit.md

Output created: _TEST-lua-short-captions-minreprex.pdf

ive uploaded my PDF output: _TEST-lua-short-captions-minreprex.pdf

i am running:

leowill01 commented 12 months ago

workaround found here: https://github.com/jgm/pandoc/issues/7915#issuecomment-1427113349

EDIT: this workaround was not able to render markdown or latex expression syntax in the short captions in the LOF, so here is modified script from that workaround with that added functionality (originally posted here) :

PANDOC_VERSION:must_be_at_least '3.1'

if FORMAT:match 'latex' then
  function Figure(f)
    local short = f.content[1].content[1].attributes['short-caption']
    if short and not f.caption.short then
      -- Parse the short caption as Markdown to handle formatting and then convert to LaTeX
      local short_caption = pandoc.read(short, 'markdown').blocks[1].content
      f.caption.short = pandoc.Inlines(short_caption)
    end  
    return f
  end  
end