]> git.donarmstrong.com Git - xtable.git/commitdiff
Response to feature request #2104 asking for \centering rather than center environmen...
authordscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Thu, 16 Aug 2012 04:02:18 +0000 (04:02 +0000)
committerdscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Thu, 16 Aug 2012 04:02:18 +0000 (04:02 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@35 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/NEWS
pkg/R/print.xtable.R
pkg/tests/test.xtable.R [new file with mode: 0644]

index 47537ba3a5965db4a5ec0f9db850be63b2770e78..1a7c96764a4babec8587a76967f11fd304a6f889 100644 (file)
--- a/pkg/NEWS
+++ b/pkg/NEWS
@@ -1,5 +1,8 @@
 1.7-1 (NOT YET RELEASED)
   * Fixed logicals bug (number 1911)
+  * Changed implementation of centering of tables. Instead of
+    inserting a centred environment, now a \centering command is
+    inserted. Response to request #2104.
 
 1.7-0
   * Added some vectorization code to improve performance.
index 3145514e517f73fdcda0c4d7be5cd56dba47afa4..79c486cef98fbdc4d0911419c637f5845ab27385 100644 (file)
@@ -63,23 +63,28 @@ print.xtable <- function(x,
     }\r
 \r
     ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 hline.after\r
-    ## 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\r
-    ## Old code that set hline.after should include c(-1, 0, nrow(x)) in the hline.after vector\r
+    ## By default it print an \hline before and after the columns names\r
+    ## independently they are printed or not and at the end of the table\r
+    ## Old code that set hline.after should include c(-1, 0, nrow(x)) in the\r
+    ## hline.after vector\r
     ## If you do not want any \hline inside the data, set hline.after to NULL\r
     ## PHEADER instead the string '\\hline\n' is used in the code\r
     ## Now hline.after counts how many time a position appear\r
     ## I left an automatic PHEADER in the longtable is this correct?\r
 \r
-    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 include.rownames, include.colnames\r
+    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 include.rownames,\r
+    ## include.colnames\r
     pos <- 0\r
     if (include.rownames) pos <- 1\r
 \r
-    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 hline.after checks\r
+    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
+    ## hline.after checks\r
     if (any(hline.after < -1) | any(hline.after > nrow(x))) {\r
         stop("'hline.after' must be inside [-1, nrow(x)]")\r
     }\r
 \r
-    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 add.to.row checks\r
+    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
+    ## add.to.row checks\r
     if (!is.null(add.to.row)) {\r
         if (is.list(add.to.row) && length(add.to.row) == 2) {\r
             if (is.null(names(add.to.row))) {\r
@@ -115,7 +120,8 @@ print.xtable <- function(x,
         ## Original code before changes in version 1.6-1\r
         ## PHEADER <- "\\hline\n"\r
 \r
-       ## booktabs code from Matthieu Stigler <matthieu.stigler@gmail.com>, 1 Feb 2012\r
+       ## booktabs code from Matthieu Stigler <matthieu.stigler@gmail.com>,\r
+        ## 1 Feb 2012\r
         if(!booktabs){\r
             PHEADER <- "\\hline\n"\r
        } else {\r
@@ -133,7 +139,8 @@ print.xtable <- function(x,
 \r
     lastcol <- rep(" ", nrow(x)+2)\r
     if (!is.null(hline.after)) {\r
-        ## booktabs change - Matthieu Stigler: fill the hline arguments separately, 1 Feb 2012\r
+        ## booktabs change - Matthieu Stigler: fill the hline arguments\r
+        ## separately, 1 Feb 2012\r
        ##\r
         ## Code before booktabs change was:\r
        ##    add.to.row$pos[[npos+1]] <- hline.after\r
@@ -184,16 +191,24 @@ print.xtable <- function(x,
     if (type == "latex") {\r
         BCOMMENT <- "% "\r
         ECOMMENT <- "\n"\r
-        ## See e-mail from "John S. Walker <jsw9c@uic.edu>" dated 5-19-2003 regarding "texfloat"\r
-        ## See e-mail form "Fernando Henrique Ferraz P. da Rosa" <academic@feferraz.net>" dated 10-28-2005 regarding "longtable"\r
+        ## See e-mail from "John S. Walker <jsw9c@uic.edu>" dated 5-19-2003\r
+        ## regarding "texfloat"\r
+        ## See e-mail form "Fernando Henrique Ferraz P. da Rosa"\r
+        ## <academic@feferraz.net>" dated 10-28-2005 regarding "longtable"\r
         if ( tabular.environment == "longtable" & floating == TRUE ) {\r
             warning("Attempt to use \"longtable\" with floating = TRUE. Changing to FALSE.")\r
             floating <- FALSE\r
         }\r
         if ( floating == TRUE ) {\r
-            ## See e-mail from "Pfaff, Bernhard <Bernhard.Pfaff@drkw.com>" dated 7-09-2003 regarding "suggestion for an amendment of the source"\r
-            ## See e-mail from "Mitchell, David" <David.Mitchell@dotars.gov.au>" dated 2003-07-09 regarding "Additions to R xtable package"\r
-            ## See e-mail from "Garbade, Sven" <Sven.Garbade@med.uni-heidelberg.de> dated 2006-05-22 regarding the floating environment.\r
+            ## See e-mail from "Pfaff, Bernhard <Bernhard.Pfaff@drkw.com>"\r
+            ## dated 7-09-2003 regarding "suggestion for an amendment of\r
+            ## the source"\r
+            ## See e-mail from "Mitchell, David"\r
+            ## <David.Mitchell@dotars.gov.au>" dated 2003-07-09 regarding\r
+            ## "Additions to R xtable package"\r
+            ## See e-mail from "Garbade, Sven"\r
+            ## <Sven.Garbade@med.uni-heidelberg.de> dated 2006-05-22\r
+            ## regarding the floating environment.\r
             BTABLE <- paste("\\begin{", floating.environment, "}",\r
                             ifelse(!is.null(table.placement),\r
                                    paste("[", table.placement, "]", sep = ""),\r
@@ -205,13 +220,19 @@ print.xtable <- function(x,
             } else {\r
                 BENVIRONMENT <- ""\r
                 EENVIRONMENT <- ""\r
-                for ( i in 1:length(latex.environments) ) {\r
-                    if ( latex.environments[i] == "" ) next\r
-                    BENVIRONMENT <- paste(BENVIRONMENT,\r
-                                          "\\begin{", latex.environments[i],\r
-                                          "}\n", sep = "")\r
-                    EENVIRONMENT <- paste("\\end{", latex.environments[i],\r
-                                          "}\n", EENVIRONMENT, sep = "")\r
+                if ("center" %in% latex.environments){\r
+                    BENVIRONMENT <- paste(BENVIRONMENT, "\\centering\n",\r
+                                          sep = "")\r
+                }\r
+                for (i in 1:length(latex.environments)) {\r
+                    if (latex.environments[i] == "") next\r
+                    if (latex.environments[i] != "center"){\r
+                        BENVIRONMENT <- paste(BENVIRONMENT,\r
+                                              "\\begin{", latex.environments[i],\r
+                                              "}\n", sep = "")\r
+                        EENVIRONMENT <- paste("\\end{", latex.environments[i],\r
+                                              "}\n", EENVIRONMENT, sep = "")\r
+                    }\r
                 }\r
             }\r
             ETABLE <- paste("\\end{", floating.environment, "}\n", sep = "")\r
@@ -228,7 +249,8 @@ print.xtable <- function(x,
                 tmp.index.start <- tmp.index.start + 1\r
             tmp.index.start <- tmp.index.start + 1\r
         }\r
-       ## Added "width" argument for use with "tabular*" or "tabularx" environments - CR, 7/2/12\r
+       ## Added "width" argument for use with "tabular*" or\r
+        ## "tabularx" environments - CR, 7/2/12\r
        if (is.null(width)){\r
             WIDTH <-""\r
        } else if (is.element(tabular.environment,\r
@@ -250,7 +272,8 @@ print.xtable <- function(x,
                         sep = "", collapse = ""),\r
                   sep = "")\r
 \r
-        ## fix 10-26-09 (robert.castelo@upf.edu) the following 'if' condition is added here to support\r
+        ## fix 10-26-09 (robert.castelo@upf.edu) the following\r
+        ## 'if' condition is added here to support\r
         ## a caption on the top of a longtable\r
         if (tabular.environment == "longtable" && caption.placement == "top") {\r
             if (is.null(short.caption)){\r
@@ -264,7 +287,8 @@ print.xtable <- function(x,
                                   sep = "")\r
             }\r
         }\r
-        ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 add.to.row position -1\r
+        ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
+        ## add.to.row position -1\r
         BTABULAR <- paste(BTABULAR, lastcol[1], sep = "")\r
         ## the \hline at the end, if present, is set in full matrix\r
         ETABULAR <- paste("\\end{", tabular.environment, "}\n", sep = "")\r
@@ -276,7 +300,8 @@ print.xtable <- function(x,
             ETABULAR <- paste(ETABULAR, "}\n", sep = "")\r
        }\r
 \r
-        ## BSIZE contributed by Benno <puetz@mpipsykl.mpg.de> in e-mail dated Wednesday, December 01, 2004\r
+        ## BSIZE contributed by Benno <puetz@mpipsykl.mpg.de> in e-mail\r
+        ## dated Wednesday, December 01, 2004\r
         if (is.null(size) || !is.character(size)) {\r
             BSIZE <- ""\r
             ESIZE <- ""\r
@@ -304,7 +329,8 @@ print.xtable <- function(x,
         BTD2 <- ""\r
         BTD3 <- ""\r
         ETD  <- ""\r
-        ## Based on contribution from Jonathan Swinton <jonathan@swintons.net> in e-mail dated Wednesday, January 17, 2007\r
+        ## Based on contribution from Jonathan Swinton\r
+        ## <jonathan@swintons.net> in e-mail dated Wednesday, January 17, 2007\r
         sanitize <- function(str) {\r
             result <- str\r
             result <- gsub("\\\\", "SANITIZE.BACKSLASH", result)\r
@@ -327,10 +353,12 @@ print.xtable <- function(x,
         sanitize.numbers <- function(x) {\r
             result <- x\r
             if ( math.style.negative ) {\r
-                ## Jake Bowers <jwbowers@illinois.edu> in e-mail from 2008-08-20 suggested\r
-                ## disabling this feature to avoid problems with LaTeX's dcolumn package.\r
-                ## by Florian Wickelmaier <florian.wickelmaier@uni-tuebingen.de> in e-mail\r
-                ## from 2008-10-03 requested the ability to use the old behavior.\r
+                ## Jake Bowers <jwbowers@illinois.edu> in e-mail\r
+                ## from 2008-08-20 suggested disabling this feature to avoid\r
+                ## problems with LaTeX's dcolumn package.\r
+                ## by Florian Wickelmaier <florian.wickelmaier@uni-tuebingen.de>\r
+                ## in e-mail from 2008-10-03 requested the ability to use the\r
+                ## old behavior.\r
                 for(i in 1:length(x)) {\r
                     result[i] <- gsub("-", "$-$", result[i], fixed = TRUE)\r
                 }\r
@@ -366,7 +394,8 @@ print.xtable <- function(x,
         align.tmp <- align.tmp[align.tmp!="|"]\r
         BTD2 <- matrix(align.tmp[(2-pos):(ncol(x)+1)],\r
                        nrow = nrow(x), ncol = ncol(x)+pos, byrow = TRUE)\r
-        ## Based on contribution from Jonathan Swinton <jonathan@swintons.net> in e-mail dated Wednesday, January 17, 2007\r
+        ## Based on contribution from Jonathan Swinton <jonathan@swintons.net>\r
+        ## in e-mail dated Wednesday, January 17, 2007\r
         BTD2[regexpr("^p", BTD2)>0] <- "left"\r
         BTD2[BTD2 == "r"] <- "right"\r
         BTD2[BTD2 == "l"] <- "left"\r
@@ -378,7 +407,8 @@ print.xtable <- function(x,
             result <- gsub("&", "&amp ", result, fixed = TRUE)\r
             result <- gsub(">", "&gt ", result, fixed = TRUE)\r
             result <- gsub("<", "&lt ", result, fixed = TRUE)\r
-            ## Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> on 2006/10/05 recommended not escaping underscores.\r
+            ## Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> on 2006/10/05\r
+            ## recommended not escaping underscores.\r
             ## result <- gsub("_", "\\_", result, fixed=TRUE)\r
             return(result)\r
         }\r
@@ -386,7 +416,8 @@ print.xtable <- function(x,
             return(x)\r
         }\r
         sanitize.final <- function(result) {\r
-            ## Suggested by Uwe Ligges <ligges@statistik.uni-dortmund.de> in e-mail dated 2005-07-30.\r
+            ## Suggested by Uwe Ligges <ligges@statistik.uni-dortmund.de>\r
+            ## in e-mail dated 2005-07-30.\r
             result$text <- gsub("  *", " ",  result$text, fixed = TRUE)\r
             result$text <- gsub(' align="left"',  "", result$text,\r
                                 fixed = TRUE)\r
@@ -396,11 +427,14 @@ print.xtable <- function(x,
 \r
     result <- string("", file = file, append = append)\r
     info <- R.Version()\r
-    ## modified Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 to set automatically the package version\r
+    ## modified Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
+    ## to set automatically the package version\r
     result <- result + BCOMMENT + type + " table generated in " +\r
-        info$language + " " + info$major + "." + info$minor + " by xtable " + packageDescription('xtable')$Version + " package" + ECOMMENT\r
+              info$language + " " + info$major + "." + info$minor +\r
+              " by xtable " +  packageDescription('xtable')$Version +\r
+              " package" + ECOMMENT\r
     result <- result + BCOMMENT + date() + ECOMMENT\r
-  ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 only.contents\r
+    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 only.contents\r
     if (!only.contents) {\r
         result <- result + BTABLE\r
         result <- result + BENVIRONMENT\r
@@ -418,7 +452,8 @@ print.xtable <- function(x,
         result <- result + BSIZE\r
         result <- result + BTABULAR\r
     }\r
-    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28 include.colnames, include.rownames\r
+    ## Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
+    ## include.colnames, include.rownames\r
     if (include.colnames) {\r
         result <- result + BROW + BTH\r
         if (include.rownames) {\r
@@ -467,11 +502,14 @@ print.xtable <- function(x,
 ##    return(y)\r
 ##  }\r
     varying.digits <- is.matrix( attr( x, "digits", exact = TRUE ) )\r
-    ## Code for letting "digits" be a matrix was provided by Arne Henningsen <ahenningsen@agric-econ.uni-kiel.de> in e-mail dated 2005-06-04.\r
+    ## Code for letting "digits" be a matrix was provided by\r
+    ## Arne Henningsen <ahenningsen@agric-econ.uni-kiel.de>\r
+    ## in e-mail dated 2005-06-04.\r
     ##if( !varying.digits ) {\r
     ## modified Claudio Agostinelli <claudio@unive.it> dated 2006-07-28\r
-    ##  attr(x,"digits") <- matrix( attr( x, "digits",exact=TRUE ), nrow = nrow(x), ncol = ncol(x)+1, byrow = TRUE )\r
-  ##}\r
+    ##  attr(x,"digits") <- matrix( attr( x, "digits",exact=TRUE ),\r
+    ## nrow = nrow(x), ncol = ncol(x)+1, byrow = TRUE )\r
+    ##}\r
     for(i in 1:ncol(x)) {\r
        xcol <- x[, i]\r
        if(is.factor(xcol))\r
@@ -518,7 +556,8 @@ print.xtable <- function(x,
        ## End Ian Fellows changes\r
 \r
         if ( any(ina) ) cols[ina, i+pos] <- NA.string\r
-        ## Based on contribution from Jonathan Swinton <jonathan@swintons.net> in e-mail dated Wednesday, January 17, 2007\r
+        ## Based on contribution from Jonathan Swinton <jonathan@swintons.net>\r
+        ## in e-mail dated Wednesday, January 17, 2007\r
         if ( is.numeric.column ) {\r
             cols[, i+pos] <- sanitize.numbers(cols[, i+pos])\r
         } else {\r
@@ -551,7 +590,8 @@ print.xtable <- function(x,
                 result <- result + PHEADER\r
             }\r
 \r
-            ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the following 'if' condition is inserted in order to avoid\r
+            ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the\r
+            ## following 'if' condition is inserted in order to avoid\r
             ## that bottom caption interferes with a top caption of a longtable\r
             if(caption.placement == "bottom"){\r
                 if ((!is.null(caption)) && (type == "latex")) {\r
diff --git a/pkg/tests/test.xtable.R b/pkg/tests/test.xtable.R
new file mode 100644 (file)
index 0000000..d39e328
--- /dev/null
@@ -0,0 +1,24 @@
+require(xtable)\r
+ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)\r
+trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)\r
+group <- gl(2,10,20, labels=c("Ctl","Trt"))\r
+weight <- c(ctl, trt)\r
+lm.D9 <- lm(weight ~ group)\r
+class(lm.D9)\r
+\r
+xtable(lm.D9, caption="\\tt latex.environment=\"center\"")\r
+\r
+## % latex table generated in R 2.15.0 by xtable 1.7-1 package\r
+## % Thu Aug 16 15:44:09 2012\r
+## \begin{table}[ht]\r
+## \centering\r
+## \begin{tabular}{rrrrr}\r
+##   \hline\r
+##  & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\\r
+##   \hline\r
+## (Intercept) & 5.0320 & 0.2202 & 22.85 & 0.0000 \\\r
+##   groupTrt & -0.3710 & 0.3114 & -1.19 & 0.2490 \\\r
+##    \hline\r
+## \end{tabular}\r
+## \caption{\tt latex.environment="center"}\r
+## \end{table}\r