How can I print one question per page in exams2pdf? - r-exams

I am writing an exam using exams2pdf. Is there a way to get each question on a new page?
I did not find an option in the manual so far.

This is explained in this thread in the R-Forge forum: https://R-Forge.R-project.org/forum/message.php?msg_id=48478&group_id=1337
Just redefine the {question} environment in the LaTeX template file:
\renewenvironment{question}{\item}{\newpage}
This adds a \newpage at the end of the question so that the next one starts on a new page. See vignette("exams", package = "exams") for more details about how to set up a LaTeX template file.

Related

Link to R6 method from separate package in help pages and pkgdown

Cross posted from: https://community.rstudio.com/t/link-to-r6-method-from-separate-package-in-help-pages-and-pkgdown/134702
I'm currently writing an R package and would like to link to the help page for an R6 method in a separate package. The page I want to link to is here: https://mc-stan.org/cmdstanr/reference/model-method-sample.html, and there is an .Rd file for the method as well (https://github.com/stan-dev/cmdstanr/blob/master/man/model-method-sample.Rd). Finally, I can also access the help page from R directly with ?cmdstanr::`model-method-sample`.
However, when I try to add a link to my own help page using the normal link to another package syntax described here, [cmdstanr::`model-method-sample`], I get this error:
Warning: Link to unknown topic: cmdstanr::`model-method-sample`
I feel like there must be some way to link to this help page, given that it definitely exists and has an .Rd page, but I haven't found a solution yet. Has anyone else run into this problem or know the solution?
I think you (or Roxygen) are using the wrong syntax for the link. According to Writing R Extensions, the Rd syntax should be:
\link[cmdstanr]{model-method-sample}
I'm not sure how to generate this from Roxygen, but it appears to work as-is if I put it in Roxygen comments.
If you want the link with different text, the syntax is
\link[cmdstanr:model-method-sample]{link text}

R/exams: how to select different font type for pdfs

HI am pretty new to coding with R/LaTeX and currently trying to figure out how to set up exams using R/exams. While I have already managed to create the exercises themselves, I now want to save them as pdfs using exams2pdf.
However, I would like to use a different font than the one used by default. Does anyone know whether this is possible and, if so, how I have to add this to my code?
Is it also possible to change the "Header", where it says "1. Problem" to something else/remove it?
Thanks a lot for your help.
Overview
In exams2pdf() there is the template = argument with which you can specify a master LaTeX template into which the individual exercises are embedded. By providing your own template you can change any aspect you like: fonts, page formatting, intro text, whether or not questions/solutions are shown, etc.
Below I give a rather detailed overview so that this post is also useful for other R/exams users that want to modify other aspects of the template in exams2pdf().
Getting started
To get started I would recommend to go to a suitable working directory and then set up exams_skeleton() there, e.g.,
exams_skeleton(dir = ".", writer = "exams2pdf", markup = "markdown")
which copies all of the .Rmd exercises (due to the markup = "markdown" specification) available in R/exams to an exercises/ folder, along with all available LaTeX templates to the templates/ folder, and sets up two demo .R scripts: demo-all.R and demo-pdf.R. You can look at these, especially the latter, and play around with the examples provided there.
Vignette
More details and background information are then provided in a package vignette: vignette("exams", package = "exams"), especially Section 3.
The master LaTeX template
For doing the concrete adaptations you ask for, I would start by modifying the plain.tex template in the templates/ directory. The "factory-fresh" default content is:
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{a4wide,color,Sweave,url,amsmath,booktabs,longtable,eurosym,amssymb}
\newenvironment{question}{\item \textbf{Problem}\newline}{}
\newenvironment{solution}{\textbf{Solution}\newline}{}
\newenvironment{answerlist}{\renewcommand{\labelenumii}{(\alph{enumii})}\begin{enumerate}}{\end{enumerate}}
\providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setkeys{Gin}{keepaspectratio}
\begin{document}
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
\end{document}
Changing question and solution formatting
For example, you could decide to only show the question but not the solution by modifying the {question} and {solution} environments above to:
\usepackage{verbatim}
\newenvironment{question}{\item}{}
\newenvironment{solution}{\comment}{\endcomment}
Changing the answer list formatting
Then you could modify the answerlist from (a), (b), ... to A., B., by modifying the {answerlist} environment:
\newenvironment{answerlist}{\renewcommand{\labelenumii}{\Alph{enumii}.}\begin{enumerate}}{\end{enumerate}}
Changing the font
And finally you could modify the default font to Lato, say, by loading the {lato} package and using a dark gray (rather than the default black) font color. To do so insert before the \begin{document} line:
\usepackage[default]{lato}
\definecolor{darkgray}{rgb}{0.23,0.23,0.23}
\color{darkgray}
Run exams2pdf() with new template
And then to see the result, run:
exams2pdf("swisscapital.Rmd", edir = "exercises/", template = "templates/plain.tex")

How do I put a custom url in a r-exams question that it will be exported to moodle?

The r-exams package offers a possibility to add a url to the question using the macro \url{some link}, however I would like to hide the link and show some text instead, for example: \command{www.r-exams.org}{R/EXAMS}. It's possible?
Answer
In R/LaTeX (Rnw) exercises you can do so with the \href{...}{...} command. In R/Markdown (Rmd) exercises you can use the [...](...) syntax. See below for rexams.Rnw and rexams.Rmd respectively.
Details
To generate the Moodle XML file for import into Moodle, use
exams2moodle("rexams.Rnw")
And to preview more quickly what the HTML code looks like use
exams2html("rexams.Rnw")
In the latter case you can also add the argument converter = "pandoc-mathjax" so that you use the same HTML converter that exams2moodle() uses by default. The same also works for the rexams.Rmd file. Both of the HTML converters (ttm and pandoc) support these syntaxes for the specification of hyperlinks.
Only when converting to PDF the default template will not work but you need to do something like
exams2pdf("rexams.Rnw", template = "mytemplate.tex")
where "mytemplate.tex" needs to be a suitable LaTeX template that includes the {hyperref} package. See Section 3 in vignette("exams", package = "exams") for further details. The reason the {hyperref} is not included in the default template is that (a) {hyperref} is quite "invasive" and changes many aspects of the document formatting and (b) the default PDF configuration is intended for printing and not hyperlinking because HTML-based formats are better for the latter.
Exercise rexams.Rnw
\begin{question}
How many times do you see the logo on the
\href{http://www.R-exams.org/}{R/exams web page}?
\end{question}
\exname{R/exams logo}
\extype{num}
\exsolution{2}
Exercise rexams.Rmd
Question
========
How many times do you see the logo on the
[R/exams web page](http://www.R-exams.org/)?
Meta-information
================
exname: R/exams logo
extype: num
exsolution: 2

Hide and show comments

I'm writing all my scripts on .R file using R for mac. It is convenient to me because there are colors to highlight the type of commands.
I have a many comments following the # symbol that are useful when I forget about the meaning of my script but they tend to blur my script so that it gets harder to find a given command line.
Is there a way to hide and show these comments ? (Using the programm I'm currently using or another one). What would you suggest as the best program to write R script ?
Thanks a lot !
RStudio supports code folding. You can standardize your comment blocks so that they are recognized as code blocks.
For example, enter this into your RStudio editor
#=======================================================
# this is a comment block
# more comments here
# comments upon comments
and then press Alt+L to fold, and Alt+Shift+L to unfold.
Try RStudio for mac. One of the greatest code writing environment for R there is.
You can also try Emacs, which is more like old-fashioned command line editor. You can find a good guide here.

How can I have the NEWS displayed in the html help page of an R package?

The help page of the news() R function says:
it is attempted to read its news in structured form from files
‘inst/NEWS.Rd’, ‘NEWS’ or ‘inst/NEWS’ (in that order).
Doing so and installing the package, we get (under windows) a link to the NEWS file at the top of the html page open by the command help(package=packagename). For an example, assuming you have installed party, you can try
help(package="party")
This works however only with files named NEWS. When we provide instead a NEWS.Rd file, there is no link to the news. Try
help(package="survival")
Is there a way to get this link when we provide a NEWS.Rd file?
Thanks for your help.
The problem has been solved since R 3.0.0.
It seems, however, that a series of the Rd formatting code such as \code{} for example are ignored when rendering the NEWS.Rd in html.

Resources