我正在使用 knitr 和 xtables 来自动化报告程序。我想在此报告中包含一些表格,并使它们以相同的宽度显示。Rnw 代码如下。有人能告诉我如何使不同列号的表格在页面上显示相同的宽度吗?
谢谢!
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{colortbl, xcolor}
\usepackage{longtable}
\begin{document}
<<do_table,echo=FALSE, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
mydf1 <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10),var3=rnorm(10))
print(xtable(mydf))
print(xtable(mydf1))
@
\end{document}
答案1
尝试使用 tabularx 包,参见 tabularx文档如何使用对齐进一步优化格式
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{colortbl, xcolor}
\usepackage{tabularx}
\begin{document}
<<do_table,echo=FALSE, results = "asis">>=
library(xtable)
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10))
mydf1 <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10),var3=rnorm(10))
print(xtable(mydf,align="XXXX"), tabular.environment = 'tabularx', width="3in")
print(xtable(mydf1,align="XXXXX"), tabular.environment = 'tabularx', width="3in")
@
\end{document}