如何在xtable的align参数中设置列宽和文本对齐方式? [英] How to set both column width and text alignment in align argument of xtable?

查看:118
本文介绍了如何在xtable的align参数中设置列宽和文本对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留使用xtablealign参数设置的列的宽度,并且我想将所有数字列向右对齐,其他数字列向左对齐,并且标题向中心对齐.

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.

我找到了一些使用表的解决方案,这些表直接写在rnw文件中,但是我想从文件中加载数据,因为我的表很大,并且在创建knitr文档时可以更改.

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.

代码(在此示例中,我使用的是iris数据集,而不是我自己的数据):

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

推荐答案

这个问题的棘手部分是LaTeX.请不要因为我的TeX代码基于tex.stackexchange上的以下两个问题:

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}

由于默认情况下p{}列是左对齐的,因此我们只需要为宽度固定的右对齐列定义一种新的列类型:R.

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.

使列名居中仅对第一行要求不同的理由.这可以使用\multicolumn命令来实现.但是,由于要向列名称添加LaTeX代码,因此我们还必须防止xtable使用sanitize.colnames.function = identity消毒列名称:

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), "}")使用原始列名,但将它们括在\multicolumn{1}{c|}{ colname }中,该列名提供居中的列名.

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:

这篇关于如何在xtable的align参数中设置列宽和文本对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆