我正在尝试创建一个带有数字列的表格number
,我希望中心和向右对齐内容如下:
使用 RMarkdown,我只能使用如下LaTeX
方法在 R 中将代码包装/内联到单元格内容中paste0()
,并且无法修改LaTeX tabular
环境:
IEtable$number <- paste0("\\begin{centering}", table$number, "\\end{centering})
因此解决这个问题的可能方法是使用eqmakebox
或multicol
或varwidth
。但我对这些命令的表面理解并不能实现我想要的结果。
下表LaTeX
为可重现的示例:
\begin{tabu} to \linewidth {>{\centering}X>{\raggedleft}X}
\hline
\multicolumn{1}{c}{name} & \multicolumn{1}{c}{number}\\
\hline
a & 10\\ % please only make
\hline
bb & 193048\\ % edits in these
\hline
ccc & 200\\ % lines so I can reproduce in R
\hline
\end{tabu}
SO发布包含更多 R 元素
答案1
---
output:
pdf_document:
keep_tex: yes
header-includes:
- \usepackage{booktabs}
---
\tabcolsep3em
```{r,echo=F}
library(knitr)
library(kableExtra)
df <- data.frame(name=c("a","bb","ccc"), number=c(10,193048,200))
kable(df,"latex", align="lr", booktabs = T, linesep = "") %>%
kable_styling() %>%
column_spec(1, width = "3em") %>%
column_spec(2, width = "3em")
```
```{r,echo=F,results='asis'}
library(xtable)
options(xtable.comment = F)
print(xtable(df,digits=0,align="llr"), include.rownames=F,booktabs=T)
```