From 77dded56fc48dd09178ecfe11cd47bd6dd77d32f Mon Sep 17 00:00:00 2001 From: roosen Date: Thu, 2 Feb 2012 08:34:09 +0000 Subject: [PATCH] Submitting multiple changes as R-Forge SVN has been down. Added the use of getOption() to obtain the defaults for print.xtable(). Added the "booktabs" argument as suggested by Matthieu Stigler. Removed the changes regarding hline handling, as these were work-arounds to enable "booktabs", are no longer needed, and seemed to be causing missing hlines with "longtable" (perhaps only when combined with the booktabs changes). git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@26 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/DESCRIPTION | 2 +- pkg/NEWS | 19 ++++-- pkg/R/print.xtable.R | 108 ++++++++++++++++++++------------- pkg/inst/doc/xtableGallery.snw | 39 ++++++++---- pkg/man/print.xtable.Rd | 52 ++++++++++------ pkg/man/xtable.Rd | 14 +++++ 6 files changed, 156 insertions(+), 78 deletions(-) diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index 4c8eab3..d3bfaed 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -1,5 +1,5 @@ Package: xtable -Version: 1.6-1 +Version: 1.7-0 Date: 2011/10/06 Title: Export tables to LaTeX or HTML Author: David B. Dahl diff --git a/pkg/NEWS b/pkg/NEWS index 78eefef..4e41217 100644 --- a/pkg/NEWS +++ b/pkg/NEWS @@ -1,20 +1,29 @@ -1.6-1 (NOT YET RELEASED) +1.7-0 (NOT YET RELEASED) * Added some vectorization code to improve performance. * Added "toLatex" method. + * Included "print.xtable" in the exported methods in the NAMESPACE file. * Added "print.results" argument to "print" that can be used to suppress the printing. * Added "format.args" argument to "print" that can be used to pass additional arguments such as "big.marks" to "formatC()". * Added "short.caption" argument to "print" to let the user - specify a Latex short caption used when creating a list of tables. + specify a Latex short caption used when creating a list of tables. + Conceptually this should really be an argument to "xtable()", but + it was added to "print()" so authors of other packages don't need + to change their "xtable" methods. * Added "rotate.colnames" and "rotate.rownames" arguments to "print.xtable". + * Added "booktabs" argument to use the "\toprule", "\midrule", and + "\bottomrule" tags from the Latex "booktabs" package rather than + using "\hline" for all horizontal lines. + * Changed the "print.xtable()" arguments to use "getOption()" + to check the options for a default value. This was suggested + since "print.xtable()" has a lot of arguments that the user + will typically leave unchanged between tables. * Added an "is.null()" check on the "table.placement" argument. - * Put in some changes from John Leonard regarding the placement - of newlines when "longtable" and "add.to.row" are used together. - * Included "print.xtable" in the exported methods in the NAMESPACE file. * Added an example of header and footer specification with longtable to the vignette. + * Added examples using the new arguments to the vignette. 1.6-0 (2011-10-07) * Allow "table*" as a value for "floating.environment" in print.xtable(). diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index 2c1c5dc..282e2a2 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -23,32 +23,35 @@ ### MA 02111-1307, USA print.xtable <- function( x, - type="latex", - file="", - append=FALSE, - floating=TRUE, - floating.environment="table", - table.placement="ht", - caption.placement="bottom", - latex.environments=c("center"), - tabular.environment="tabular", - size=NULL, - hline.after=c(-1,0,nrow(x)), - NA.string="", - include.rownames=TRUE, - include.colnames=TRUE, - only.contents=FALSE, - add.to.row=NULL, - sanitize.text.function=NULL, - sanitize.rownames.function=sanitize.text.function, - sanitize.colnames.function=sanitize.text.function, - math.style.negative=FALSE, - html.table.attributes="border=1", - print.results=TRUE, - format.args=NULL, - short.caption=NULL, - rotate.rownames=FALSE, - rotate.colnames=FALSE, + type=getOption("xtable.type", "latex"), + file=getOption("xtable.file", ""), + append=getOption("xtable.append", FALSE), + floating=getOption("xtable.floating", TRUE), + floating.environment=getOption("xtable.floating.environment", "table"), + table.placement=getOption("xtable.table.placement", "ht"), + caption.placement=getOption("xtable.caption.placement", "bottom"), + latex.environments=getOption("xtable.latex.environments", c("center")), + tabular.environment=getOption("xtable.tabular.environment", "tabular"), + size=getOption("xtable.size", NULL), + hline.after=getOption("xtable.hline.after", c(-1,0,nrow(x))), + NA.string=getOption("xtable.NA.string", ""), + include.rownames=getOption("xtable.include.rownames", TRUE), + include.colnames=getOption("xtable.include.colnames", TRUE), + only.contents=getOption("xtable.only.contents", FALSE), + add.to.row=getOption("xtable.add.to.row", NULL), + sanitize.text.function=getOption("xtable.sanitize.text.function", NULL), + sanitize.rownames.function=getOption("xtable.sanitize.rownames.function", + sanitize.text.function), + sanitize.colnames.function=getOption("xtable.sanitize.colnames.function", + sanitize.text.function), + math.style.negative=getOption("xtable.math.style.negative", FALSE), + html.table.attributes=getOption("xtable.html.table.attributes", "border=1"), + print.results=getOption("xtable.print.results", TRUE), + format.args=getOption("xtable.format.args", NULL), + short.caption=getOption("xtable.short.caption", NULL), + rotate.rownames=getOption("xtable.rotate.rownames", FALSE), + rotate.colnames=getOption("xtable.rotate.colnames", FALSE), + booktabs = getOption("xtable.booktabs", FALSE), ...) { # Claudio Agostinelli dated 2006-07-28 hline.after # By default it print an \hline before and after the columns names independently they are printed or not and at the end of the table @@ -94,21 +97,42 @@ print.xtable <- function( # Claudio Agostinelli dated 2006-07-28 add.to.row # Add further commands at the end of rows if (type=="latex") { - PHEADER <- "\\hline\n" - # John Leonard October 21, 2011 - # The extra \hline gets in the way when using longtable and add.to.row - if(tabular.environment=="longtable" && !is.null(add.to.row) ) { - PHEADER <- "" - } + ## Original code before changes in version 1.6-1 + # PHEADER <- "\\hline\n" + + # booktabs code from Matthieu Stigler , 1 Feb 2012 + if(!booktabs){ + PHEADER <- "\\hline\n" + } else { + PHEADER <- ifelse(-1%in%hline.after, "\\toprule\n", "") + if(0%in%hline.after) { + PHEADER <- c(PHEADER, "\\midrule\n") + } + if(nrow(x)%in%hline.after) { + PHEADER <- c(PHEADER, "\\bottomrule\n") + } + } } else { PHEADER <- "" } lastcol <- rep(" ", nrow(x)+2) if (!is.null(hline.after)) { - add.to.row$pos[[npos+1]] <- hline.after - add.to.row$command <- c(add.to.row$command, PHEADER) + # booktabs change - Matthieu Stigler: fill the hline arguments separately, 1 Feb 2012 + # + # Code before booktabs change was: + # add.to.row$pos[[npos+1]] <- hline.after + + if (!booktabs){ + add.to.row$pos[[npos+1]] <- hline.after + } else { + for(i in 1:length(hline.after)) { + add.to.row$pos[[npos+i]] <- hline.after[i] + } + } + add.to.row$command <- c(add.to.row$command, PHEADER) } + if ( length(add.to.row$command) > 0 ) { for (i in 1:length(add.to.row$command)) { addpos <- add.to.row$pos[[i]] @@ -440,19 +464,16 @@ print.xtable <- function( full[,multiplier*(0:(ncol(x)+pos-1))+6] <- ETD full[,multiplier*(ncol(x)+pos)+2] <- paste(EROW, lastcol[-(1:2)], sep=" ") - - # John Leonard October 21, 2011 - # Removes the "\\" from the last row of the contents so that - # booktabs (\bottomline) appears in the correct position. - if(tabular.environment=="longtable" & !is.null(add.to.row)) { - full[dim(full)[1],multiplier*(ncol(x)+pos)+2] <- "%\n" - } - + if (type=="latex") full[,2] <- "" result <- result + lastcol[2] + paste(t(full),collapse="") if (!only.contents) { if (tabular.environment == "longtable") { - result <- result + PHEADER + # booktabs change added the if() - 1 Feb 2012 + if(!booktabs) { + result <- result + PHEADER + } + ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the following 'if' condition is inserted in order to avoid ## that bottom caption interferes with a top caption of a longtable if(caption.placement=="bottom"){ @@ -471,6 +492,7 @@ print.xtable <- function( result <- result + ETABLE } result <- sanitize.final(result) + if (print.results){ print(result) } diff --git a/pkg/inst/doc/xtableGallery.snw b/pkg/inst/doc/xtableGallery.snw index 79d1626..da83911 100644 --- a/pkg/inst/doc/xtableGallery.snw +++ b/pkg/inst/doc/xtableGallery.snw @@ -31,6 +31,7 @@ The xtable gallery \SweaveOpts{prefix.string=figdir/fig,debug=TRUE,eps=FALSE,echo=TRUE} \usepackage{rotating} \usepackage{longtable} +\usepackage{booktabs} %\usepackage{hyperref} \begin{document} @@ -397,10 +398,30 @@ print((tli.table),rotate.rownames=TRUE,rotate.colnames=TRUE) @ \subsection{Horizontal lines} + +\subsubsection{Line locations} + +Use the {\tt hline.after} argument to specify the position of the horizontal lines. + <>= print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE) @ +\subsubsection{Line styles} + +The \LaTeX package {\tt booktabs} can be used to specify different +line style tags for top, middle, and bottom lines. Specifying +{\tt booktabs = TRUE} will lead to separate tags being generated +for the three line types. + +Insert \verb|\usepackage{booktabs}| in your \LaTeX preamble and +define the {\tt toprule}, {\tt midrule}, and {\tt bottomrule} +tags to specify the line styles. + +<>= +print(tli.table , booktabs=TRUE) +@ + \subsection{Table-level \LaTeX} <>= print(xtable(anova(glm.D93)),size="small",floating=FALSE) @@ -408,7 +429,7 @@ print(xtable(anova(glm.D93)),size="small",floating=FALSE) \subsection{Long tables} -Remember to insert \verb|\usepackage{longtable}| in your LaTeX preamble. +Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble. See Table \ref{tabbig}. <>= @@ -422,11 +443,11 @@ x.big <- xtable(x,label='tabbig', print(x.big,tabular.environment='longtable',floating=FALSE) @ -\subsubsection{Long tables with headers on each page} +\subsubsection{Long tables with the header on each page} The {\tt add.to.row} argument can be used to display the header for a long table on each page, and to add a "continued" footer -on all pages except the last page. +on all pages except the last page. See Table \ref{tabbig2}. <>= library(xtable) @@ -442,9 +463,9 @@ addtorow$command<-c(paste( " {\\footnotesize Continued on next page} \n", " \\endfoot \n", " \\endlastfoot \n",sep="")) -x.big <- xtable(x, label = "tabbig", - caption = "Example of longtable spanning several pages") -print(x.big, tabular.environment = "longtable", floating = FALSE, +x.big2 <- xtable(x, label = "tabbig2", + caption = "Example of longtable with the header on each page") +print(x.big2, tabular.environment = "longtable", floating = FALSE, include.rownames=FALSE,add.to.row=addtorow,hline.after=c(-1) ) @ @@ -470,11 +491,7 @@ output and also return the character strings invisibly. The printing to standard output can be suppressed by specifying {\tt print.results = FALSE}. <<>>= -# This will print the output and store it. -x.out <- print(tli.table) - -# This will just store the value. -x.out2 <- print(tli.table, print.results = FALSE) +x.out <- print(tli.table, print.results = FALSE) @ Formatted output can also be captured without printing with the diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd index 8c75a25..62d05b1 100644 --- a/pkg/man/print.xtable.Rd +++ b/pkg/man/print.xtable.Rd @@ -3,24 +3,37 @@ \title{Print Export Tables} \description{Function returning and displaying or writing to disk the LaTeX or HTML code associated with the supplied object of class \code{xtable}.} \usage{ - \method{print}{xtable}(x, type="latex", file="", append=FALSE, - floating=TRUE, floating.environment="table", - table.placement = "ht", caption.placement="bottom", - latex.environments=c("center"), - tabular.environment = "tabular", size=NULL, - hline.after=c(-1,0,nrow(x)), NA.string = "", include.rownames=TRUE, - include.colnames=TRUE, only.contents=FALSE, add.to.row=NULL, - sanitize.text.function=NULL, - sanitize.rownames.function=sanitize.text.function, - sanitize.colnames.function=sanitize.text.function, - math.style.negative=FALSE, - html.table.attributes="border=1", - print.results=TRUE, - format.args=NULL, - short.caption=NULL, - rotate.rownames = FALSE, - rotate.colnames = FALSE, - ...)} +\method{print}{xtable}(x, + type=getOption("xtable.type", "latex"), + file=getOption("xtable.file", ""), + append=getOption("xtable.append", FALSE), + floating=getOption("xtable.floating", TRUE), + floating.environment=getOption("xtable.floating.environment", "table"), + table.placement=getOption("xtable.table.placement", "ht"), + caption.placement=getOption("xtable.caption.placement", "bottom"), + latex.environments=getOption("xtable.latex.environments", c("center")), + tabular.environment=getOption("xtable.tabular.environment", "tabular"), + size=getOption("xtable.size", NULL), + hline.after=getOption("xtable.hline.after", c(-1,0,nrow(x))), + NA.string=getOption("xtable.NA.string", ""), + include.rownames=getOption("xtable.include.rownames", TRUE), + include.colnames=getOption("xtable.include.colnames", TRUE), + only.contents=getOption("xtable.only.contents", FALSE), + add.to.row=getOption("xtable.add.to.row", NULL), + sanitize.text.function=getOption("xtable.sanitize.text.function", NULL), + sanitize.rownames.function=getOption("xtable.sanitize.rownames.function", + sanitize.text.function), + sanitize.colnames.function=getOption("xtable.sanitize.colnames.function", + sanitize.text.function), + math.style.negative=getOption("xtable.math.style.negative", FALSE), + html.table.attributes=getOption("xtable.html.table.attributes", "border=1"), + print.results=getOption("xtable.print.results", TRUE), + format.args=getOption("xtable.format.args", NULL), + short.caption=getOption("xtable.short.caption", NULL), + rotate.rownames=getOption("xtable.rotate.rownames", FALSE), + rotate.colnames=getOption("xtable.rotate.colnames", FALSE), + booktabs = getOption("xtable.booktabs", FALSE), + ...)} \arguments{ \item{x}{An object of class \code{"xtable"}.} \item{type}{Type of table to produce. Possible values for \code{type} are \code{"latex"} or \code{"html"}. @@ -58,6 +71,7 @@ \item{short.caption}{A "short caption" can be specified that gets used as the table descriptor when LaTeX generates a "List of Tables".} \item{rotate.rownames}{If \code{TRUE}, the row names are displayed vertically in LaTeX.} \item{rotate.colnames}{If \code{TRUE}, the column names are displayed vertically in LaTeX.} + \item{booktabs}{If \code{TRUE}, the \code{toprule}, \code{midrule} and \code{bottomrule} tags from the LaTex "booktabs" package are used rather than \code{hline} for the horizontal line tags.} \item{...}{Additional arguments. (Currently ignored.)} } \details{ @@ -69,6 +83,8 @@ From version 1.4-3, all non-numeric columns are sanitized, and all LaTeX special characters are sanitised for LaTeX output. See Section 3 of the \code{xtableGallery} vignette for an example of customising the sanitization. From version 1.4-4, the sanitization also applies to column names. To remove any text sanitization, specify \code{sanitize.text.function=function(x){x}}. + + From version 1.6-1 the default values for the arguments other than \code{x} are obtainined using \code{getOption()}. Hence the user can set the values once with \code{options()} rather than setting them in every call to \code{print.xtable()}. } \author{David Dahl \email{dahl@stat.tamu.edu} with contributions and suggestions from many others (see source code).} \references{ diff --git a/pkg/man/xtable.Rd b/pkg/man/xtable.Rd index 7211a83..41628ac 100644 --- a/pkg/man/xtable.Rd +++ b/pkg/man/xtable.Rd @@ -283,6 +283,20 @@ rownames(mat) <- "$y_{t-1}$" colnames(mat) <- c("$R^2$", "$\\\bar{R}^2$", "F-stat", "S.E.E", "DW") print(xtable(mat), type="latex", sanitize.text.function = function(x){x}) +## Demonstrate booktabs +print(tli.table) +print(tli.table , hline.after=c(-1,0)) +print(tli.table , hline.after=NULL) +print(tli.table , add.to.row=list(pos=list(2), command=c("\\vspace{2mm} \n"))) + +print(tli.table , booktabs=TRUE) +print(tli.table , booktabs=TRUE, hline.after=c(-1,0)) +print(tli.table , booktabs=TRUE, hline.after=NULL) +print(tli.table , booktabs=TRUE, + add.to.row=list(pos=list(2), command=c("\\vspace{2mm} \n"))) +print(tli.table , booktabs=TRUE, add.to.row=list(pos=list(2), + command=c("youhou\n")),tabular.environment = "longtable") + \testonly{ for(i in c("latex","html")) { outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="") -- 2.39.2