1

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}|")))
        @
CL.
  • 14,577
  • 5
  • 46
  • 73
kamila
  • 122
  • 1
  • 2
  • 13
  • Please have a look at [this answer](http://tex.stackexchange.com/a/5018/37118). Does it answer your question? – CL. Oct 19 '15 at 11:40
  • Thank you. I have seen this one before I asked here. I think I would be able to use if I would like to write the table directly into the .Rnw file. I have no idea how to use it with data imported from file by a code chunk... – kamila Oct 20 '15 at 12:32

2 Answers2

6

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

Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73
0

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}|")))
Seyma Kalay
  • 2,037
  • 10
  • 22