3

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")
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
n. small
  • 45
  • 6
  • I'm on an Ubuntu box and am unable to reproduce. I get a nice pdf document. Linux bosxes are more likely to be equiped for Latex opeations from the getgo than are Mac or Windoze boxes. Kudos for trying to create a running example, but the install process for `exams` package fails to also install the `tth` package. Apparently the package authors missed that requirement in their dependencies. – IRTFM May 13 '20 at 18:25
  • 1
    @AchimZeileis: Since the package maintainer is a sometimes contributor to SO, If the "SO-pin" doesn't attract his attention then you can use the maintainer info at the package DESCRIPTION: Achim.Zeileis at R-project.org – IRTFM May 13 '20 at 18:29
  • Thank you @IRTFM for correcting my question and the minimal example. I should have be more precise about the package dependencies, right. As far as I understand this is not the source of the error message, I had the the tth package installed. – n. small May 13 '20 at 18:35
  • Thank you again @IRTFM, I thought of contacting Achim Zeileis but I wanted to post my question here first. – n. small May 13 '20 at 18:40
  • It turned out that the 'tth'-pkg was only needed for the first function call: `exams2html` and that Achim was aware of this and therefore only had 'tth' in Suggests rather than Imports, so really my error and not his. – IRTFM May 14 '20 at 15:42

1 Answers1

3

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}
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • Dear @Achim Zeileis! Thank you for your immediate reply and for formatting my question. I forget to mention that I use MacTeX on Mac OS. I use R and Latex extensively for my research and teaching for years and I already looked at the link you provided me, thank you. I am not sure that it addresses my problem but I will look at all your other suggestions. – n. small May 13 '20 at 20:47
  • @IRTFM Thanks for your support with this. Regarding the problems with the `tth` installation: I would need more details to say something about this. Please feel free to report this by e-mail or post the details elsewhere. – Achim Zeileis May 13 '20 at 21:24
  • @n.small In that case I would recommend to _not_ install the TinyTeX distribution. Instead as a short-term workaround you can use your own LaTeX template (like the `myplain.tex` that I posted). Additionally I would recommend to resolve the problem that `Sweave.sty` cannot be found. Possibly the suggestion from user chan1142 in the discussion linked above might work for you... – Achim Zeileis May 13 '20 at 21:28
  • Thank you @Achim Zeileis. The tex-Template you provided solved my problem after putting it into my R working directory. Still I understand that this is a work around and that I have not grasped the complexity of my problem yet, but for now I am able to produce a series of randomized versions of one of your templates. – n. small May 13 '20 at 21:34
  • Concerning the problem that Sweave.sty cannot be found, this message never showed up before. I write all me lecture notes using knitr. Still I will at your recommendation in order to better understand the whole setup! I already have installed TinyTeX though... – n. small May 13 '20 at 21:41
  • I'll send you the console transcript but my attempts to remove `tth`-pkg and reproduce the error are failing. Furthermore, I'm using an out-of-date R, I was doing this in a session with almost 80 loaded packages, and it was in Rstudio so feel free to file this in the appropriate bin. – IRTFM May 14 '20 at 00:20
  • `knitr` does not use `Sweave.sty` by default (i.e., not unless you tell it it to). I would recommend that you search for `Sweave.sty` in the `share/texmf/` directory that is provided by your R installation. And then you should add that directory to your `texhash`. I cannot provide more details because I have never used OS X/MacTeX. – Achim Zeileis May 14 '20 at 00:23
  • @AchimZeileis .. Maybe I should see what trouble I can get into on my Mac? It's also not up-to-date but I doubt this configuration issue is arising just because the OP is using Catalina. – IRTFM May 14 '20 at 01:21
  • I think it is just an environment variable that is not set properly which results in R's `share/texmf/` not being included in the texmf paths. Why this can happen and how to best resolve it on OS X with MacTeX I don't know, unfortunately. – Achim Zeileis May 14 '20 at 02:35
  • I am aware of that ```knitr```does not use ```Sweave.sty```. That is why I was puzzled about the message given my Weave options/settings in R-Studio. Following your suggestion @AchimZeileis I used the instructions from http://www.jason-french.com/blog/2013/08/16/easy-sweaving-for-latex-and-r/ and I am now able to run the minimal example from my initial question. Perfect! @AchimZeileis and @IRTFM thank your immediate support and for providing this highly useful R package! – n. small May 14 '20 at 09:03
  • 1
    OK, good! Regarding the Weave option settings in RStudio. These influence weaving/knitting buttons in RStudio but not the behavior of other packages. In R/exams the default is to use `Sweave()` for .Rnw files to keep dependencies simple and because the extended facilities of `knitr` are typically not needed for most exams. However, you can set the `engine = "knitr"` if you want in `exams2pdf()`. Nevertheless, this will still invoke the `knitr::render_sweave()` compatibility option. – Achim Zeileis May 14 '20 at 09:34