这里有一个使用文件预处理器
filepp的解决方案。我们定义了一个函数
img(caption, path, options)
,它会根据标志替换为
![caption](path)
或
\includegraphics
。请注意两件事:您需要在函数参数中转义逗号(请参见第一个图的第三个参数);即使未指定一个参数,您也需要有两个未转义的逗号(请参见下面的第二个示例图)。
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,)
我们可以在 filepp 命令中指定输出格式,并通过 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输出:
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输出:
<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>
echo '![](img.jpg)' | pandoc -t latex
已经输出了\includegraphics
等内容,对吗? - mb21