kamila
kamila

Reputation: 142

How to set both column width and text alignment in align argument of xtable?

I would like to keep the width of columns I set using align argument of xtable and I would like to align all numeric columns to the right, others to the left and the headers to the center.

I found some solutions using tables which are written directly in the rnw file but I want to load my data from file because my table is quite big and can change during creation of the knitr document.

The code (I used the iris dataset in this example instead of my own data):

<<table_symbionts_chunk, results="asis", echo=FALSE>>=
    library(xtable)

        irisX <-print (xtable (iris,
                                     digits=rep(0,6),
                                     align= c("p{0.015\\textwidth}|", 
                                              "p{0.37\\textwidth}|", 
                                              "p{0.12\\textwidth}|", 
                                              "p{0.08\\textwidth}|", 
                                              "p{0.02\\textwidth}|", 
                                              "p{0.35\\textwidth}|")))
        @

Upvotes: 1

Views: 11022

Answers (2)

Seyma Kalay
Seyma Kalay

Reputation: 2859

I was having the same problem, and want to share this works in R console as

library(xtable)

irisShort <- head(iris)
print(xtable(irisShort,
             digits=rep(0,6),
             align=c(
               "p{1cm}|","p{3cm}|",
               "p{2cm}|","p{3cm}|",
               "p{3cm}|","p{3cm}|")))

Upvotes: 0

CL.
CL.

Reputation: 14957

The tricky part of this question refers to LaTeX. Please not that my TeX code is based on these two questions on tex.stackexchange:


One part of the question is easy to answer: How to set a fixed column width but align all numeric columns right and all other columns left?

This is only a matter of correct column types (see the answers linked above). A solution could be:

\documentclass{article}

\usepackage{array}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}
<<table_symbionts_chunk, results="asis", echo=FALSE>>=
library(xtable)

irisShort <- head(iris)
print(xtable(irisShort,
             digits=rep(0,6),
             align=c(
               "p{0.015\\textwidth}|",
               "R{0.37\\textwidth}|",
               "R{0.12\\textwidth}|",
               "R{0.08\\textwidth}|",
               "R{0.02\\textwidth}|",
               "p{0.35\\textwidth}|")))
@
\end{document}

As p{} columns are left justified by default we only need to define one new column type for right justified columns with a fixed width: R.

Note that the column names overlap but this is due to the widths specified in the question.


Centering the column names requires a different justifications for the first row only. This can be achieved using the \multicolumn command. However, as we want to add LaTeX code to the column names, we moreover have to prevent xtable from sanitizing the column names using sanitize.colnames.function = identity:

irisShort2 <- irisShort
colnames(irisShort2) <- paste("\\multicolumn{1}{c|}{", colnames(irisShort2), "}")

print(xtable(irisShort2,
             digits=rep(0,6),
             align=c(
               "p{0.015\\textwidth}|",
               "R{0.37\\textwidth}|",
               "R{0.12\\textwidth}|",
               "R{0.08\\textwidth}|",
               "R{0.02\\textwidth}|",
               "p{0.35\\textwidth}|")),
      sanitize.colnames.function = identity)

paste("\\multicolumn{1}{c|}{", colnames(irisShort2), "}") uses the original column names but encloses them in \multicolumn{1}{c|}{colname} which provides centered column names.

Note that now to column names do not overlap anymore (instead, the table is too wide) because of the changed column type in the first row.


The two code snippets in this answer produce the following output: enter image description here

Upvotes: 6

Related Questions