SCM

Forum: support

Monitor Forum | Start New Thread Start New Thread
RE: Using latex packages in exams2canvas [ Reply ]
By: Achim Zeileis on 2020-04-05 21:45
[forum:47524]

tikz_tree.Rmd (2) downloads
Dieuwke, I'm including some basic pointers here. If you need struggle with any aspects or need more information, please feel free to follow-up!

The .Rnw file format is also known as "Sweave" which mixes R code and LaTeX. An R code begins with <<...>>= where ... is a (possibly empty) list of options that controls what to with the code chunk (evaluate, display, create figure, etc.). The @ ends the R code chunk and switches back to LaTeX.

So the few lines of R code in the .Rnw (a) determine into which format the TikZ code should be rendered, (b) the actual TikZ code, (c) create the TikZ output that should be included in the file.

(a) With match_exams_call() you can find out whether the file is currently processed with exams2pdf() or exams2canvas() or something else. For the two PDF/LaTeX-based formats the .tex code is simply preserved ("tex") but in all other cases it is rendered into "svg" which can be included in HTML. See

?match_exams_call

within R for what the function does.

(b) is just your TikZ code except that backslashes have to be escaped within strings.

(c) The include_tikz() function embeds the TikZ code in the question - ether as is or in SVG format (see a). In the latter case it is embedded in a {standalone} LaTeX document, then texi2pdf is called, and the output transformed with pdf2svg. Finally a suitable \includegraphics{} call is included. For more details see

?include_tikz

As an alternative to using the LaTeX-based .Rnw file, you can also use a Markdown-based .Rmd file. This works essentially in the same way but maybe the markup feels more familiar to you. I have attached the .Rmd version for your dummy exercise. The main difference is that it is not so easy to include the TikZ code "as is" for the PDF output. Instead a standalone PDF graphic is created.

RE: Using latex packages in exams2canvas [ Reply ]
By: Dieuwke Hupkes on 2020-04-05 16:24
[forum:47523]
Dear Achim,

Thank you, this works indeed.

I had seen the other examples before, but I did not manage to extend it to my case and also couldn't find the corresponding documentation. Could you perhaps point me to the documentation explaining how the following lines that you added to my question work:

```
typ <- if(match_exams_call() %in% c("exams2pdf", "exams2nops")) "tex" else "svg"
```

```
<<echo=FALSE, results=tex>>=
include_tikz(tikz_tree, name = "tik_tree", format = typ,
packages = "tikz-qtree", width = "5cm")
```

And how about the "@"-signs?

Thank you!

RE: Using latex packages in exams2canvas [ Reply ]
By: Achim Zeileis on 2020-04-05 13:38
[forum:47522]

tikz_tree.Rnw (5) downloads
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 compiled tikz graphics in LaTeX and then convert them to a graphics format supported by HTML, e.g., SVG vector graphics or alternatively PNG or JPG. This functionality is offered by the include_tikz() function in the "exams" package.

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(). See the following exercises for similar examples:

http://www.R-exams.org/templates/automaton/
http://www.R-exams.org/templates/logic/

Using latex packages in exams2canvas [ Reply ]
By: Dieuwke Hupkes on 2020-04-05 08:41
[forum:47521]

canvas_question_example.Rnw (5) downloads
I am trying to use r-exams to create a canvas exam, but my questions contain latex code that require specific packages (in particular: tikz, tikz-qtree and several other tikz libraries).

Is it possible to tell the exams2canvas function to import these packages, or is there any other way that I can create canvas exams that have tikz figures inside? I attached the example question I am trying to compile.

Many thanks!

Thanks to:
Vienna University of Economics and Business Powered By FusionForge