From 060c03c571f3a6caeaa116ad855476fb63041053 Mon Sep 17 00:00:00 2001 From: Qiang Li Date: Mon, 18 Apr 2011 22:52:05 +0800 Subject: [PATCH 1/4] R/pgfSweaveDriver.R: Generates Hinput environments instead of Houtput. Houtput seemed strange. It surrounds both input and output, so customization would also affect the results. I made it a inner environment of Schunk just like Sinput, and renamed it to Hinput accordingly. --- R/pgfSweaveDriver.R | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/R/pgfSweaveDriver.R b/R/pgfSweaveDriver.R index ac81983..b530e02 100644 --- a/R/pgfSweaveDriver.R +++ b/R/pgfSweaveDriver.R @@ -154,7 +154,7 @@ pgfSweaveWritedoc <- function(object, chunk) which <- grep(begindoc, chunk) # add definitions for highlight environment - hstyle <- c(hstyle, "\\newenvironment{Houtput}{\\raggedright}{%\n%\n}") + hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\raggedright}{%\n%\n}") # put in the style definitions after the \documentclass command if(length(which)) { @@ -291,8 +291,8 @@ pgfSweaveRuncode <- function(object, chunk, options) { openSinput <- FALSE openSchunk <- FALSE - # for the highlighted output environment - openHoutput <- FALSE + + Sinputenv <- ifelse(options$highlight, "Hinput", "Sinput") if(length(chunkexps)==0) return(object) @@ -353,22 +353,15 @@ pgfSweaveRuncode <- function(object, chunk, options) { cat("\nRnw> ", paste(dce, collapse="\n+ "),"\n") if(options$echo && length(dce)){ - if(!openSinput & !options$highlight){ + if(!openSinput){ if(!openSchunk){ cat("\\begin{Schunk}\n",file=chunkout, append=TRUE) linesout[thisline + 1] <- srcline thisline <- thisline + 1 openSchunk <- TRUE } - cat("\\begin{Sinput}",file=chunkout, append=TRUE) + cat("\\begin{", Sinputenv, "}", sep="", file=chunkout, append=TRUE) openSinput <- TRUE - }else if(!openHoutput & options$highlight){ - - cat("\\begin{Houtput}\n",file=chunkout, append=TRUE) - linesout[thisline + 1] <- srcline - thisline <- thisline + 1 - openHoutput <- TRUE - } # Actual printing of chunk code @@ -438,7 +431,7 @@ pgfSweaveRuncode <- function(object, chunk, options) { if(length(output)>0 & (options$results != "hide")){ if(openSinput){ - cat("\n\\end{Sinput}\n", file=chunkout,append=TRUE) + cat("\n\\end{", Sinputenv, "}\n", sep="", file=chunkout, append=TRUE) linesout[thisline + 1:2] <- srcline thisline <- thisline + 2 openSinput <- FALSE @@ -488,7 +481,7 @@ pgfSweaveRuncode <- function(object, chunk, options) { cat("\n", file=chunkout, append=TRUE) if(openSinput){ - cat("\n\\end{Sinput}\n", file=chunkout, append=TRUE) + cat("\n\\end{", Sinputenv, "}\n", sep="", file=chunkout, append=TRUE) linesout[thisline + 1:2] <- srcline thisline <- thisline + 2 } @@ -499,12 +492,6 @@ pgfSweaveRuncode <- function(object, chunk, options) { thisline <- thisline + 1 } - if(openHoutput){ - cat("\\end{Houtput}\n", file=chunkout, append=TRUE) - linesout[thisline + 1] <- srcline - thisline <- thisline + 1 - } - if(is.null(options$label) & options$split) close(chunkout) From e64011b2ed4a89d4592b9d67256ad786c1ca34fd Mon Sep 17 00:00:00 2001 From: Qiang Li Date: Tue, 19 Apr 2011 03:08:06 +0800 Subject: [PATCH 2/4] R/pgfSweaveDriver.R: Removed newline_latex() at the beginning of Sinput. The {Sinput} environment should ensure the newline and determine the surrounding space. --- R/pgfSweaveDriver.R | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/R/pgfSweaveDriver.R b/R/pgfSweaveDriver.R index b530e02..8ac63f0 100644 --- a/R/pgfSweaveDriver.R +++ b/R/pgfSweaveDriver.R @@ -154,7 +154,7 @@ pgfSweaveWritedoc <- function(object, chunk) which <- grep(begindoc, chunk) # add definitions for highlight environment - hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\raggedright}{%\n%\n}") + hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\par\\noindent}{%\n%\n}") # put in the style definitions after the \documentclass command if(length(which)) { @@ -377,13 +377,9 @@ pgfSweaveRuncode <- function(object, chunk, options) { }else{ - if(nce == 1) - cat(newline_latex(),file=chunkout, append=TRUE) - highlight(parser.output=parser(text=dce), renderer=renderer_latex(document=FALSE), - output = chunkout, showPrompts=TRUE,final.newline = TRUE) - # highlight doesnt put in an ending newline for some reason + output = chunkout, showPrompts=TRUE) cat(newline_latex(),file=chunkout, append=TRUE) } From 1e17cc8e5c3920426ae75e706ecd896c0d5d33be Mon Sep 17 00:00:00 2001 From: Qiang Li Date: Tue, 19 Apr 2011 13:19:36 +0800 Subject: [PATCH 3/4] R/pgfSweaveDriver.R: cancel out the effect of hard newline at the end of Hinput Now we can define Schunk and Hinput based on trivlist, and Soutput based on Verbatim, so that vertical spaces between text/chunk/text and code/output can be individually set with \topsep, e.g. \renewenvironment{Schunk}{\begin{trivlist}\item}{\end{trivlist}} \RequirePackage{fancyvrb} \renewenvironment{Hinput}{\setlength{\topsep}{0pt}\begin{trivlist}\item} {\vspace{-\baselineskip}\end{trivlist}} \RecustomVerbatimEnvironment{Soutput}{Verbatim}{listparameters={\setlength{\topsep}{\smallskipamount}}} --- R/pgfSweaveDriver.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/pgfSweaveDriver.R b/R/pgfSweaveDriver.R index 8ac63f0..b67f446 100644 --- a/R/pgfSweaveDriver.R +++ b/R/pgfSweaveDriver.R @@ -154,7 +154,8 @@ pgfSweaveWritedoc <- function(object, chunk) which <- grep(begindoc, chunk) # add definitions for highlight environment - hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\par\\noindent}{%\n%\n}") + ## Hinput should start a newline, and cancel the ending hard newline + hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\par}{\\vspace{-\\baselineskip}}") # put in the style definitions after the \documentclass command if(length(which)) { From dada019de51c722bf81d06ccd079745b84849b9d Mon Sep 17 00:00:00 2001 From: Qiang Li Date: Tue, 19 Apr 2011 18:56:48 +0800 Subject: [PATCH 4/4] Removed hard newline_latex at the end of {Sinput} Now the ugly hack \vspace{-\baselineskip} is avoided. --- R/pgfSweaveDriver.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/pgfSweaveDriver.R b/R/pgfSweaveDriver.R index b67f446..40d23c0 100644 --- a/R/pgfSweaveDriver.R +++ b/R/pgfSweaveDriver.R @@ -154,8 +154,7 @@ pgfSweaveWritedoc <- function(object, chunk) which <- grep(begindoc, chunk) # add definitions for highlight environment - ## Hinput should start a newline, and cancel the ending hard newline - hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\par}{\\vspace{-\\baselineskip}}") + hstyle <- c(hstyle, "\\newenvironment{Hinput}{\\begin{trivlist}\\item}{\\end{trivlist}}") # put in the style definitions after the \documentclass command if(length(which)) { @@ -294,6 +293,8 @@ pgfSweaveRuncode <- function(object, chunk, options) { openSchunk <- FALSE Sinputenv <- ifelse(options$highlight, "Hinput", "Sinput") + ## Flag for the beginning of Hinput. If so, don't add hard newline_latex + beginSinput <- FALSE if(length(chunkexps)==0) return(object) @@ -363,6 +364,7 @@ pgfSweaveRuncode <- function(object, chunk, options) { } cat("\\begin{", Sinputenv, "}", sep="", file=chunkout, append=TRUE) openSinput <- TRUE + beginSinput <- TRUE } # Actual printing of chunk code @@ -378,12 +380,14 @@ pgfSweaveRuncode <- function(object, chunk, options) { }else{ + if (!beginSinput) cat(newline_latex(), file=chunkout, append=TRUE) + highlight(parser.output=parser(text=dce), renderer=renderer_latex(document=FALSE), output = chunkout, showPrompts=TRUE) - cat(newline_latex(),file=chunkout, append=TRUE) } + beginSinput <- FALSE }else{ # regular output, may be tidy'd or not