Friday, October 22, 2010

LaTeX, Sweave, R

\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{url}
\usepackage{cite}


$ R CMD Sweave foo.Rnw ; texi2pdf foo.tex

http://www.stat.umn.edu/~charlie/Sweave/
http://www.stat.umn.edu/~charlie/Sweave/foo.pdf
http://www.stat.umn.edu/~charlie/Sweave/foo.Rnw

\pagebreak[3]
\verb@Sweave@

\begin{verbatim}
latex foo
\end{verbatim}


Figure~\ref{fig:one} (p.~\pageref{fig:one})


<>=
plot(x, y)
abline(out1)
@


\begin{figure}
\begin{center}
<>=
<>
@
\end{center}
\caption{Scatter Plot with Regression Line}
\label{fig:one}
\end{figure}



<>=
n <- 50
x <- seq(1, n)
a.true <- 3
b.true <- 1.5
y.true <- a.true + b.true * x
s.true <- 17.3
y <- y.true + s.true * rnorm(n)
out1 <- lm(y ~ x)
summary(out1)
@





The commands in package lattice have different behavior than the standard plot commands in
the base package: lattice commands return an object of class "trellis", the actual plotting is
performed by the print method for the class. Encapsulating calls to lattice functions in print()
statements should do the trick, e.g.:
<>=
library(lattice)
print(bwplot(1:10))
@



BibTeX and bibliography styles

http://amath.colorado.edu/documentation/LaTeX/reference/faq/bibstyles.html

The two Latex editors that I found most useful are:
 1. Texmaker - lightweight, spell-check, have to press F1 and F3 to generate a PDF, need to click on the log window a lot
 2. Kile - nicer, but I couldn't get syntax coloring to work for Sweave, spell-check, one-click gets you a PDF


Trick
- if you rename your '.Rnw' to '.tex' as a work around, it plays nicely with the editors
- then once all formatting is done, copy '.tex' to '.Rnw' and run the command
- or just create a tex symlink to Rnw!   ln -s mydoc.Rnw mydoc.tex

R CMD Sweave mydoc.Rnw && texi2pdf mydoc.tex && evince mydoc.pdf

In R, call
> Stangle(file='foo.Rnw')
to extract the R code, WARNING: this will overwrite 'foo.R'!!!!!


No comments: