Set column alignment and width for the generated longtable

Please consider the following MWE

library(xtable) DF <- as.data.frame(UCBAdmissions) print(xtable(DF, align="p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"), sanitize.text.function = function(x){x}, table.placement="!htp", include.rownames=FALSE, tabular.environment='longtable',floating=FALSE) 

I want to set the alignment of my longtable as

 \begin{longtable}{p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}} 

However, when I try to pass an argument to an xtable object, I get

 Warning message: In .alignStringToVector(value) : Nonstandard alignments in align string Error in print(xtable(DF, align = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}"), : error in evaluating the argument 'x' in selecting a method for function 'print': Error in `align<-.xtable`(`*tmp*`, value = "p{0.4\textwidth}|p{0.15\textwidth} p{0.15\textwidth} p{0.15\textwidth}") : "align" must have length equal to 5 ( ncol(x) + 1 ) 

I understand that I have to add alignment for the 5th column (how?), But also I do not understand the error message. Should I sanitize a string?

+7
r tex xtable
source share
1 answer

I cannot verify this, but I think you need to apply standard R-escaping to the backslash in the string, remove extraneous "\" and add the missing "pipe columns" ( | ). Then align<- succeeds only with a warning:

 xtb <- xtable(DF, table.placement="!htp", include.rownames=FALSE, tabular.environment='longtable',floating=FALSE) align(xtb) <- "p{0.4\\textwidth}|p{0.15\\textwidth}|p{0.15\\textwidth}| p{0.15\\textwidth}" #Warning message: #In .alignStringToVector(value) : Nonstandard alignments in align string print(xtb) 

Or:

 xtb <- xtable(DF, type="latex", table.placement="!htp", include.rownames=FALSE, tabular.environment='longtable',floating=FALSE, align= c("p{0.15\\textwidth}", "p{0.4\\textwidth}", "p{0.15\\textwidth}|", "p{0.15\\textwidth}", "p{0.15\\textwidth}" )) 
+11
source share

All Articles