The previous post “Writing a Book Using R” described using latex for writing a book, saving time with one master bibliography and other organizational devices. Sweave allows R code to be included in a latex file. This is a good marriage; while latex provides typeset text; R is statistically and graphic oriented.
Here is an example of the overall steps one would take to compile a combined latex and R code document for publication.
Part of the latex is generated by the Sweave package in R, not R itself. For example, you need to first run Sweave(â€script.Râ€) in the R console. This passes the latex you write untouched, and processes the R commands, particularly plot() that produces plots. There is also a package called xtable that produces latex from data.frames, and model result and time series objects. To you it you run your analysis, store the results in the appropriate object like a model, data.frame or time.series then call xtable(x) and all the results come out in formatted latex.
With the latex, you then compile it with any of the compilers, e.g. a typical sequence that would automatically do all the table of contents, bibliography, index, figure and table references and list of steps would be:
- Sweave(“master.R”) — done in R
- pdflatex master — on the command line
- bibtex master — does the bibliography
- makeindex master — makes the index
- pdflatex master — put bibliography and index in
- pdflatex master — does the cross referencing
This should produce the finished pdf ready to print with the results of analysis, figures, and everything collated.