9.1 LaTeX or HTML output
LaTeX and HTML are two commonly used output formats. The function knitr::is_latex_output()
tells you if the output format is LaTeX (including Pandoc output formats latex
and beamer
). Similarly, the function knitr::is_html_output
tells you if the output format is HTML. By default, these Pandoc output formats are considered HTML formats: markdown
, epub
, html
, html4
, html5
, revealjs
, s5
, slideous
, and slidy
. If you do not think a certain Pandoc format is HTML, you may use the excludes
argument to exclude it, e.g.,
If a certain output element can only be generated in LaTeX or HTML, you can use these functions to conditionally generate it. For example, when a table is too big on a PDF page, you may include the table in an environment of a smaller font size, but such a LaTeX environment certainly will not work for HTML output, so it should not be included in HTML output (if you want to tweak the font size for HTML output, you may use CSS). Below is a full example:
The key in the above example is the chunk option include = knitr::is_latex_output()
. That is, the environment begin{tiny} end{tiny}
is only included when the output format is LaTeX. The two tables in the example will look identical when the output format is not LaTeX.
Upmath extremely simplifies this task by using Markdown and LaTeX. It converts the Markdown syntax extended with LaTeX equations support into HTML code you can publish anywhere on the web. Upmath extremely simplifies this task by using Markdown and LaTeX. It converts the Markdown syntax extended with LaTeX equations support into HTML code you can publish anywhere on the web. Definition from Wikipedia: Markdown is a lightweight markup language with plain text formatting syntax designed so that it can be converted to HTML and many other formats using a tool.
In Section 5.1, we used these functions to change the text color for HTML and LaTeX output. In Section 4.14, we showed an animation example, which also used this trick. The code chunk that generated the animation for HTML output and static images for LaTeX output is like this:
Python Markdown Latex
Markdown Latex Cheat Sheet
These conditional functions can be used anywhere. You can use them in other chunk options (e.g., eval
for conditional evaluation of the chunk), or in your R code, e.g.,