]> git.donarmstrong.com Git - xtable.git/commitdiff
Added the "width" argument for use with "tabular*" or "tabularx" tabular environments.
authorroosen <roosen@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Tue, 7 Feb 2012 12:38:37 +0000 (12:38 +0000)
committerroosen <roosen@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Tue, 7 Feb 2012 12:38:37 +0000 (12:38 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@29 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/NEWS
pkg/R/print.xtable.R
pkg/R/table.attributes.R
pkg/inst/doc/xtableGallery.snw
pkg/man/print.xtable.Rd

index 1c407bd27abf632d22af71e3276186c84886ce06..1766d38567fb60e2288af6694700dcaf349b3ae0 100644 (file)
--- a/pkg/NEWS
+++ b/pkg/NEWS
@@ -16,6 +16,8 @@
   * Added "scalebox" argument to include a "\scalebox" clause around
     the tabular environment with the specified value used as the
     scaling factor.    
+  * Added "width" argument to allow specification of the width
+    value in tabular environments such as "tabularx".  
   * 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
index 8c56c33875fe660c5ef4fde002e50ea7ab39db8b..94cb18d0b04340167e2c7f1d9050373b3ecd28fd 100644 (file)
@@ -52,6 +52,7 @@ print.xtable <- function(
   rotate.colnames=getOption("xtable.rotate.colnames", FALSE),\r
   booktabs = getOption("xtable.booktabs", FALSE),\r
   scalebox = getOption("xtable.scalebox", NULL),\r
+  width = getOption("xtable.width", NULL),\r
   ...) {\r
   # If caption is length 2, treat the second value as the "short caption"\r
   caption <- attr(x,"caption",exact=TRUE)\r
@@ -203,10 +204,20 @@ print.xtable <- function(
       while ( attr(x,"align",exact=TRUE)[tmp.index.start] == '|' ) tmp.index.start <- tmp.index.start + 1\r
       tmp.index.start <- tmp.index.start + 1\r
     }\r
-    BTABULAR <- paste("\\begin{",tabular.environment,"}{",\r
-                      paste(c(attr(x, "align",exact=TRUE)[tmp.index.start:length(attr(x,"align",exact=TRUE))], "}\n"),\r
-                            sep="", collapse=""),\r
-                      sep="")\r
+       # Added "width" argument for use with "tabular*" or "tabularx" environments - CR, 7/2/12\r
+       if (is.null(width)){\r
+         WIDTH <-""\r
+       } else if (is.element(tabular.environment, c("tabular", "longtable"))){\r
+         warning("Ignoring 'width' argument.  The 'tabular' and 'longtable' environments do not support a width specification.  Use another environment such as 'tabular*' or 'tabularx' to specify the width.")\r
+         WIDTH <- ""\r
+       } else {\r
+         WIDTH <- paste("{", width, "}", sep="")\r
+       }\r
+       \r
+    BTABULAR <- paste("\\begin{",tabular.environment,"}", WIDTH, "{",\r
+        paste(c(attr(x, "align",exact=TRUE)[tmp.index.start:length(attr(x,"align",\r
+                   exact=TRUE))], "}\n"), sep="", collapse=""),\r
+        sep="")\r
 \r
     ## fix 10-26-09 (robert.castelo@upf.edu) the following 'if' condition is added here to support\r
     ## a caption on the top of a longtable\r
index ca09e98b5fdedefb39771380ff2af8934fe78f4b..b228f6832903a3d02ed871c9ac607c4c5f667f6d 100644 (file)
@@ -72,7 +72,7 @@ label.xtable <- function(x,...) {
     aString.Width <- c(aString.Width,thisWidth)
   }
 
-  alignAllowed <- c("l","r","p","c","|") 
+  alignAllowed <- c("l","r","p","c","|","X"
   
   if (any( !(aString.Align %in% alignAllowed))) {
     warning("Nonstandard alignments in align string")
index c69c98e4176719d8a04120dd4ef1be7431bbc1db..6f52a05b4eaf784f0b366b446c91d7c01bdcaac8 100644 (file)
@@ -32,6 +32,7 @@ The xtable gallery
 \usepackage{rotating} 
 \usepackage{longtable} 
 \usepackage{booktabs}
+\usepackage{tabularx}
 %\usepackage{hyperref}
 \begin{document}
 
@@ -499,6 +500,30 @@ x.rescale <- xtable(x,label='tabrescaled',caption='A rescaled table')
 print(x.rescale, scalebox=0.7)
 @
 
+\subsection{Table Width} 
+The {\tt tabularx} tabular environment provides more alignment options,
+and has a \code{width} argument to specify the table width.
+Remember to insert \verb|\usepackage{tabularx}| in your \LaTeX preamble.
+
+<<>>=
+df.width <- data.frame(
+  "label 1 with much more text than is needed" = c("item 1", "A"), 
+  "label 2 is also very long" = c("item 2","B"),
+  "label 3" = c("item 3","C"), 
+  "label 4" = c("item 4 but again with too much text","D"),
+  check.names = FALSE)
+
+x.width <- xtable(df.width, 
+  caption="Using the 'tabularx' environment")
+align(x.width) <- "|l|X|X|l|X|"  
+@
+
+<<results=tex>>=
+print(x.width, tabular.environment="tabularx", 
+  width="\\textwidth")
+@
+
 \section{Suppressing Printing}
 By default the {\tt print} method will print the LaTeX or HTML to standard
 output and also return the character strings invisibly.  The printing to
index 1f7271e8368daf165d398e98a4e63c7c159e2db0..26dfe98b632377bd8a9c19a9cebd90dab967aa0a 100644 (file)
@@ -33,6 +33,7 @@
   rotate.colnames=getOption("xtable.rotate.colnames", FALSE),\r
   booktabs = getOption("xtable.booktabs", FALSE),\r
   scalebox = getOption("xtable.scalebox", NULL),\r
+  width = getOption("xtable.width", NULL),\r
   ...)}\r
 \arguments{\r
   \item{x}{An object of class \code{"xtable"}.}\r
@@ -72,6 +73,7 @@
   \item{rotate.colnames}{If \code{TRUE}, the column names are displayed vertically in LaTeX.}\r
   \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.}\r
   \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be added around the tabular environment with the specified value used as the scaling factor.}\r
+  \item{width}{If not \code{NULL}, the specified value is included in parenthesis between the tabular environment \code{begin} tag and the alignment specification.  This allows specification of the table width when using tabular environments such as \code{tabular*} and \code{tabularx}.  Note that table width specification is not supported with the \code{tabular} or \code{longtable} environments.}\r
   \item{...}{Additional arguments.  (Currently ignored.)}\r
 }\r
 \details{\r