Site icon R-bloggers

Using LaTeX in R/exams: What, Why, How?

[This article was first published on R/exams, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

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:

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:

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

install.packages("tinytex")

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

tinytex::install_tinytex()

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

  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.

To leave a comment for the author, please follow the link and comment on their blog: R/exams.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.