[Tex/LaTex] With Pandoc, how to apply a style to a “fenced div” block

formattingpandoc

I use Pandoc to generate an output from a Markdown file to many outputs: html, word (.docx), latex, pdf, etc.

I tried using the "fenced div" feature to style differently some parts of the text. For example:


blablabla.

:::: special

Some special text, with markdown formatting.

::::


To style that block with Html, it's fairly straight forward. I have a template.css file that includes a .special {background-color: #aaaaaa;} line and it's done (it's referenced in my command line arguments).

I also have a template.latex file, but I'm less fluent in all things Latex. I've tried in many ways and couldn't find how to tweak it to apply a style to pandoc fenced divs. In the raw Latex file that it outputs, there's not even a command indicating that there was something there. It's just ignored.

I've searched a long time for this answer, and I can't find any clue. Thanks in advance.

Best Answer

You need a lua filter to process the fenced div:

function Div(el)
  if el.classes[1] == "special" then
    -- insert element in front
    table.insert(
      el.content, 1,
      pandoc.RawBlock("latex", "\\begin{Special}"))
    -- insert element at the back
    table.insert(
      el.content,
      pandoc.RawBlock("latex", "\\end{Special}"))
  end
  return el
end

This will transform the div to a latex environment:

blablabla.

\begin{Special}

Some special text, \emph{with markdown formatting.}

\end{Special}

You mustn't forget do define the environment in your template:

\usepackage{mdframed}
\newenvironment{Special}%
  {\begin{mdframed}[backgroundcolor=lightgray]}%
  {\end{mdframed}}

enter image description here