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

徘徊边缘 提交于 2019-12-02 04:35:06
CL.

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:

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