Different copies of question with table for Moodle with R-Exams

后端 未结 1 551
没有蜡笔的小新
没有蜡笔的小新 2021-01-23 12:45

I would like to generate several copies of a question with randomly generated data in order to upload to Moodle and make a quiz. This question would include a table which depend

1条回答
  •  野的像风
    2021-01-23 13:17

    Overview

    Table formatting is not so straightforward for Moodle, both when starting from an exercise in R/LaTeX format (Rnw, as you do) or in R/Markdown format (Rmd). Below I'm showing a couple of variations of what you can do although I'm not 100% happy with all of them. In all cases the example is static but could be made dynamic in the "usual" way by inserting the random numbers into the respective tables. If you have problems with making one of the solutions dynamic, please let me know.

    Plain

    When you are starting in Rnw you typically generate a {tabular} object either by hand or via packages like xtable or knitr::kable etc. These are converted to valid HTML and imported into Moodle but the formatting with lines (horizontal and/or vertical) is not preserved. The same is true when starting in Rmd and using plain Markdown markup to code the table (again by hand or via knitr::kable etc.).

    Example:

    Rnw:

    \begin{question}
    Consider the following table:
    
    \begin{tabular}{lrr}
    \hline
    Name & Min & Max \\
    \hline
    Foo  & 0   & 1   \\
    Bar  & 0   & 100 \\
    \hline
    \end{tabular}
    
    What is the overall maximum?
    \end{question}
    
    \exname{Table}
    \extype{num}
    \exsolution{100}
    \extol{0.01}
    

    Rmd: Would be similar to above but the table in plain Markdown as:

    | Name | Min | Max |
    |:-----|----:|----:|
    | Foo  |   0 |   1 |
    | Bar  |   0 | 100 |
    

    Some other learning management systems (like OpenOLAT for example) offer suitable table classes in their CSS so that we can tweak the

    in the resulting HTML to
    (where the "mytable" class would need to be provided in the CSS). I looked around a bit in Moodle's question editor but there doesn't seem to be support for such dedicated CSS table styles. If anyone knows more about this I would appreciate some pointers.

    HTML

    The best alternative to this is probably to start in Rmd but instead of writing the table in Markdown you can use full HTML directly. This gives you extensive possibilities for styling the cells by hand. There are also various packages that help you with this. Below I'm using a combination of knitr::kable and kableExtra::kable_styling. The latter offers many more options than those that I use below.

    Example:

    Rmd:

    Question
    ========
    Consider the following table:
    
    ```{r, echo = FALSE, results = "asis"}
    d <- data.frame(
      Name = c("Foo", "Bar"),
      Min = c(0, 1),
      Max = c(0, 100)
    )
    kableExtra::kable_styling(
      knitr::kable(d, format = "html", booktabs = TRUE),
      bootstrap_options = "bordered", full_width = FALSE, position = "left")
    ```
    
    What is the overall maximum?
    
    Meta-information
    ================
    exname: Table
    extype: num
    exsolution: 100
    extol: 0.01
    

    Rnw: I guess the same trick should be possible in Rnw exercises, i.e., include HTML in the LaTeX exercise and preserve that when converting to HTML with pandoc. However, I didn't manage to find the right flag for that. So this currently works just from Rmd exercises.

    LaTeX

    You can also typeset the table with LaTeX and use pdfLaTeX for rendering and then convert the output to PNG or SVG. This is supported by the tex2image() function in the exams package. This can be used in both Rnw and Rmd exercises and the resulting image has to be included in the exercise. The disadvantage is that the fonts etc. differ between the table and the main question (and you have to play with the fontsize and resolution in tex2image()). Moreover, this is relatively slow because pdfLaTeX has to be run on each exercise with such a table.

    Example:

    Rnw:

    \begin{question}
    Consider the following table:
    
    <>=
    tab <- '\\begin{tabular}{lrr}
    \\hline
    Name & Min & Max \\\\
    \\hline
    Foo  & 0   & 1   \\\\
    Bar  & 0   & 100 \\\\
    \\hline
    \\end{tabular}'
    tex2image(tab, name = "tab", dir = ".", pt = 8, resize = 250)
    @
    \includegraphics{tab.png}
    
    What is the overall maximum?
    \end{question}
    
    \exname{Table}
    \extype{num}
    \exsolution{100}
    \extol{0.01}
    

    Rmd: The same code chunk generating the image could be used in Rmd. Just the \includegraphics would need to be replace by the corresponding ![]() Markdown.

    CSS

    Yet another option to render the table in Moodle is to insert a custom stylesheet with a class for the

    to be rendered. A worked example is provided by Kenji Sato in his blog at: https://www.kenjisato.jp/en/post/2020/07/moodle-bordered-table/. We plan to integrate this with a couple of typical classes in exams2moodle() so that the CSS does not have to be inserted in every exercise manually. However, we did not yet get round to impelement this.

    0 讨论(0)
    提交回复
    热议问题