Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Markdown conditionals for knitting HTML vs PDF

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.

like image 337
stackinator Avatar asked Sep 05 '18 20:09

stackinator


People also ask

How do you knit a PDF in R Markdown?

Rendering. To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I knit HTML files in R?

To knit in RStudio , click the Knit pull down button. You want to use the Knit HTML option for this lesson. When you click the Knit HTML button, a window will open in your console titled R Markdown. This pane shows the knitting progress.


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.

like image 120
tarleb Avatar answered Nov 06 '22 10:11

tarleb