R语言 并排kables返回“不在外部标准模式”

clj7thdc  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(114)

这可能与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吗?
rryofs0p

rryofs0p1#

是的,您可以在使用并排表时编织到PDF。下面的代码将生成示例数据的并排表。下面是解释和示例.rmd。

```{r working solution, results = "asis"}
library(tidyverse)
library(knitr)
library(kableExtra)

species.table = kbl(format = "latex",  booktabs = TRUE,
  starwars %>%
    count(species) %>%
    filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))

planet.table = kbl(format = "latex", booktabs = TRUE,
  starwars %>%
    count(homeworld) %>%
    filter(n > 1)
)%>% kable_styling(latex_options = c("striped", "Hold_position"))

species.table = substr(species.table, start = 25, stop = nchar(species.table) - 11)
planet.table = substr(planet.table, start = 25, stop = nchar(planet.table) - 11)

# The following code is from a similar SO question: 
# https://stackoverflow.com/a/38037481/15293705

cat(c("\\begin{table}[!htb]
  \\begin{minipage}{.5\\linewidth}
    \\caption{Species Table}
    \\centering",
    species.table,
  "\\end{minipage}%
  \\begin{minipage}{.5\\linewidth}
    \\centering
    \\caption{Planets Table}",
    planet.table,
  "\\end{minipage}
  \\end{table}"
  ))

示例代码出错是因为初始表被 Package 在它们自己的\开始{table}->\end{table}环境中,然后kables进来并试图将它们 Package 在自己的表环境中。Latex看到类似\开始{table}->\begin{table}的内容,并输出错误。
从两个内部表中剥离\开始{table}和\end{table}(以及一个松散的\centering)命令,允许我们使用以前的SO解决方案来输出表。
下面的.rmd文件演示并介绍了如何获得此解决方案。

title: "Solved Example Document"
author: "Carbiner"
date: "r Sys.Date()"
output: pdf_document

library(tidyverse)
library(knitr)
library(kableExtra)

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.

species.table = kbl(format = "latex",  booktabs = TRUE,
  starwars %>%
    count(species) %>%
    filter(n > 1)
) %>% kable_styling(latex_options = c("striped", "Hold_position"))

print(species.table)

Looks fine. The table has a table enviornment, which is centered, and a tabular enviornment.

planet.table = kbl(format = "latex", booktabs = TRUE,
  starwars %>%
    count(homeworld) %>%
    filter(n > 1)
    )%>% kable_styling(latex_options = c("striped", "Hold_position"))

print(planet.table)

Same thing here. All looks good. These tables both output normally.

But what does kables do?


no.good = kables(list(species.table, planet.table))

print(no.good)

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 from kables, 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 as striped) 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.


# 25 characters in is after the initial \begin{table} 
# and \centering commands, which we don't want to include.

# The last 11 characters are the \end{table} command
# which we also don't want to include. 

species.table = substr(species.table, start = 25, stop = nchar(species.table) - 11)
planet.table = substr(planet.table, start = 25, stop = nchar(planet.table) - 11)

# The following code is from a similar SO question: 
# https://stackoverflow.com/a/38037481/15293705

cat(c("\\begin{table}[!htb]
  \\begin{minipage}{.5\\linewidth}
    \\caption{Species Table}
    \\centering",
    species.table,
  "\\end{minipage}%
  \\begin{minipage}{.5\\linewidth}
    \\centering
    \\caption{Planets Table}",
    planet.table,
  "\\end{minipage}
  \\end{table}"
  ))

This should output the tables properly. I haven't tested if all formatting commands still work, but booktabs, striped do.


应该注意的是,这个解决方案依赖于字符串子集,在创建它们时将表格格式设置为“latex”会极大地帮助它。字符串子集也是... hacky,并且完全依赖于未修复的kable和kables的行为。
这对于将latex表转换为PDF非常有用。如果您打算输出为多种格式,或者不想使用latex,则可能必须找到其他解决方法。

相关问题