Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional sections in Pandoc markdown

Tags:

pandoc

I want some sections of my document to be only for a given output format. For instance, I have an image that requires special LaTeX treatment and i want to use \includegraphics for it. But for the epub version, I want the standard behavior.

Is there a way in Pandoc markdown to specify a section that should be processed only in a given context (LaTex vs epub). Templates have conditionals. I did not see the feature for Pandoc markdown.

Or is there another way of dealing with this use case?

like image 357
Arnaud Avatar asked Jan 08 '23 16:01

Arnaud


1 Answers

Here is a solution with the file preprocessor filepp. We define a function img(caption,path,options) that is replaced by ![caption](path) or by \includegraphics depending on a flag. Notice two things: you need to escape commas in the function arguments (see the third argument of the first plot); you need to have two unescaped commas, even if one argument is not specified (see the second example plot below).

test.md

#bigfunc img(caption,path,options)
#if "out_fmt" eq "tex"
\begin{figure}
   \caption{\label{caption} caption}
   \includegraphics[options]{path}
\end{figure}
#else
![caption](path)
#endif
#endbigfunc

Here is an image

img(Plot,Rplot.png,height=2\,width=2)
img(Plot,Rplot.png,)

We specify the ouput format in the filepp command and pipe it through pandoc:

filepp -m bigfunc.pm -D out_fmt=tex test.md | pandoc -o test.tex
filepp -m bigfunc.pm -D out_fmt=html test.md | pandoc -o test.html

tex output:

Here is an image

\begin{figure}
   \Plot{\label{Plot} Plot}
   \includegraphics[height=2,width=2]{Rplot.png}
\end{figure}

\begin{figure}
   \Plot{\label{Plot} Plot}
   \includegraphics[]{Rplot.png}
\end{figure}

html output:

<p>Here is an image</p>
<div class="figure">
<img src="Rplot.png" alt="Plot" />
<p class="caption">Plot</p>
</div>
<div class="figure">
<img src="Rplot.png" alt="Plot" />
<p class="caption">Plot</p>
</div>
like image 78
scoa Avatar answered Mar 16 '23 05:03

scoa