用于编织HTML与PDF的R Markdown条件

时间:2022-11-16 12:05:56

In LaTeX I can create conditionals in the following manner

在LaTeX中,我可以通过以下方式创建条件

  \iftoggle{ebook}{
    \newcommand{\textbreak}{\newline\hrule\newline}
  }{
    \newcommand{\textbreak}{\begin{center}\LARGE{$\Psi\quad\Psi\quad\Psi$}\end{center}}
  }

Can I do the same while knitting R Markdown, depending on if the output, is say HTML or PDF.

我可以在编织R Markdown时执行相同的操作,具体取决于输出是HTML还是PDF。

1 个解决方案

#1


10  

If you just need to include a short command in your target format, then you could use raw elements for your target format:

如果您只需要在目标格式中包含一个简短命令,那么您可以使用原始元素作为目标格式:

`<br><hr><br>`{=html}
`\begin{center}\LARGE{$\Psi\quad\Psi\quad\Psi$}\end{center}`{=latex}

The first line will only be included in HTML formats (like epub), while the latter will be used when exporting to or via LaTeX.

第一行仅包含在HTML格式(如epub)中,而后者将在导出到LaTeX或通过LaTeX时使用。

For longer text, or if you don't want to write in the target format directly, I'd recommend using fenced divs in combination with a pandoc filter, e.g. a Lua filter; this works both with raw pandoc as well as with RMarkdown.

对于较长的文本,或者如果您不想直接以目标格式书写,我建议将fenced div与pandoc过滤器结合使用,例如: Lua过滤器;这适用于原始pandoc以及RMarkdown。

#1


10  

If you just need to include a short command in your target format, then you could use raw elements for your target format:

如果您只需要在目标格式中包含一个简短命令,那么您可以使用原始元素作为目标格式:

`<br><hr><br>`{=html}
`\begin{center}\LARGE{$\Psi\quad\Psi\quad\Psi$}\end{center}`{=latex}

The first line will only be included in HTML formats (like epub), while the latter will be used when exporting to or via LaTeX.

第一行仅包含在HTML格式(如epub)中,而后者将在导出到LaTeX或通过LaTeX时使用。

For longer text, or if you don't want to write in the target format directly, I'd recommend using fenced divs in combination with a pandoc filter, e.g. a Lua filter; this works both with raw pandoc as well as with RMarkdown.

对于较长的文本,或者如果您不想直接以目标格式书写,我建议将fenced div与pandoc过滤器结合使用,例如: Lua过滤器;这适用于原始pandoc以及RMarkdown。