Site icon R-bloggers

Mathematical Notation in Online R/exams

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

Many R/exams exercises employ mathematical notation that needs to be converted and rendered suitably for inclusion in online exams. While R/exams attempts to set suitable defaults, an overview is provided of possible adjustments and when these might be useful or even necessary.

Overview

A popular use case of the R/exams package is the generation of dynamic exercises for online learning management systems in large-scale courses in mathematics, statistics, or physics. Often, these contain some mathematical notation using LaTeX markup. While LaTeX can be easily rendered into PDF for printing written exams, the options for rendering it into HTML for online exams are somewhat more diverse and the best strategies depend on a number of factors. As these involve a number of technical details this tutorial aims to give a brief overview and makes a few practical recommendations. Factors include the following:

Why does all of this matter?

As a simple illustration of the strengths and weaknesses of the different approaches, the deriv exercise template (computation of a derivative using the product rule) is converted to HTML using exams2html(). Here, the R/LaTeX version of the exercise is used ("deriv" or equivelently "deriv.Rnw") but using the R/Markdown version ("deriv.Rmd") yields almost the same output.

The following examples and resulting screenshots contrast the output between Firefox and Chrome. By clicking on the screenshots you can also see what the HTML pages look like in your own browser.

MathML rendered by browser

By default, exams2html() generates HTML with MathML embedded and uses a template that does not enable MathJax.

library("exams")
set.seed(1090)
exams2html("deriv")

The screenshots that the native MathML support in Firefox leads to output that renders fast and smoothly. However, the lack of native MathML support in Chrome means that the exercise cannot be displayed correctly.

MathML rendered by MathJax

To easily explore the effect of MathJax rendering exams2html() supports the argument mathjax = TRUE which inserts the MathJax <script> into the template (loaded from RStudio’s content delivery network).

set.seed(1090)
exams2html("deriv", mathjax = TRUE)

Now the math output looks good in both browsers. However, for Firefox users the version without MathJax might still be preferable as it renders faster with somewhat smoother output.

LaTeX rendered by MathJax

To preserve the LaTeX equations, the argument converter = "pandoc-mathjax" can be used. Then, Pandoc converts the LaTeX text to HTML but preserves the LaTeX equations (in minimal HTML markup).

set.seed(1090)
exams2html("deriv", converter = "pandoc-mathjax", mathjax = TRUE)

The output is very similar to the MathML rendered MathJax above. However, note that the alignment in the equations is changed (from left to right). This is caused by Pandoc replacing the LaTeX {eqnarray*} environment employed in the "deriv" exercise by {aligned} environments.

Practical considerations

Further technical details

The mathematical equation in the random draw of the deriv exercise in LaTeX is: f(x) = x^{8} e^{3.4x}. Here, we highlight that all converters yield almost equivalent output when rendered by MathJax:

"pandoc-mathjax" "ttm" "pandoc-mathml"
\(f(x) = x^{8} e^{3.4x}\) < math xmlns="http://www.w3.org/1998/Math/MathML">< mrow>< mi>f< mo stretchy="false">(< mi>x< mo stretchy="false">)< mo>=< msup>< mrow>< mi>x< mrow>< mn>8< msup>< mrow>< mi>e< mrow>< mn>3< mo>.< mn>4< mi>x < math display="inline" xmlns="http://www.w3.org/1998/Math/MathML">< semantics>< mrow>< mi>f< mo stretchy="false" form="prefix">(< mi>x< mo stretchy="false" form="postfix">)< mo>=< msup>< mi>x< mn>8< msup>< mi>e< mrow>< mn>3.4< mi>x< annotation encoding="application/x-tex">f(x) = x^{8} e^{3.4x}

(Note: If you are viewing this on R-bloggers or another aggregator some or all of the equations will not display correctly. Refer to the R/exams site for a version with MathJax properly enabled.)

The underlying LaTeX code generated by converter = "pandoc-mathjax" is simply the original LaTeX code with some minimal HTML markup:

<span class="math inline">\(f(x) = x^{8} e^{3.4x}\)</span>

The MathML code generated by converter = "ttm" differs slightly from the of converter = "pandoc" (or equivalently "pandoc-mathml"). The former yields:

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>f</mi><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo><mo>=</mo>
<msup><mrow><mi>x</mi></mrow><mrow><mn>8</mn></mrow>
</msup>
<msup><mrow><mi>e</mi></mrow><mrow><mn>3</mn><mo>.</mo><mn>4</mn><mi>x</mi></mrow>
</msup>
</mrow></math>

The Pandoc version is very similar but contains some more markup and annotation:

<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><semantics>
<mrow><mi>f</mi>
<mo stretchy="false" form="prefix">(</mo><mi>x</mi><mo stretchy="false" form="postfix">)</mo>
<mo>=</mo>
<msup><mi>x</mi><mn>8</mn></msup><msup><mi>e</mi><mrow><mn>3.4</mn><mi>x</mi></mrow></msup>
</mrow><annotation encoding="application/x-tex">f(x) = x^{8} e^{3.4x}</annotation>
</semantics></math>

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.