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.
I want to place the table at the center as below.
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}
The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).
The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.
Centering Images You can use the knitr include_graphics() function along with the fig. align='center' chunk option. This technique has the benefit of working for both HTML and LaTeX output. You can add CSS styles that center the image (note that this technique works only for HTML output).
The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With