12
votes

I am using the kable() function of knitr package to produce a nice book quality table in a pdf document. The output is as below where the table is placed on the left. enter image description here

I want to place the table at the center as below. enter image description here

I would appreciate if anyone can give some advice. I know I can do it using xtable. Is there any way I can do it directly using kable?

The complete reproducible knitr code (.Rnw file) is as below;

\documentclass{article}
\usepackage{booktabs}

\begin{document}

<<results='asis'>>=
library(knitr)
kable(head(women), format='latex', booktabs=TRUE)
@

\end{document}
1
You can provide a custom LaTeX preamble to center all your tables like suggested at tex.stackexchange.com/a/6036/2878daroczig
@daroczig That suggestion did not work for me.Mahbubul Majumder
Mahbubul, depending on what version of pandoc you (RStudio) are using, you might want to update the ctable or longtable environment instead of tabulardaroczig

1 Answers

16
votes

With kableExtra, you can set the alignment by using:

kable(head(women), "latex", booktabs = T) %>%
  kable_styling(position = "center")

http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf