PandocLuaError "all choices failed" in custom pandoc writer
Asked Answered
O

1

2

I'm trying to develop a pandoc (v2.18) lua custom writer for kramdown. Kramdown uses $$ as delimiter for display and inline math and so my writer looks like:

function Writer (doc, opts)
    local filter = {
      Math = function(elem)

        local math = elem

        if elem.mathtype == 'DisplayMath' then
            local delimited = '\n$$' .. elem.text ..'$$\n'
            math = pandoc.RawBlock('markdown', delimited)
        end

        if elem.mathtype == 'InlineMath' then
            local delimited = '$$' .. elem.text ..'$$'
            math = pandoc.RawInline('markdown', delimited)
        end 
        
        return math
      end           
    }
    return pandoc.write(doc:walk(filter), 'markdown', opts)
  end

Now when trying to convert a latex test file called vector.tex this fails with the error message

$ pandoc -t kramdown.lua vector.tex -o vector.md --wrap=preserve
Error running Lua:
PandocLuaError "all choices failed"
stack traceback:
    kramdown.lua:21: in function 'Writer'

I realized that it works and I get the output I want by replacing RawBlock with RawInline like

math = pandoc.RawInline('markdown', delimited .. '\n')

So there seems to be a problem with my usage of RawBlock. I am new to pandoc and lua so maybe I'm missing something basic here. Can someone give me a hint what might be the issue here?

Outreach answered 8/6, 2022 at 14:39 Comment(0)
G
2

Using RawInline works, as Math elements are inline elements. Display math may look like a block, but internally it's still an inline. Filters must replace inline elements with other inlines, and blocks with blocks.

A "Block" is something like a paragraph, list, or block quote, while an "Inline" is text, emphasis, an image, or a link.

Sorry for the abysmal error message, I'll try to improve that.

Gemmiparous answered 8/6, 2022 at 15:18 Comment(2)
Thank you. I overlooked that completely, because from the documentation it is a bit difficult to see that pandoc.org/lua-filters.html#pandoc.math is below pandoc.org/lua-filters.html#inline . Also the urls don't reflect that hierarchy. Maybe this could be improved also.Outreach
@Outreach That's true, good points. I'll see if we can fix that as well.Gemmiparous

© 2022 - 2024 — McMap. All rights reserved.