Dear R-help, I am using Sweave and pdflatex to generate a large report from data contained in my database (Postgres via RODBC). Currently, I work with a single R/Sweave file, containing several "chapter" indications for the Latex engine. My master tex file sets the document class, and includes the introduction, the main Sweave file, and a conclusions and reference file. I use a makefile to produce the final PDF (based on the thread "Sweave, R and complex latex projects: http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4891.html) What I would like to do, is to be able to get 2 types of output with the same code (I'm lazy ;-) ): 1. my large report in a single PDF file, for printing out and distributing 2. a PDF and HTML file *per chapter*, for displaying on our website and allowing people to download individual chapters I have tried the following things: - see if pdflatex has an option to split PDF output per chapter; as far as I see, it doesn't - separate the Sweave file into chapter parts. The problems here are 1) that I do a certain number of R preparations (variables setting, table querying) which are data that I will need in later parts of the code, 2) that I would need to embed the generated tex files with per-chapter master tex files setting the documentclass and other options and including the chapter; I also tried to see if it was possible to tell pdflatex to assume documentclass X even if it wasn't specified in the file, but that doesn't seem to work either - generate my large PDF report as usual and manually cut it into chapters (tedious) - use R via PHP to output per-chapter HTMLs which I could turn into PDFs using output buffering; this works for the graphics, but I'm unable to get back e.g. tabular data for proper display; also I would loose the latex-y beauty of my PDF As I'm a novice in Latex and Makefiles usage, I'd be glad if you could tell me if what I want to do is feasible (I'm sure it is), and which would be the best, fussless method to do it (i.e. generate both types of output without changing the R/Sweave code). I know you'll probably tell me to break my long Sweave code into smaller parts, but as I briefly said above, I do some variable setting and table querying at the start - things I will repeatedly need in later chapters (e.g. I query a population table for computing incidence rates several times in later chapters). If there is a better way to split the code without having to requery the database at each chapter, I'll be glad to know about that too! BTW, I'm working on Ubuntu Linux. Thanks a lot for your insight, Anne-Marie
On 21/05/2008 5:40 AM, Anne-Marie Ternes wrote:> Dear R-help, > > I am using Sweave and pdflatex to generate a large report from data > contained in my database (Postgres via RODBC). Currently, I work with > a single R/Sweave file, containing several "chapter" indications for > the Latex engine. My master tex file sets the document class, and > includes the introduction, the main Sweave file, and a conclusions and > reference file. I use a makefile to produce the final PDF (based on > the thread "Sweave, R and complex latex projects: > http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4891.html) > > What I would like to do, is to be able to get 2 types of output with > the same code (I'm lazy ;-) ): > 1. my large report in a single PDF file, for printing out and distributing > 2. a PDF and HTML file *per chapter*, for displaying on our website > and allowing people to download individual chapters > > I have tried the following things: > - see if pdflatex has an option to split PDF output per chapter; as > far as I see, it doesn't > - separate the Sweave file into chapter parts. The problems here are > 1) that I do a certain number of R preparations (variables setting, > table querying) which are data that I will need in later parts of the > code, 2) that I would need to embed the generated tex files with > per-chapter master tex files setting the documentclass and other > options and including the chapter; I also tried to see if it was > possible to tell pdflatex to assume documentclass X even if it wasn't > specified in the file, but that doesn't seem to work either > - generate my large PDF report as usual and manually cut it into > chapters (tedious) > - use R via PHP to output per-chapter HTMLs which I could turn into > PDFs using output buffering; this works for the graphics, but I'm > unable to get back e.g. tabular data for proper display; also I would > loose the latex-y beauty of my PDF > > As I'm a novice in Latex and Makefiles usage, I'd be glad if you could > tell me if what I want to do is feasible (I'm sure it is), and which > would be the best, fussless method to do it (i.e. generate both types > of output without changing the R/Sweave code).I think you will need to make some changes, including splitting your source into one main file and one file per chapter. In your main file, use \include{foo} to include the chapter foo. If you want the full document, do nothing else. If you want to generate *only* that one chapter, then before any of the \include lines, use \includeonly{foo} Sweave will completely ignore these lines, but you can set up your makefile to only run Sweave when the Sweave source has changed, then run pdflatex once on the main file at the end.> I know you'll probably tell me to break my long Sweave code into > smaller parts, but as I briefly said above, I do some variable setting > and table querying at the start - things I will repeatedly need in > later chapters (e.g. I query a population table for computing > incidence rates several times in later chapters). If there is a better > way to split the code without having to requery the database at each > chapter, I'll be glad to know about that too!If you want to avoid repeating some computations, then save them into a .Rdata file, and load that at the beginning of each chapter. I normally keep code to do this in the main Sweave source, but mark it as eval=FALSE after the first time through. It shouldn't print anything that you want in the final document, because the eval=FALSE marking will stop those print statements from running. Put them into a separate code chunk that you run after loading. Duncan Murdoch
"Anne-Marie Ternes" <amternes at gmail.com> writes:> What I would like to do, is to be able to get 2 types of output with > the same code (I'm lazy ;-) ): > ... > 2. a PDF and HTML file *per chapter*, for displaying on our website > and allowing people to download individual chapters > > ... > > BTW, I'm working on Ubuntu Linux.I'm assuming that you are using pdflatex to process the latex file(s) produced by Sweave. Assuming also that you are using latex's \include command (e.g., \include{chap1}, \include{chap2}, etc.) inside your master file, you could add a line like \includeonly{chap2} in the preamble to just create a pdf file containing only Chapter 2. The easiest way to do this is probably to edit the master latex file by hand, after having run pdflatex a few times to produce the full document (you need to do this to get all the pagination and cross-referencing information into the .aux, .toc, and other files). Another way to accomplish 2 is to install Ubuntu's pdftk package then use the pdftk command with the "cat" option to extract whatever pages you want.
Dear Anne-Marie, we had a similar problem to handle the LaTeX book that ships with the seqinr package (http://pbil.univ-lyon1.fr/software/seqinr/seqinr_1_1-5.pdf). Here is basically the approach we have used: o Each book chapter is written first as a LaTeX article produced by Sweaving its corresponding *.rnw file. They are therefore available as individual articles in PDF. o The book itself is produced by collating the core of each LaTeX source files and turning articles titles into chapter titles (among other things such as computing the full author list from article authors). The book is therefore also available in PDF without changing the *.rnw files. There are of course some constraints for this to work (e.g. all articles are sharing the same *.bib file, all figures are in the same folder and we are using the following (ugly) construct: <<hack, echo=FALSE, fig=FALSE, results=hide, eval=TRUE>>.CurFileName <- get("file", env = parent.frame(3)) .PrefixName <- strsplit(.CurFileName, "\\.")[[1]][1] @ \SweaveOpts{prefix.string = ../figs/\Sexpr{.PrefixName} } to avoid collisions between figures names produced by Sweave by prefixing them with the current *.rnw file name, etc.) If you want to know more the source code for the book is distributed with the seqinr package in the /doc/src folder, see /doc/src/template/template.pdf file as a starter. Please send an e-mail to the seqinr diffusion list if you have more questions. HTH, Jean -- Jean R. Lobry (lobry at biomserv.univ-lyon1.fr) Laboratoire BBE-CNRS-UMR-5558, Univ. C. Bernard - LYON I, 43 Bd 11/11/1918, F-69622 VILLEURBANNE CEDEX, FRANCE allo : +33 472 43 27 56 fax : +33 472 43 13 88 http://pbil.univ-lyon1.fr/members/lobry/
Hi. Either split the resulting PDF (either interactively in your PDF reader or using something like pdftk) or do what I have done for my thesis: Have separate Rnw files for each chapter, use a Makefile to generate the tex files, and then have a master latex document for the main project that uses \include to include the relevant chapters. See my Makefile below. This is based on the one discussed in that previous thread. I also generate standalone "chapters" (which end up as papers). A simple way to do this (if no changes are necessary), is to create separate "master" latex files which only import that single "chapter". For me, a thesis chapter always needs a lot of cutting to create a paper, so I use subversion to create a "branch" and then can merge changes back and forth as necessary. Factor out common functionality into an .R file and import that code (and run it) from within your first <<init>> block. This does mean the same stuff is computed multiple times, but you could choose to cache data on a first run if it is a problem. I don't - the common stuff (like database queries and fixing the data) are quick to perform. Best wishes, Mark My makefile: # # Makefile # # $Id: Makefile 1042 2008-05-03 16:27:16Z mark $ # # Global project Makefile # # # This makefile was based on # http://209.85.135.104/search?q=cache:-fU4UEn36AgJ:kriemhild.uft.uni-bremen.de/viewcvs/Makefile.rnoweb+makefile+sweave+latex&hl=en&ct=clnk&cd=10&client=safari # (The original site found on Google is no longer available and I couldn't track a new version) # This is credited to Johannes Ranke, based on work by Nicholas Lewin-Koh and Rouben Rostmaian # Further advice was provided by Deepayan Sarkar via the R-help mailing list. # # The master LaTeX document is thesis.tex, and \include{}'s the required chapters # Other master documents are acnr_master.tex (which includes acnr_paper.tex), genetic_master and drpla_master # This approach ensures integrity of the Sweave files for papers MASTER = thesis.pdf acnr_master.pdf genetic_master.pdf genetic_suppl.pdf drpla_master.pdf icars_master.pdf clinical_master.pdf # the master document depends on all of the tex files RNWFILES = $(wildcard *.Rnw) TEXFILES = $(wildcard *.tex) DEPENDS = $(patsubst %.Rnw,%.tex,$(RNWFILES)) $(TEXFILES) $(RFILES) RERUN = "(There were undefined references|Rerun to get (citations|cross-references|the bars) (correct|right)|Table widths have changed. Rerun LaTeX.|Linenumber reference failed)" RERUNBIB = "No file.*\.bbl|Citation.*undefined" all: $(MASTER) $(MASTER): $(DEPENDS) %.tex: %.Rnw ataxia.R R CMD SWEAVE '$<' %.pdf: %.tex @pdflatex $< @makeindex -s $*.ist -t $*.alg -o $*.acr $*.acn @egrep -c $(RERUNBIB) $*.log && (bibtex $*;pdflatex $<); true @egrep $(RERUN) $*.log && (pdflatex $<) ; true @egrep $(RERUN) $*.log && (pdflatex $<) ; true %.txt: %.pdf @pdftotext -nopgbrk $< clean: @rm -f *.aux *.log *.bbl *.blg *.brf *.cb *.ind *.idx *.ilg \ *.inx *.ps *.dvi *.toc *.out *.lot *~ *.lof *.alg *.acn *.acr *.glo *.ist *.ttt *.fff \ *.eps *.pdf *.svn *.dot *.lol @rm -f $(patsubst %.Rnw,%.tex,$(RNWFILES)) 2008/5/21 Anne-Marie Ternes <amternes at gmail.com>:> Dear R-help, > > I am using Sweave and pdflatex to generate a large report from data > contained in my database (Postgres via RODBC). Currently, I work with > a single R/Sweave file, containing several "chapter" indications for > the Latex engine. My master tex file sets the document class, and > includes the introduction, the main Sweave file, and a conclusions and > reference file. I use a makefile to produce the final PDF (based on > the thread "Sweave, R and complex latex projects: > http://tolstoy.newcastle.edu.au/R/e2/help/06/11/4891.html) > > What I would like to do, is to be able to get 2 types of output with > the same code (I'm lazy ;-) ): > 1. my large report in a single PDF file, for printing out and distributing > 2. a PDF and HTML file *per chapter*, for displaying on our website > and allowing people to download individual chapters > > I have tried the following things: > - see if pdflatex has an option to split PDF output per chapter; as > far as I see, it doesn't > - separate the Sweave file into chapter parts. The problems here are > 1) that I do a certain number of R preparations (variables setting, > table querying) which are data that I will need in later parts of the > code, 2) that I would need to embed the generated tex files with > per-chapter master tex files setting the documentclass and other > options and including the chapter; I also tried to see if it was > possible to tell pdflatex to assume documentclass X even if it wasn't > specified in the file, but that doesn't seem to work either > - generate my large PDF report as usual and manually cut it into > chapters (tedious) > - use R via PHP to output per-chapter HTMLs which I could turn into > PDFs using output buffering; this works for the graphics, but I'm > unable to get back e.g. tabular data for proper display; also I would > loose the latex-y beauty of my PDF > > As I'm a novice in Latex and Makefiles usage, I'd be glad if you could > tell me if what I want to do is feasible (I'm sure it is), and which > would be the best, fussless method to do it (i.e. generate both types > of output without changing the R/Sweave code). > > I know you'll probably tell me to break my long Sweave code into > smaller parts, but as I briefly said above, I do some variable setting > and table querying at the start - things I will repeatedly need in > later chapters (e.g. I query a population table for computing > incidence rates several times in later chapters). If there is a better > way to split the code without having to requery the database at each > chapter, I'll be glad to know about that too! > > BTW, I'm working on Ubuntu Linux. > > Thanks a lot for your insight, > > Anne-Marie > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > ______________________________________________________________________ > This email has been scanned by the MessageLabs Email Security System. > For more information please visit http://www.messagelabs.com/email > ______________________________________________________________________ >-- Dr. Mark Wardle Specialist registrar, Neurology Cardiff, UK