Search code examples
rr-exams

Use R/exams exams2pdf() to produce a PDF document


I am new to the R/exams package and I try to produce a pdf document from one of the templates provided by the developers. (http://www.R-exams.org/templates/confint3/)

I am able to compile a Rnw file into a HTML document using the commands

library("exams")
exams2html("confint3.Rnw")

When calling the function exams2pdf("confint3.Rnw") it gives the error message

! LaTeX Error: File `Sweave.sty' not found.

I have Latex installed and there are no problems using it in general. I do not understand:

  1. Do I need to tell exams2pdf() the location of the latex installation?
  2. Do I need to define a template (as plain.tex) first? How should it look like?
  3. What is it that I do not understand?

I looked at the documentation of the exams package, I also tried exams2pdf() after installing and calling library("tinytex").

Any help where to look at or what to do is highly appreciated. Thank you!

Minimal example:

install.packages("exams")
install.packages("tth")
library("exams")

set.seed(1090)
exams2html("confint3.Rnw")
set.seed(1090)
exams2pdf("confint3.Rnw")

Solution

  • It is hard to diagnose what exactly goes wrong with the information provided. In any case, when running pdfLaTeX either through utils::texi2dvi() (the default when the R package tinytex is not installed) or through tinytex::latexmk() (the default when the R package tinytex is installed) does not find the Sweave.sty file provided by the R base system. What is not clear to me which LaTeX engine is running in the background: MikTeX on Windows?

    There are several strategies that could resolve this issue:

    • Tell your LaTeX installation about the texmf directory provided by the R base system so that it is found no matter where on your system you call pdfLaTeX.
    • Use a different LaTeX installation, e.g., by installing TinyTeX (the LaTeX distribution) through tinytex (the R package): tinytex::install_tinytex(). This might be particularly attractive if you are not actually a LaTeX user and just need it to compile PDF exams.
    • Avoid using the Sweave.sty file in a custom template file, say myplain.tex. A suggestion for such a file is included at the end of this post.

    Further details are discussed in this thread: https://tex.stackexchange.com/questions/153193/latex-error-sweave-sty-not-found

    As for your three questions:

    1. As already explained above: exams2pdf() leverages either utils::texi2dvi() or tinytex::latexmk(). So these need to know about the LaTeX installation - but this seems to be the case. They just don't find the texmf provided by base R.
    2. You should not have to do this but it is certainly an option that you can use. As a starting point, run exams_skeleton(markup = "latex", writer = "exams2pdf"). Among other things this creates a templates folder where you could put the myplain.tex template below.
    3. As I said above, it's hard to answer that with the information provided. Hopefully, one of the clues provided here gets you a couple of steps forward.

    Content of myplain.tex:

    \documentclass[a4paper]{article}
    
    \usepackage[utf8]{inputenc}
    \usepackage{a4wide,graphicx,color,verbatim,url,fancyvrb,ae,amsmath,amssymb,booktabs,longtable,eurosym}
    \newenvironment{question}{\item \textbf{Problem}\newline}{}
    \newenvironment{solution}{\textbf{Solution}\newline}{}
    \newenvironment{answerlist}{\renewcommand{\labelenumi}{(\alph{enumi})}\begin{enumerate}}{\end{enumerate}}
    \providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
    
    \setkeys{Gin}{keepaspectratio}
    
    \DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
    \DefineVerbatimEnvironment{Soutput}{Verbatim}{}
    \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
    \newenvironment{Schunk}{}{}
    
    \begin{document}
    \begin{enumerate}
    %% \exinput{exercises}
    \end{enumerate}
    \end{document}