R Markdown conditionals for knitting HTML vs PDF

Viewed 344

In LaTeX I can create conditionals in the following manner

  \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.

1 Answers

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.

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.

Related