Search code examples
canvaslatextikzr-exams

Is there a way to use LaTeX packages in the exams2canvas of R/exams?


I'm trying to setup up an exam using R/exams, using the function exams2canvas(). My questions contain LaTeX code that requires to use some packages (in particular tikz and tikz-qtree and a few other tikz libraries) but I cannot figure out how to import them.

To create a pdf, I manually modified the tex template (plain.tex) in the exams package of R, by adding the following two lines:

\usepackage{tikz}
\usepackage{tikz-qtree}

Could I perhaps modify the canvas file in a similar way? Is there another way that I can use LaTeX packages in exams2canvas()?


Minimal example:

\begin{question}
\Tree[.S [.NP [.D the ] [.N children ] ] [.VP [.V study ] ]  [.NP [.N books ] ]  ]
\begin{answerlist}
  \item 0.7
  \item 0.2
  \item 0.1
\end{answerlist}
\end{question}

\begin{solution}
\begin{answerlist}
  \item False
  \item False
  \item True
\end{answerlist}
\end{solution}

\exname{Trees}
\extype{schoice}
\exsolution{001}

Solution

  • The problem is that for Canvas output, just like for other HTML-based formats, the LaTeX code needs to be converted to HTML. And the HTML converters we use (tth and pandoc) both just support a limited number of LaTeX commands beyond the base LaTeX distribution.

    Thus, you need to compile tikz graphics in LaTeX and then convert them to a graphics format supported by HTML, e.g., SVG vector graphics or alternatively raster graphics such as PNG or JPG. This functionality is offered by the include_tikz() function in R/exams.

    I have modified your exercise so that the tikz code is only included as LaTeX for exams2pdf() and exams2nops() - which then need to be adapted to load tikz and tikz-qtree. Otherwise, the tikz code is rendered to SVG for which magick plus pdf2svg is used. Alternatively, you could also render to PNG, for example.

    So you can do:

    exams2html("tikz_tree.Rnw")
    

    or

    exams2nops("tikz_tree.Rnw", usepackage = c("tikz", "tikz-qtree"))
    

    etc.

    exams2canvas() will work like exams2html(). The modified tikz_tree.Rnw file is included at the end of the post. Some further pointers and details (including an R/Markdown version of the exercise) are available in this discussion in the R/exams forum on R-Forge: https://R-Forge.R-project.org/forum/forum.php?thread_id=33909&forum_id=4377&group_id=1337

    Some similar but more elaborate exercise templates are available on the R/exams web page:
    http://www.R-exams.org/templates/automaton/
    http://www.R-exams.org/templates/logic/

    tikz_tree.Rnw

    <<echo=FALSE, results=hide>>=
    ## determine the output type depending on exams2xyz interface:
    ## - plain .tex for exams2pdf, exams2nops which then need to use packages tikz and tikz-qtree
    ## - .svg for other HTML-based interfaces
    typ <- if(match_exams_call() %in% c("exams2pdf", "exams2nops")) "tex" else "svg"
    
    ## TikZ code (note that backslashes need to be escaped"
    tikz_tree <- "\\Tree[.S [.NP [.D the ] [.N children ] ] [.VP [.V study ] ]  [.NP [.N books ] ]  ]"
    @
    
    \begin{question}
    
    <<echo=FALSE, results=tex>>=
    include_tikz(tikz_tree, name = "tik_tree", format = typ,
      packages = "tikz-qtree", width = "5cm")
    @
    
    \begin{answerlist}
      \item 0.4
      \item 0.2
      \item 0.9
    \end{answerlist}
    \end{question}
    
    \begin{solution}
    \begin{answerlist}
      \item False
      \item False
      \item True
    \end{answerlist}
    \end{solution}
    
    \exname{Tree}
    \extype{schoice}
    \exsolution{001}