HOLD_position 参数和相关解决方案不适用于第四级标头
我正在编写一份较大的报告,其中我在 r 中生成数据表和图形,然后使用 rmarkdown 将这些结果编译成 pdf 报告。通常,我不会遇到使表格/数字出现在我想要的地方的问题(HOLD_position 可以解决这个问题),但我发现,由于某种原因,在涉及第四级标题的情况下,正常的技巧不起作用。
附上一个可重现的示例,首先在 R 中制作一个图形和表格:
data("iris")
library(ggplot2)
# plots with numbered titles to help track in document
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 1")
png('mock1.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 2")
png('mock2.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 3")
png('mock3.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
write.csv(iris[1:30,], file = "table.csv")
现在是一个 rmd,它生成的图形/表格相对于第 4 级标题是无序的(#### 第 4 级标题
)
title: "I broke HOLD_position"
author: "cdtip"
date: "3/1/2022"
output: pdf_document
---
# 1st level heading
## 2nd level heading
### 3rd level heading
#### 4th: set 1
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock1}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 1", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
#### 4th: set 2
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock2}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 2", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
#### 4th: set 3
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock3}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 3", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
如果您遵循提供的代码并假设我的问题是可重现的,您应该生成一个 pdf,其中表格和图形放置在神秘的 pdf 浮动神决定放置的位置,而不是按照我指定的顺序。现在,如果您重复相同的 .rmd 部分,但将第 4 级标题更改为第 3 级标题(例如,####
更改为 ###
),则这些项目应该正确放置。
我尝试了针对类似问题提出的一些解决方案:
- 空行放置 如何保留kable 可以格式化表格后面的文本吗?
- 基于乳胶的解决方案 R Markdown,kable_styling(latex_options="HOLD_position") 不起作用< /a>
- 通读 kableExtra 以及其他参考资料,但是 没有看到似乎可以解决这个具体问题的东西。
对于当前项目,只使用第三级标题就可以了,但我不喜欢这反过来影响目录,因为我现在有几个附加名称来标记以前在不同第三级类别下的差异。为了使用第四级标题可以修复这个问题吗?
I am working on a somewhat large report, where I produce data tables and figures in r and then use rmarkdown to compile those results into a pdf report. Normally I do not have issues making tables/figures appear where I want them (HOLD_position does the trick), but I discovered that the normal trick does not work for some reason where a 4th level header is concerned.
Attaching a reproducible example, first making a figure and tables in R:
data("iris")
library(ggplot2)
# plots with numbered titles to help track in document
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 1")
png('mock1.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 2")
png('mock2.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
p = ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_bw() +
geom_smooth() + ggtitle("Plot 3")
png('mock3.png', res = 600, width = 3, height = 4, units = 'in')
print(p)
dev.off()
write.csv(iris[1:30,], file = "table.csv")
Now an rmd which produces figures/tables that are out of order with respect to the 4th level headers (#### 4th level header
)
title: "I broke HOLD_position"
author: "cdtip"
date: "3/1/2022"
output: pdf_document
---
# 1st level heading
## 2nd level heading
### 3rd level heading
#### 4th: set 1
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock1}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 1", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
#### 4th: set 2
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock2}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 2", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
#### 4th: set 3
```{=latex}
\begin{figure}[H]
\centering
\includegraphics[width=14 cm]{mock3}
\caption{Caption for mock fig.}
\end{figure}
```
```{r echo=FALSE, results='asis'}
suppressWarnings(library(kableExtra, quietly = T))
tab = read.csv(file = "table.csv", header = T, check.names = F)
kbl(tab, caption = "Table 3", booktabs = T) %>%
kable_styling(latex_options = c("HOLD_position"))
```
If you follow the code provided and assuming my issue is reproducible, you should produce a pdf where the tables and figures are placed whereever the mystical pdf float gods decided to place them, and NOT in the order I specified. Now, if you repeat that same .rmd portion, but change the 4th level to 3rd level headers (e.g., ####
to ###
), the items should be placed correctly.
I've tried a few solutions proposed for similar issues:
- Empty line placement
How to I keep kable from formatting text that follows the table? - Latex-based solution
R Markdown, kable_styling(latex_options="HOLD_position") not working - Read through kableExtra and other reference materials as well, but
not seeing something that seems to address this specific issues.
For the immediate project it is fine to just use 3rd level headings, but I don't like how this in turn affects the ToC as I now have several appended names to mark the differences in what previously would have been under different 3rd level categories. Can this be fixed in order to use 4th level headers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论