Using LaTeX in R/exams: What, Why, How?
This article is originally published at https://www.R-exams.org/
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:
- 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 usingexams2pdf()
for flexible, customizable PDF output andexams2nops()
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
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.
System LaTeX via
tools::texi2dvi()
When: The R packagetinytex
is not installed or when settingoptions(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.System LaTeX via
tinytex::latexmk()
When: The R packagetinytex
is installed but not the TinyTeX distribution (and theexams_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.TinyTeX via
tinytex::latexmk()
When: The R packagetinytex
is installed andtinytex::install_tinytex()
was used to install the TinyTeX distribution (and theexams_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.
Thanks for visiting r-craft.org
This article is originally published at https://www.R-exams.org/
Please visit source website for post related comments.