Pandoc: how to generate a different \begin{figure} \end{figure} environment

pandoc

I have a question similar to this one.

I have a markdown document and want to convert it to pdf. I found the figures are placed in a position where latex things they fit best, but I want them to appear exactly where the tag is in the markdown code.
I would like pandoc to generate the following figure code:

\begin{figure}[H]
\centering
\fbox{\includegraphics[scale=0.4]{somepic.png}}
\caption{Some caption}
\end{figure}

I would like the scale argument, the [H], the fbox. And also, pandoc create a "Figure N" caption, what about other languages?
I have reviewed a lot of posts on this matter but I have really hard times with the latex solutions proposed (I have some latex skill, not an expert though, I just want to create a decent pdf from markdown).
Thanks.

Best Answer

Please make sure to read Is float placement [H] considered heinous? before proceeding.

Ok then. If you really want to do this, write a lua filter along the lines of short-captions.lua:

if FORMAT ~= "latex" then
  return
end

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

function figure_image(elem)
  local image = elem.content and elem.content[1]
  return (image.t == 'Image' and image.title == 'fig:')
      and image
      or nil
end

function Para(para)
  local img = figure_image(para)
  if not img or not img.caption then
    return nil
  end

  return pandoc.Para {
    latex("\\begin{figure}[H]\n\\centering\n"),
    latex("\\fbox{"), img, latex("}"),
    latex("\n\\caption"), pandoc.Span(img.caption),
    latex("\n\\end{figure}\n")
  }
end

Which will transform ![Some caption](somepic.png){width=40%} to

\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.4\textwidth,height=\textheight]{somepic.png}}
\caption{Some caption}
\end{figure}