Search code examples
latexr-markdownr-exams

How to use tables as options of a schoice exercise in r exams?


I want to create an schoice exercise where each of the five options is a table with a CDF, like this one:

enter image description here

I do not want to provide tables as image. I came across a possibility:

creating a list with 5 tables:

sc$questions <- c(rbind(c("F(x)","1")) |> kableExtra::kbl(format = 'latex', col.names = c("","1")),
                  rbind(c("F(x)","2")) |> kableExtra::kbl(format = 'latex', col.names = c("","1")),
                  rbind(c("F(x)","3")) |> kableExtra::kbl(format = 'latex', col.names = c("","1")),
                  rbind(c("F(x)","4")) |> kableExtra::kbl(format = 'latex', col.names = c("","1")),
                  rbind(c("F(x)","5")) |> kableExtra::kbl(format = 'latex', col.names = c("","1")))

And then print them using:

```{r questionlist, echo = FALSE, results = "asis"}
answerlist(sc$questions, markup = "markdown")

However, this solution only works if I set kbl()format to latex, I would like to use pandoc to set it adequately with math mode (e.g., colnames with "\leq").


Solution

  • I came accross this solution:

    
    sc <- list()
    sc$questions <- c(rbind(c("$F(x)$","$0$","$\\frac{1}{6}$","$\\frac{1}{2}$","$1$")) |> kable("latex", booktabs=TRUE, align = "c", escape = F, col.names = c("","$x<1$","$1\\leq x<2$","$2\\leq x<3$","$x\\geq 3$")),
                        rbind(c("$F(x)$","$0$","$\\frac{1}{6}$","$\\frac{1}{2}$","$1$")) |> kable("latex", booktabs=TRUE, align = "c", escape = F, col.names = c("","$x\\leq 1$","$1<x\\leq 2$","$2< x\\leq 3$","$x> 3$")),
                        rbind(c("$f(x)$","$\\frac{1}{6}$","$\\frac{1}{2}$","$1$")) |> kable("latex", booktabs=TRUE, align = "c", escape = F, col.names = c("","$x=1$","$x=2$","$x=3$")),
                        rbind(c("$F(x)$","$0$","$\\frac{1}{6}$","$\\frac{1}{2}$","$1$")) |> kable("latex", booktabs=TRUE, align = "c", escape = F, col.names = c("","$x=0$","$x=1$","$x=2$","$x=3$")),
                        rbind(c("$f(x)$","$\\frac{1}{6}$","$\\frac{2}{6}$","$\\frac{3}{6}$")) |> kable("latex", booktabs=TRUE, align = "c", escape = F, col.names = c("","$x=1$","$x=2$","$x=3$")))
    

    And:

    ```{r questionlist, echo = FALSE, results = "asis"}
    answerlist(sc$questions, markup = "markdown")
    

    Which produced:

    enter image description here