2

I'm creating a small package for generating exams with randomly ordered questions (and responses on multiple choice items) and the R part is going fine, but now I need a little more control over the resulting LaTeX that is created when rendering a .pdf.

Here is my issue: questions and their corresponding response items can be split across multiple pages, which is not ideal. I've come up with a suitable LaTeX solution by encapsulating the question items within a minipage. The original .tex file looks something like this:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Which bear is best?

  A. Teddy bear\\
  B. Black bear\\
  C. Grizzly bear\\
  D. Honey bear\\
  E. Polar bear

\item
  Which beer is best?

  A. Miller Lite\\
  B. Naturdays\\
  C. Leine's\\
  D. Hamm's\\
  E. PBR
\end{minipage}

And my minipage solution is to do something like this:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\begin{minipage}{\linewidth}
\item
  Which bear is best?

  A. Teddy bear\\
  B. Black bear\\
  C. Grizzly bear\\
  D. Honey bear\\
  E. Polar bear
\end{minipage}
\begin{minipage}{\linewidth}
\item
  Which beer is best?

  A. Miller Lite\\
  B. Naturdays\\
  C. Leine's\\
  D. Hamm's\\
  E. PBR
\end{minipage}

The problem is that I don't know how to do this with RMarkdown and R in a one step process. Ideally, a user would have an .Rmd file in which they simply do something like...

---
title: Some test
output: pdf_document
---

```{r}
library(examgen)

output_questions("/path/to/questions.json")
```

and the LaTeX will be formatted appropriately, but I think this is not simply an issue of including a custom .tex file (I could be wrong here). I could easily create a shell script that inserts the minipagess at the appropriate place and then again renders a .pdf with pdflatex or whatever was being used, but obviously this is not the most user friendly. I'd also like this package to be able to output .html as well.

I can provide more details about the R code if that's relevent.

EDIT

I've created a branch of my git repo for this question so you can see exactly what I'm dealing with:

# clone the repo for access to example .Rmd and data
git clone -b se-question https://gitlab.com/mhaffner/examgen.git

# install the package
Rscript -e "devtools::install('examgen/R')"

# render rmarkdown
Rscript -e "rmarkdown::render('examgen/examples/exam-template.Rmd')"

Then you can look at the files examples/exam-template.tex and examples/exam-template.pdf.

EDIT 2

And here's a simpler example that skirts by the package but gets to the root of the issue:

---
title: "Exam Title"
author: "Your name here"
date: "November 21, 2019"
output:
  pdf_document:
    keep_tex: true
fontsize: 12pt
geometry: margin=1in
---

```{r results = "asis", echo = FALSE}
question <- c("1. Which beer is best?\n
\tA. Miller Lite\n
\tB. Hamm's\n")

cat(question)
cat("\n")
cat(question)
cat("\n")
```

Put this is in a .Rmd, render it, and take a look at the resulting .tex and .pdf.

haff
  • 918
  • 2
  • 9
  • 20
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Nov 21 '19 at 16:44
  • 2
    Interesting problem! My solution would be to make `format_mc` generate the raw LaTeX code instead of letting knitr/pandoc convert it for you, and then add some conditionals to take care of the .html-output based on e.g. `knitr::is_html_output()`. But I'm sure it can be solved in a much more elegant way. – henrik_ibsen Nov 22 '19 at 09:15
  • 1
    @henrik_ibsen Yep, generating LaTeX directly is what I've been working on. This way I may be able to bypass RMarkdown entirely, but this may be less user friendly. However, you `knitr::is_html_output()` idea may be a good comprimise. I didn't know about these higher level functions, so this helps. – haff Nov 22 '19 at 18:52

0 Answers0