R Markdown Latex



  1. R Markdown Latex Table
  2. R Markdown Latex Matrix
  • Some LaTeX packages work in all the various output formats for R Markdown and some don't. One of the packages that your example is using generates errors when using it in R Markdown to generate Word or HTML output but works fine when creating PDF.
  • An R Markdown Template for Academic Manuscripts. Posted on February 10, 2016 by steve in R Markdown The frontmatter to an R Markdown document. You should consider no longer using LaTeX as a front-end for your manuscripts. Use a wrapper for LaTeX instead, like R Markdown.

LaTeX is a document prepration system that is very powerful but can sometimes also be a bit confusing. Hence, some useful details are provided that should help R/exams users getting LaTeX installed and used effectively for preparing exams.

R Markdown Latex Table

What?

LaTeX is a software system for document preparation, see the corresponding Wikipedia page for an overview. The system encompasses various elements that are important to distinguish:

R Markdown Latex Matrix

In case of R/Markdown exercises in Rmd format, R/exams converts the Markdown markup to LaTeX behind the scenes (using the powerful pandoc document converter), before using a TeX engine to render the PDF output. The following comments should help you choose an appropriate way to install and interface a TeX distribution when working with R/exams.

  • It uses a markup language that can be written in any simple text editor with commands structuring a document (title, sections, etc.), formatting text (italics, bold face, etc.), and providing a powerful mathematical notation.
  • Subsequently, a TeX engine can be used to process such a text file and produce an output document that can be viewed, printed, etc. The probably most commonly-used TeX engine is pdfTeX rendering LaTeX files into PDF documents.
  • TeX distributions ship collections of such TeX engines along with further utilities, macros, and fonts for processing LaTeX files. The most widely-used TeX distributions are TeX Live available on all major operating systems and MikTeX for Windows.

Why?

In general LaTeX it is very useful for professional typesetting, especially of scientific documents, and is the most widely-used markup language for mathematical notation. However, the complexity of the LaTeX markup language can also be confusing, especially for newcomers, and hence it can be easier to use other markup languages such as Markdown for many documents. But even when using Markdown as the main markup for a document, LaTeX may still be involved in producing an output document: either only for mathematical notation (e.g., in HTML output) and/or for rendering PDF output. In short, due to its power and flexibility LaTeX is used either explicitly or implicitly in the preparation of a lot of output documents.

Consequently, R/exams leverages LaTeX, mainly for three purposes:

  • As the main markup language for authoring exercises and formatting text in R/LaTeX (Rnw) format.
    However, this is optional and the R/Markdown (Rmd) format for authoring exercises is probably more widely used. See the First Steps tutorial for a side-by-side comparison.
  • As the markup language for mathematical notation.
    This is used in both Rnw and Rmd exercises and can be displayed in various ways. See the Mathematical Notation tutorial for some guidance, especially when rendering online exams.
  • As the engine for producing PDF output.
    This is also used for both Rnw and Rmd exercises, especially when using exams2pdf() for flexible, customizable PDF output and exams2nops() for standardized PDF output that can be automatically scanned and evaluated.

How?

If you use R/exams for rendering exercises without mathematical notation into online exams then you likely do not need LaTeX at all (and can stop reading here).

If you only use mathematical notation but still only generate online HTML-based exams, especially for e-learning materials, then you need a bit of the markup language but do not actually need to install a TeX engine as part of a TeX distribution.

However, when you produce PDF output, be it from Rnw or Rmd exercises, then you need to install a TeX distribution with suitable TeX engines. In case of R/Markdown exercises in Rmd format, R/exams converts the Markdown markup to LaTeX behind the scenes (using the powerful pandoc document converter), before using a TeX engine to render the PDF output. The following comments should help you choose an appropriate way to install and interface a TeX distribution when working with R/exams.

The TeX distribution providing the TeX engine(s) can either be installed “as usual” on the system, e.g., by downloading MikTeX for Windows or installing TeX Live from a standard repository under Linux or OS X. Alternatively, the R package tinytex offers the possibility for installing a lightweight TeX distribution called “TinyTeX”, based on TeX Live. The latter option is particularly attractive for R/exams users that do not actually use LaTeX for other projects. This is due to a couple of reasons: Everything is handled through R; no special super user rights are needed for the installation; and the installation is minimal with additional style files etc. being installed automatically only if needed. To install the R package tinytex you just need

or alternatively use the graphical user interface in RStudio etc. To install the TeX distribution “TinyTeX” you just need

Finally, even when you have installed a TeX distribution (other than TinyTeX) on your system (e.g., TeX Live or MikTeX), the R package tinytex may (or may not) still be useful for producing PDF exams. Three cases need to be distinguished.

  1. System LaTeX via tools::texi2dvi()
    When: The R package tinytex is not installed or when setting options(exams_tex = 'tools').
    Why: For R/exams users that also use LaTeX for other projects and do not need any further assistance and hence do not need to introduce another dependency in their code.

  2. System LaTeX via tinytex::latexmk()
    When: The R package tinytex is installed but not the TinyTeX distribution (and the exams_tex option is not set to 'tools').
    Why: This tries to resolve some dependencies automatically (e.g., additional style files). Also, it not only supports the 'pdflatex' engine but also 'xelatex' or 'lualatex' etc., which may be useful/necessary for handling certain fonts.

  3. TinyTeX via tinytex::latexmk()
    When: The R package tinytex is installed and tinytex::install_tinytex() was used to install the TinyTeX distribution (and the exams_tex option is not set to 'tools').
    Why: For users that do not use LaTeX outside of R as it is more lightweight and very easy to install/maintain.

Achim ZeileisTUTORIALS
LaTeXMathMLMathJax

I often need to write short reports which are not full blown manuscripts, e.g. annual grant progress reports. Though such documents don’t need to adhere to a strict template, I still want them to look nice. I’ve accomplished this for years by writing directly in LaTeX, but I want to align my process with my recent transition to composing most docs in RStudio/Rmd. Ultimately though, I don’t want to abandon the LaTeX look in the compiled document. Thankfully, RStudio will render a LaTeX pdf, but formatting beyond the defaults (which are still nice!) can be a bit mysterious. This repository holds my working template for such purposes.

Here’s a minimal example of what the defaults within a .Rmd will give you:

Now, two specific things I’d like to change are:

  • Left-justify the title/author/date section
  • Modify the font specs used in section titles

A solution to these two problems easily generalizes to the broader question of “How do I format the title and H1-H6 specs in the context of LaTeX rendering from .Rmd files?”

To start, we will borrow the LaTeX template R Markdown is currently using (h/t SO). The relevant remote repo is here, and you can copy the local version you’re using into your working directory with this line:

If you look past the pandoc nastiness in this template file (I at least find it nasty, being that I was mostly unfamiliar with pandoc scripting!), you’ll see familiar LaTeX commands that are often surrounded by $if(X)$ statements that are triggered if X appears in your .Rmd YAML. Here’s a straightforward example where, if you have title: in your .Rmd YAML header, the maketitle command will be executed in your LaTeX render:

Now the problem feels more tractable: all we have to do is modify the maketitle defaults in the usual LaTeX manner within the template.tex document. Let’s start with the following:

R Markdown Latex

And don’t forget to include template.tex in your .Rmd YAML header like so:

Rendering gives the below, nice!

Next, we clearly need to fix the fact that section titles are now larger than the document title! Let’s do this with the LaTeX sectsty package – you can basically stuff this code anywhere in the preamble, like so:

Let’s also reduce the overall margins a touch via the geometry argument in the YAML while we’re at it. Here’s the full .Rmd:

Mission accomplished!

Here’s a bonus I learned along the way. You can send custom arguments to your .tex doc by simply defining new variables in the YAML header (relevant info in the pandoc docs here). Suppose I want an optional subtitle parameter. This is accomplished like so:

With this trick, you can start to do even fancier things (literally), such as include fancyhdr options. This option is ultimately included in the template.tex provided in the repository, and here’s the relevant YAML and output:

Happy R Markdowning!