简体   繁体   中英

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: 在此处输入图像描述

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

在此处输入图像描述

You just need to set the table.envir argument to "center" to wrap the \tabular in \begin{center}... \end{center} . In your MRE:

---
output: pdf_document
---

```{r}
library(magrittr)
```

This produces a `tabular`, non-floating, table, and it *is* centered:
```{r}
knitr::kable(head(cars), format = 'latex', table.envir = "center")
```

If you want to wrap it twice (eg centered and boldface), you can use something like table.envir = c("bf", "center", "bf") : because kable() isn't smart enough to reverse the order when adding the \end{} markers, you need to use a palindrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM