I am currently working with the R/exams package, specifically creating exams using the exams2nops
function and the order of the questions is randomized. Everything works fine except for one detail: The samepage = TRUE
option only prevents page breaks within a paragraph. For my purpose it is however necessary not to split any of the exercises. Within one text it would still be possible to write only one paragraph (although this is not helpful for clarity). Unfortunately, whenever I need to include a table, I am forced to start a new paragraph which is not "protected" against page break. Due to the randomization there are always some copies where text and tables are splitted to multiple pages. I tried .Rmd as well as .Rnw files and also tried to integrate some LaTeX functions such as \nopagebreak
and \needspace
without success. So far, I am not too experienced with LaTeX and also with Google's help I did not find a solution.
Here is a minimal example of what kind of exercises I am talking about:
.Rmd
Question
========
Some kind of question:
| A | B | C |
|:-:|:-:|:-:|
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
| 1 | 2 | 3 |
Answerlist
----------
* First option
* Second option
* Third option
Some further informational text.
.Rnw
\begin{question}
Some kind of question:
\begin{center}
\begin{tabular}{ccccccr}
$\text{A}$ & $\text{B}$ & $\text{C}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
$\text{1}$ & $\text{2}$ & $\text{3}$ \\
\end{tabular}
\end{center}
Some further informational text.
\begin{answerlist}
\item First option
\item Second option
\item Third option
\end{answerlist}
\end{question}
I am not sure what to try.
The samepage = TRUE
option only enforces that the {answerlist}
is in a {samepage}
environment - but not the entire {question}
.
The easiest option to accomplish what you want to do seems to be to re-define the {question}
environment in the header =
argument. You could either try to put everything into a {samepage}
environment via
exams2nops(...,
header = "\\renewenvironment{question}{\\item \\begin{samepage}}{\\end{samepage}}")
This may have to be coupled, though, with some \nopagebreak
commands in between paragraphs. (See: Make an unbreakable block in TeX)
A simpler solution might be to put every exercise on its own page by including a page break at the end of each exercise:
exams2nops(...,
header = "\\renewenvironment{question}{\\item}{\\newpage}")
In case you are not familiar with the LaTeX syntax above:
\renewenvironment{foo}{...}{...}
re-defines the environment "foo"....
is what is executed at the beginning of the environment....
is executed at the end.\item
is executed at the beginning to increase the enumerated counter for the exercises.\\item
) are required in R to escape the backslashes as they are a special character.