I am very new to LaTeX and Sweave but am excited that it may make my life much simpler when combined with R.

What I am looking to do: Create multiple crosstabs, most likely in a loop. As a result, the number of tables will not be determined ahead of time and I am hoping to have a page break in between each. Lastly, for the time being, I am looking to make this a report instead of a presentation (Beamer).

My question: I assume this is possible, but is it easier to do in Sweave or LaTeX and how should I get started. I have had some bumps in the road getting up and running, but I have been able to compile some very basic examples of both.

This would be my first document in LaTeX, so I am open to any and all suggestions.

  • Bad start, because this is a big problem to make such thing in Sweave.
    – user7
    Commented Jul 27, 2010 at 12:41
  • 1
    That doesn't sound too difficult to me. Go ahead and do it! Commented Jul 27, 2010 at 16:12
  • "Create multiple crosstabs, most likely in a loop."? This requires creating Rnw with R and then Sweaving (this is the way I have seen it done in case of making exam question sheets), what is not elegant at all.
    – user7
    Commented Jul 27, 2010 at 16:45
  • Or rather brew, as sheed03 suggested.
    – user7
    Commented Jul 27, 2010 at 16:50
  • 1
    An example would be nice. =) This can be a tricky one, so give us some guide. What kind of crosstab you'd like to make with Sweave? You should definitely avoid loops, IMHO...
    – aL3xa
    Commented Jul 27, 2010 at 19:53

4 Answers 4


Have a look at the xtable package, which prints LaTeX tables neatly. I have used this a lot for Sweave auto-generated reports.

The following is a toy example of printing some tables for LaTeX in a Sweave document

## generate an example set of tables                                                                                                                         
my.tables <- list()
for(iTable in 1:20){
 my.tables[[iTable]] <- tli[1:20 + iTable,]

## print these out, with page breaks in between                                                                                                              
for(iTable in 1:20){

This might give you an impression how it works (or better: How I would do it :-)

Below is your Sweave file "tstSweave.Rnw" (use this file http://gist.github.com/492318 if you want give it a try).



lalalala \newline\newline

<<echo = FALSE, results = tex>>=


df <- data.frame(x = rbinom(100, 1, 0.2), 
                 y = rbinom(100, 1, 0.6), 
                 g = sample(1:10,100, replace = TRUE))

for(i in unique(df$g)){
  sub <- subset(df, g == i)
  mytab <- with(sub, table(x, y))
  cat(paste("Group ",i, ""))
  cat(" \\clearpage \\pagebreak ")



In R you can use the Sweave() function:

## set your working directory
Sweave(file = "tstSweave.Rnw")

which should give you a new file: tstSweave.txt

pdflatex tstSweave.tex

which should give you a PDF file: tstSweave.pdf

That's it.


I just wanted to chime in to highly recommend RStudio. It's distributed as either a standalone application or as a web service. The latter requires a bit setup with a webserver (e.g., apache). It has fantastic support for both knitr and sweave along with some very basic "chunk" navigation. See this snapshot from their site.

enter image description here

  • +1 While it does not actually answer the question, it is definitely a good recommendation for someone who is just starting with R ans Sweave. Commented May 3, 2013 at 5:00

Have you taken a look at this post from the Learning R Blog? The author has created an automated report with the Brew package, but one of the comments on the post does the same using Sweave and LaTeX. It sounds like you are trying to create a similar report. I was able to adapt the example for one of my automated reports.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .