Mahbubul Majumder
Mahbubul Majumder

Reputation: 340

centering the table generated by kable function of knitr package

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}

Upvotes: 17

Views: 25460

Answers (1)

Hao
Hao

Reputation: 7836

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

Upvotes: 23

Related Questions