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

孤人 提交于 2019-12-02 13:33:35

问题


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}|")))
        @

回答1:


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

  • How to create fixed width table columns with text raggedright/centered/raggedleft?
  • Center column with specifying width in table (tabular enviroment)?

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:



来源:https://stackoverflow.com/questions/33208777/how-to-set-both-column-width-and-text-alignment-in-align-argument-of-xtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!