From 8e91b1d8e5075887172b70d649f4cb476df8bc87 Mon Sep 17 00:00:00 2001 From: roosen Date: Tue, 7 Feb 2012 12:38:37 +0000 Subject: [PATCH] Added the "width" argument for use with "tabular*" or "tabularx" tabular environments. git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@29 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/NEWS | 2 ++ pkg/R/print.xtable.R | 19 +++++++++++++++---- pkg/R/table.attributes.R | 2 +- pkg/inst/doc/xtableGallery.snw | 25 +++++++++++++++++++++++++ pkg/man/print.xtable.Rd | 2 ++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/pkg/NEWS b/pkg/NEWS index 1c407bd..1766d38 100644 --- 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 diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index 8c56c33..94cb18d 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -52,6 +52,7 @@ print.xtable <- function( rotate.colnames=getOption("xtable.rotate.colnames", FALSE), booktabs = getOption("xtable.booktabs", FALSE), scalebox = getOption("xtable.scalebox", NULL), + width = getOption("xtable.width", NULL), ...) { # If caption is length 2, treat the second value as the "short caption" caption <- attr(x,"caption",exact=TRUE) @@ -203,10 +204,20 @@ print.xtable <- function( while ( attr(x,"align",exact=TRUE)[tmp.index.start] == '|' ) tmp.index.start <- tmp.index.start + 1 tmp.index.start <- tmp.index.start + 1 } - BTABULAR <- paste("\\begin{",tabular.environment,"}{", - paste(c(attr(x, "align",exact=TRUE)[tmp.index.start:length(attr(x,"align",exact=TRUE))], "}\n"), - sep="", collapse=""), - sep="") + # Added "width" argument for use with "tabular*" or "tabularx" environments - CR, 7/2/12 + if (is.null(width)){ + WIDTH <-"" + } else if (is.element(tabular.environment, c("tabular", "longtable"))){ + 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.") + WIDTH <- "" + } else { + WIDTH <- paste("{", width, "}", sep="") + } + + BTABULAR <- paste("\\begin{",tabular.environment,"}", WIDTH, "{", + paste(c(attr(x, "align",exact=TRUE)[tmp.index.start:length(attr(x,"align", + exact=TRUE))], "}\n"), sep="", collapse=""), + sep="") ## fix 10-26-09 (robert.castelo@upf.edu) the following 'if' condition is added here to support ## a caption on the top of a longtable diff --git a/pkg/R/table.attributes.R b/pkg/R/table.attributes.R index ca09e98..b228f68 100644 --- a/pkg/R/table.attributes.R +++ b/pkg/R/table.attributes.R @@ -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") diff --git a/pkg/inst/doc/xtableGallery.snw b/pkg/inst/doc/xtableGallery.snw index c69c98e..6f52a05 100644 --- a/pkg/inst/doc/xtableGallery.snw +++ b/pkg/inst/doc/xtableGallery.snw @@ -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|" +@ + +<>= +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 diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd index 1f7271e..26dfe98 100644 --- a/pkg/man/print.xtable.Rd +++ b/pkg/man/print.xtable.Rd @@ -33,6 +33,7 @@ rotate.colnames=getOption("xtable.rotate.colnames", FALSE), booktabs = getOption("xtable.booktabs", FALSE), scalebox = getOption("xtable.scalebox", NULL), + width = getOption("xtable.width", NULL), ...)} \arguments{ \item{x}{An object of class \code{"xtable"}.} @@ -72,6 +73,7 @@ \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{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.} + \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.} \item{...}{Additional arguments. (Currently ignored.)} } \details{ -- 2.39.2