Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

workflow between Latex and R screwed

Tags:

r

latex

sweave

Trying to write my second Latex file

\documentclass{article}
\title{simple Sweave script}
\usepackage{/Users/Brenden/Documents/R/R-2.15.1/share/texmf/tex/latex/Sweave}
\begin{document}
\maketitle
 This is a simple script to demonstrate Sweave. Let's
 begin by generating some results
<<>>=
x=rnorm(30)
y=rnorm(30)
mean(x)
cor(x,y)
@
and follow that up with some random text
\end{document}

File saved with .Rnw as extension. I could then use Sweave under R to convert the file to tex. I then run pdflatex under cmd to get my .pdf file. R is stored under /Users/Brenden/Documents. MiKTex is stored under /Users/Brenden/Desktop.

It is said that usually the line of "usepackage" is not needed, as when I run Sweave under R, a line of "usepackage{Sweave}" will be added to the tex file which is stored under /Users/Brenden/Documents. However, if I don't put in userpackage line, when I run pdflatex under cmd (either under /Documents or /Desktop), I got a messsage that says "Sweave.sty not found". So I have been through this trouble by always adding a line of usepackage with a detailed path to help circumvent the problem. Although the "Sweave.sty not found" problem is circumvented, I do notice that when I run pdflatex under cmd, I got the response

LaTeX Warning: You have requested package '/Users/Brenden/Documents/R/R- 
2.15.1/share/texmf/tex/latex/Sweave', but the package provides "Sweave'.

Then it runs through several files with .sty under MiKTex

(C:\Users\Brenden\Desktop\MiKTex\tex\latex\base\ifthen.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\graphicx.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\keyval.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\graphics.sty)
(C:\Users\Brenden\Desktop\MiKTex\tex\latex\graphics\trig.sty)
...

to eventually create a .pdf

From another post on Stackoverflow, it is said, "That path is set automatically when you install LaTeX; it's your personal texmf tree and you can put any style or class files you want LaTeX to find there. The directory names don't matter, it searches recursively in them for Sweave.sty". To me, however, clearly my MiKTex could not find the Sweave.sty unless I specify the path. And even with the path specification, LaTex still gives me a warning. Could someone explain to me where I screwed up (during installing MiKTex, perhaps? ) so that I could help MiKtex find its way to Sweave without my specifying the path?

Thank you.

like image 840
B Chen Avatar asked Oct 22 '12 18:10

B Chen


3 Answers

It is annoying to be always distracted by little devils like this (new concept texmf tree, endless environment variables TEXINPUTS, SWEAVE_STYLEPATH_DEFAULT, ...). You will never worry about LaTeX packages if you use knitr, and if you like the Sweave style, you can simply put render_sweave() in the document like:

\documentclass{article}
\title{simple Sweave script}
<<setup, include=FALSE>>=
render_sweave()
@

\begin{document}
\maketitle
 This is a simple script to demonstrate Sweave. Let's
 begin by generating some results
<<>>=
x=rnorm(30)
y=rnorm(30)
mean(x)
cor(x,y)
@
and follow that up with some random text
\end{document}

Save it as, say, test.Rnw and compile it with

library(knitr); knit('test.Rnw') # or knit2pdf('test.Rnw')

And I guess you are probably better off without render_sweave() (i.e. use the default LaTeX style in knitr).

like image 22
Yihui Xie Avatar answered Oct 21 '22 03:10

Yihui Xie


In the MiKTeX Settings program, there is a tab called "Roots". It is there that you can specify where additional .sty files can be found. In your case, I believe that you will want to add C:\Users\Brenden\Documents\R\R-2.15.1\share\texmf as an additional root. Doing this, any of the TeX programs will be able to find Sweave.sty whether run from inside R or from the command line.

like image 179
Brian Diggs Avatar answered Oct 21 '22 05:10

Brian Diggs


I suspect the path referred to in the other post is the path to your personal texmf tree, which is not where Sweave puts it. One option is to move it to your personal texmf tree, but the usual method nowadays, I believe, is to run pdflatex from within R, like this:

texi2dvi("foo.tex", pdf = TRUE)

I think you can also have Sweave add the full path automatically by adding

export SWEAVE_STYLEPATH_DEFAULT="TRUE"

to your .bashrc file, or whatever the equivalent would be on your system.

like image 21
Aaron left Stack Overflow Avatar answered Oct 21 '22 03:10

Aaron left Stack Overflow