Search code examples
lualatexmarkdownpandoc

Removing captions in some images using Pandoc markdown to LaTeX conversion while keeping them centered


I'm using Pandoc 3.1.9 to convert a large document to PDF from markdown. The document includes a significant number of figures, and I would like some of them not to have the caption Figure X: text after it. My main problem is that figures stick to the left when they don't have a caption.

I have tried adding a class to the image:

![](./image.png){ .center width=200px }

Then using a Lua filter to check for specific classes as in:

if FORMAT:match 'latex' then
  function Image (elem)
    if elem.className == "center" then
    return {
      pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
      elem,
      pandoc.RawInline('latex', '\\par}')
    }
    end
  end
end

And then applying the filter on the Pandoc call:

pandoc --lua-filter=center.lua <myfile>.md -o <myfile>.pdf

But this won't work. Other threads have suggested removing the markdown-implicit_figures, but it does it for every image in the document, and I don't want that. Anyone can help?


Solution

  • The implicit_figures extension makes pandoc treats an image as a figure if it would otherwise be the only element in a paragraph. HTML comments count as elements, so do something like this to prevent an image from being treated as a figure:

    ![](./image.png){ .center width=200px }<!-- not a figure -->
    

    Also note that the Lua code to check for a class should be updated:

    function Image (elem)
      if elem.classes:includes "center" then
        return {
          pandoc.RawInline('latex', '\\hfill\\break{\\centering'),
          elem,
          pandoc.RawInline('latex', '\\par}')
        }
      end
    end