rlatexmoodlernwr-exams

Use isotope latex-package inside Rnw (r-exams)


I want to load and use isotope package in my .Rnw file:

\exname{foo}
\extype{schoice}
\exsolution{0001}
\usepackage{isotope}
\begin{question}
  Foo \isotope[A][Z]{H}\ bar
  \begin{answerlist}
    \item a
    \item b
    \item c
    \item d
  \end{answerlist}
\end{question}

But when I render it into Moodle's XML (exams2moodle) the \isotope disappear and appears only Foo bar. I'm using MathML rendering.

How can I load and use correctly?


Solution

  • This short answer is: There is no way (that I know of) of rendering the \isotope command using either MathML or MathJax. See also this discussion: How to use a LaTeX package with R/exams?

    There are two possible workarounds:

    Option 1

    Compile the \isotope command using pdfLaTeX as usual, extract the image (e.g., in a SVG vector graphic), and embed it into the exercise is. All this can be done conveniently with the tex2image() function.

    The advantage is that you can use the isotope package that you are used to. However, there is a number of disadvantages: The rendering is rather slow, especially if you need many such images.The scaling of the graphic might not match the scaling of the text, especially when zooming into the HTML. The kind of graphic you need will depend on the kind of output (HTML vs. PDF) etc.

    Option 2

    Rather than using a specialized package, mimic the output using standard LaTeX commands. In this case using the \sideset command from amsmath would be one option because the amsmath package is supported by MathJax (but not in the MathML converters). Disadvantage: The LaTeX code is slightly more cumbersome. Advantages: Very fast, can be scaled in the HTML, works also in PDF. Hence, I would recommend Option 2.

    Example

    Below is the adapted code of your foo.Rnw file. This can be rendered into HTML via:

    exams2html("foo.Rnw", converter = "pandoc-mathjax")
    

    This is the same converter that current versions of R/exams use in exams2moodle() as well.

    <<echo=FALSE, results=hide>>=
    tex2image("\\isotope[A][Z]{H}", packages = "isotope",
      name = "iso_AZH", format = "svg", dir = ".")
    @
    
    \begin{question}
    Option 1: \includegraphics{iso_AZH.svg}
    
    Option 2: $\sideset{_Z^A}{}H$
    \begin{answerlist}
      \item a
      \item b
      \item c
      \item d
    \end{answerlist}
    \end{question}
    
    \exname{foo}
    \extype{schoice}
    \exsolution{0001}