lukeflo
lukeflo

Reputation: 175

Access values of pandoc.Figure elements in pandoc.RawBlock with custom Lua writer

To solve my concrete question (asked also on the Pandoc mailing list, which, unfortunatley, seems to be down due to spam mails at the moment) regarding using the Latex \sidecaption command in a figure element exported to Latex, I'm now trying to understand the processing of Lua writers/filters and especially the accessing of field values for Pandoc more generally.

The main question is, how can I access the values of the fields of a pandoc.Figure to rearrange them in the output through a custom writer; especially the fields content (image), caption and attr. I checked the pandoc docs and some examples from the web, but couldn't find many maybe due to the overhaul for figures in Pandoc 3.0.

So far I got the following code:

function Writer (doc, opts)
  local filter = {
     Figure = function (fig)
    local tester = '\\begin{figure*}\n' .. fig.content .. '\n\\end{figure*}\n' -- or/and fig.caption
    return pandoc.RawBlock('latex', tester)
    end
  }
  return pandoc.write(doc:walk(filter), 'latex', opts)
end

If I call the writer (together with my custom template) like this pandoc --template=mytemplate -t test-writer.lua ast-test.txt -o ast-test.tex, I get the error bad argument #2 to 'concat' (table expected, got string) for the line containing the code when called with fig.content; and the error attempt to concatenate a table value (field 'caption') when called with fig.caption; for the latter case I also tried fig.caption.long which also results in (table expected, got string).

But when I call it without any fig. content, I get an empty {figure*} (starred!) Latex environment in the output file without any error messages. Thus, the method seems to work, but I just can't figure out how to access the field values of the Figure block.

The test file ast-test.txt only contains a few lines:

---
title: A title
---

# Einleitung

Some text

![caption *to* an image](counter_plot_new_periods.png)

Edit

I tried an approach using a Lua filter instead of a Lua custom writer (inspired from this answer by @tarleb):

function Figure (elem)
  raw_elem = '\\begin{figure*}\n' ..
              '\\centering\n' .. 
              '\\includegraphics[width=0.9\\textwidth]{' .. elem.content .. '}\n' .. 
              '\\caption{' .. elem.caption .. '}\n' .. 
              '\\end{figure*}'
  return pandoc.RawBlock('latex', raw_elem)
end

but get the same error. This version also works if I remove the elem.(...) parts and renders the Latex commands correctly in the .tex file. But of course I need the values of the fields from the figure block to get a working Latex document.

2nd Edit:

I played around a little bit more and at least got the content of the caption with pandoc.utils.stringify:

Figure = function(elem)
   Caption = pandoc.utils.stringify(elem.caption.long)
   local raw_elem = '\\begin{figure*}\n' .. Caption .. '\n\\end{figure*}'
   return pandoc.RawBlock('latex', raw_elem)
end

This outputs the content of elem.caption.long but, as mentioned in the docs, erases all formatting, thus, also the emphasised "to" in the caption.

Furthermore, this is no solution for the image content of the Figure which is a pandoc function on its own, which should be preserved inside the edited Figure function...


I appreciate any hint how to access those Figure field values correctly in Lua!

Upvotes: 2

Views: 158

Answers (0)

Related Questions