3

I am using knitr and xtables to automate a report procedure. I want to include a few tables in this report and make them displayed with equal width. The Rnw code is listed below. Anyone could tell me how to make the tables of different column numbers showed of the same width in page?

thanks!

\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}
4

Try using the tabularx package, see tabularx documentation for how to use align to further refine your formatting

\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}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy