rmarkdown 设置 kable 的位置

Jus*_*kis 13 r r-markdown kable

我有以下问题,一旦将 Rstudio 中的 Rmarkdown 编织为 PDF,我的表格就不会出现在它们在 Rmarkdown 文件中的位置,而是出现在页面的顶部。我尝试添加:

header-includes:
  - \usepackage{float}
Run Code Online (Sandbox Code Playgroud)

```{r setup, include=FALSE}
knitr::opts_chunk$set(... fig.pos = "H")
```
Run Code Online (Sandbox Code Playgroud)

但它没有用。R 和 Rstudio 在 Linux 上运行,LaTeX 引擎是“pdflatex”

完全可重现的示例:

---
title: "Untitled"
output: pdf_document
header-includes:
  - \usepackage{float}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning = FALSE, fig.align = "center", dev = "cairo_pdf", fig.pos = "H")
```

```{r}
library(kableExtra)
library(tidyverse)
```
## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

\newpage

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance")
```
Run Code Online (Sandbox Code Playgroud)

Gae*_*elS 16

您可以用“HOLD_position”替换 claudius 答案中的“hold_position”:

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
  kable_styling(latex_options = "HOLD_position")
```
Run Code Online (Sandbox Code Playgroud)

如 kableExtra 包中所述:

如果您发现hold_position 的功能不足以将您的桌子固定在确切位置,您可能需要使用HOLD_position,这是此功能的更强大版本。对于熟悉 TeX 的人来说,hold_position 使用 [!h],HOLD_position 使用 [H] 和 float 包。

参考:https : //haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

您可能还想通过将 fig.pos='H' 添加到图形块标题来控制图形位置。

  • 是的,同时使用 `fig.pos='H'` 和 `latex_options = "HOLD_position"` 至关重要。多谢! (3认同)

cla*_*ius 8

要修复 Latex 文档中的表格位置,您不需要包含标题选项,也不需要设置 opts_chunk 配置。为此,您应该通过在[ 1 ] 中添加函数来指定latex_optionsas 。hold_positionkable_styling()kable

所以最后一块将是:

```{r}    
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
  kable_styling(latex_options = "hold_position")
```
Run Code Online (Sandbox Code Playgroud)