'Make centered LaTeX tabular table using knitr kable
The knitr::kable with the latex format option produces a tabular environment.
For example,
# produces a tabular environment
knitr::kable(head(cars),
format = 'latex')
produces
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
The table is not centered. If I use kable_stylying from kableExtra, whose value for the option position defaults to center, I get a centered floating table, using the LaTeX table enviornment.
For example, this
# produces a tabular environment inside a table
knitr::kable(head(cars),
format = 'latex') %>%
kableExtra::kable_styling()
produces
\begin{table}
\centering
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
\end{table}
However, I just want to make a small, almost inline, table. I do not want it to float. I do not want captions, etc. What I want then is something to produce LaTeX code like this:
\begin{center}
\begin{tabular}{r|r}
\hline
speed & dist\\
\hline
4 & 2\\
\hline
4 & 10\\
\hline
7 & 4\\
\hline
7 & 22\\
\hline
8 & 16\\
\hline
9 & 10\\
\hline
\end{tabular}
\end{center}
Is that possible using knitr and kableExtra?
I can use a workaround. For example, use chunk options results='asis', and in the chunk, do
cat("\\begin{center}",sep='\n')
knitr::kable(head(cars),
format = 'latex')
cat("\\end{center}",sep='\n')
However, I would like to know if this it is possible without the workaround and whether I am missing something.
Minimal reproducible example
The following is a minimal working example of an RMarkdown document at produces the three kinds of tables mentioned above. Dependencies are magrittr, knitr, and kableExtra.
---
output: pdf_document
---
```{r}
library(magrittr)
```
This produces a `tabular`, non-floating, table. But it is not centered.
```{r}
knitr::kable(head(cars), format = 'latex')
```
The following code produces a centered table, but using a `table` environment, so it floats (in this case to the top of the page), which is probably what we usually want, but we don't *always* want.
```{r}
knitr::kable(head(PlantGrowth), format = 'latex') %>%
kableExtra::kable_styling()
```
We can produce a centered `tabular` environment using chunk option `results='asis'` etc.
```{r, echo=FALSE, results='asis'}
cat("\\begin{center}",sep='\n')
knitr::kable(head(ToothGrowth),
format = 'latex')
cat("\\end{center}",sep='\n')
```
Rendering this (as a pdf_document) creates a single page pdf with this content:

Solution 1:[1]
If you use \centering, you'll avoid the extra vertical spacing from the center environment:
---
output:
pdf_document:
keep_tex: true
header-includes:
- \AddToHook{env/tabular/before}{\begingroup\centering}
- \AddToHook{env/tabular/after}{\par\endgroup}
---
```{r}
library(magrittr)
```
text
```{r}
knitr::kable(head(cars), format = 'latex')
```
text
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

