rmarkdown 设置 kable 的位置 [英] Rmarkdown setting the position of kable
问题描述
我有以下问题,一旦将 Rstudio 中的 Rmarkdown 编织为 PDF,我的表格就不会出现在它们在 Rmarkdown 文件中的位置,而是出现在页面顶部.我尝试添加:
I have the following issue, once knitting the Rmarkdown in Rstudio to PDF, my tables appear not in the position where they are in Rmarkdown file, but on the top of the page. I tried to add:
header-includes:
- \usepackage{float}
和
```{r setup, include=FALSE}
knitr::opts_chunk$set(... fig.pos = "H")
```
但是没有用.R 和 Rstudio 在 Linux 上运行,LaTeX 引擎是pdflatex"
But it didn't work. R and Rstudio run on Linux, the LaTeX engine is "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")
```
推荐答案
您可以将 claudius answer 中的hold_position"替换为HOLD_position":
You can replace "hold_position" from claudius answer with "HOLD_position":
```{r}
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
kable_styling(latex_options = "HOLD_position")
```
如 kableExtra 包中所述:
As mentionned in the kableExtra package:
如果您发现hold_position 的功能不足以将您的桌子固定在确切位置,您可能需要使用HOLD_position,这是此功能的更强大版本.对于熟悉 TeX 的人,hold_position 使用 [!h],HOLD_position 使用 [H] 和 float 包.
if you find hold_position is not powerful enough to literally PIN your table in the exact position, you may want to use HOLD_position, which is a more powerful version of this feature. For those who are familiar with TeX, hold_position uses[!h] and HOLD_position uses [H] and the float package.
参考:https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
您可能还想通过将 fig.pos='H' 添加到图形块标题来控制图形位置.
You may also want to control figure position by adding fig.pos='H' to the figure chunk header.
这篇关于rmarkdown 设置 kable 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!