这可能与latex kable side-by-side tables "Not in outer par mode"有关,但作者确定的解决方案,使用latex_options = c("Hold_position")
并没有改变我在尝试编织PDF时的结果。作者的评论全文如下:
我通过添加kable_styling(latex_options = c("striped", "Hold_position"))
使表并排显示。
下面是一个运行良好的最小示例:
---
title: "Example Document"
date: "Last compiled on `r format(Sys.time(), '%d %B, %Y at %H:%M ')`"
output: pdf_document
---
```{r setup, include=FALSE, echo=FALSE}
library(tidyverse)
library(kableExtra)
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
)%>% kable_styling(latex_options = c("striped", "Hold_position"))
当我使用`knitr::kables`将它们组合成一对并排的表时,我可以运行代码块并很好地编织到HTML中,但我仍然得到一个“not in outer par mode”错误:
! LaTeX Error: Not in outer par mode.
Error: LaTeX failed to compile 10_knit_doesnt.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See 10_knit_doesnt.log for more info.
Execution halted
实际的代码块是这样的:
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling()
)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
我尝试将样式(`kable_styling(latex_options = c("striped", "Hold_position"))`)移动到各个表中,但这似乎没有任何影响。
knitr::kables(list(
kable(caption = "Species",
starwars %>%
count(species) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position")),
kable(caption = "Homeworld",
starwars %>%
count(homeworld) %>%
filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))
)
) %>% kable_styling()
我可以在使用并排表时编织到PDF吗?
1条答案
按热度按时间rryofs0p1#
是的,您可以在使用并排表时编织到PDF。下面的代码将生成示例数据的并排表。下面是解释和示例.rmd。
title: "Solved Example Document"
author: "Carbiner"
date: "
r Sys.Date()
"output: pdf_document
Let's take a look at what our tables actually look like, under the hood. Note that I've changed the format to "latex", because the solution to this problem works best when it's directly specified.
Looks fine. The table has a table enviornment, which is centered, and a tabular enviornment.
Same thing here. All looks good. These tables both output normally.
But what does
kables
do?kables
puts the both planets and species into an overarching table environment. The double\begin{table} \begin{table}
is what seems to kill latex. The first is coming fromkables
, the second comes from the species table.kables
works for latex output when the original table doesn't wrap everything in a table environment. Wrapping things in a table environment seems to happen when certain formatting options (such asstriped
) are used.A solution is to strip out the commands that create the inner table environments. Because these are internally character vectors, we can use substring to slice out the commands we don't want.
Doing this stops kables from working properly (not that it was), so we're going to borrow some other SO code to make them output.
This should output the tables properly. I haven't tested if all formatting commands still work, but
booktabs
,striped
do.