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

后端 未结 2 1409
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 09:14

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 th

相关标签:
2条回答
  • 2021-01-23 09:25

    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:

    0 讨论(0)
  • 2021-01-23 09:51

    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}|")))
    
    0 讨论(0)
提交回复
热议问题