【问题标题】:Conditional sections in Pandoc markdownPandoc markdown 中的条件部分
【发布时间】:2015-08-25 21:38:02
【问题描述】:

我希望文档的某些部分仅适用于给定的输出格式。例如,我有一个需要特殊 LaTeX 处理的图像,我想为它使用 \includegraphics。但是对于 epub 版本,我想要标准的行为。

Pandoc markdown 中有没有办法指定只应在给定上下文中处理的部分(LaTex 与 epub)。模板有条件。我没有看到 Pandoc markdown 的功能。

或者还有其他方法可以处理这个用例吗?

【问题讨论】:

  • echo '![](img.jpg)' | pandoc -t latex 已经输出了\includegraphics 等等,对吧?
  • 是的,但是如何将一些参数传递给 \includegraphics,例如修改大小,从降价?

标签: pandoc


【解决方案1】:

这是使用文件预处理器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>

【讨论】:

    【解决方案2】:

    有几种可能:

    【讨论】:

    • 我设法通过在我的降价文件\ifdef{TEX} ...\ifdef{EPUB} ... 中添加以下行来使用gpp 使其工作,然后您可以使用gpp -T -DEPUBgpp -T -DTEX。但这不再是有效的降价。当我pandoc 它时,它抱怨 \ifdef 没有定义。有什么方法可以让 gpp 预处理命令对 markdown 友好?
    • 这个想法是首先运行 gdp 来处理 &lt;#ifdef TEX&gt;...&lt;#endif&gt; 并生成不再包含它们的有效降价。然后在上面运行 pandoc。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2021-08-26
    • 1970-01-01
    相关资源
    最近更新 更多