Pandoc markdown中的条件部分

4
我希望我的文档中的某些部分只适用于特定的输出格式。例如,我有一张图片需要特殊的LaTeX处理,我想使用\includegraphics来处理它。但对于epub版本,我希望使用标准行为。
在Pandoc markdown中是否有一种指定应仅在给定上下文(LaTeX vs epub)中处理的部分的方法?模板有条件语句。我没有看到Pandoc markdown的此功能。
或者还有其他解决此问题的方法吗?

echo '![](img.jpg)' | pandoc -t latex 已经输出了 \includegraphics 等内容,对吗? - mb21
是的,但我怎样才能在Markdown中向\includegraphics传递一些参数,例如修改大小? - Arnaud
更适合于http://tex.stackexchange.com/。 - Dave Jarvis
2个回答

5
这里有一个使用文件预处理器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>

4
有几种可能性:

我使用gpp成功地使它工作,通过在我的markdown文件中添加以下行: \ifdef{TEX} ...\ifdef{EPUB} ... 然后你可以使用 gpp -T -DTEXgpp -T -DEPUB。但这不再是有效的markdown了。当我用pandoc转换时,它会抱怨\ifdef未定义。有没有办法使gpp预处理命令更符合markdown的规范? - Arnaud
1
首先运行gdp,该程序处理<#ifdef TEX>...<#endif>并生成不再包含它们的有效markdown。然后在此上运行pandoc。 - mb21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接