我一直在尝试用Quarto
它来代替RMarkdown
撰写报告,但是在表格方面遇到了一些问题。
主要问题似乎是,如果我尝试同时使用表格标题以及一些可用的工具来kableExtra
创建美观的LaTeX
表格,则无法进行编译,请参阅下面的代码:
---
title: "REPREX - QMD KABLE CAPTION COMPILATION PROBLEM"
format:
pdf:
documentclass: article
code-line-numbers: true
latex-output-dir: ../output
keep-tex: true
---
```{r}
#| label: setup
#| include: false
library(tidyverse)
library(kableExtra)
tbl <- tribble(
~"pos_neg", ~"<8", ~"8-10", ~">10",
"Negative", 95, 23, 45,
"Positive", 49, 22, 25,
)
```
```{r}
#| tbl-cap: Test
#| label: tbl-test
tbl %>%
janitor::adorn_totals(where = c("row", "col")) %>%
kable(
longtable = TRUE,
booktabs = TRUE,
linesep = "",
align = c("l", "c", "c", "c", "c"),
col.names = c("", names(.)[-1]),
) %>%
add_header_above(c(" " = 1, "Inoculum volume (mL)" = 3, " " = 1), bold = TRUE) %>%
row_spec(0, bold = TRUE) %>%
column_spec(1, bold = TRUE) %>%
column_spec(2:4, width = "3em")
```
我收到此错误:
编译失败 - 错误包数组错误:使用了非法的前导标记(\caption):`c'。
请参阅 array 包文档以了解解释。键入 H 可立即获得帮助。...
l.238 ...{3em}>{\centering\arraybackslash}p{3em}c}
这是为表格输出的 tex 代码:
\begin{longtable}{>{}
\caption{\label{tbl-test}Test }\tabularnewline
l>{\centering\arraybackslash}p{3em}>{\centering\arraybackslash}p{3em}>{\centering\arraybackslash}p{3em}c}
\toprule
\multicolumn{1}{c}{\textbf{ }} & \multicolumn{3}{c}{\textbf{Inoculum volume (mL)}} & \multicolumn{1}{c}{\textbf{ }} \\
\cmidrule(l{3pt}r{3pt}){2-4}
\textbf{} & \textbf{<8} & \textbf{8-10} & \textbf{>10} & \textbf{Total}\\
\midrule
\textbf{Negative} & 95 & 23 & 45 & 163\\
\textbf{Positive} & 49 & 22 & 25 & 96\\
\textbf{Total} & 144 & 45 & 70 & 259\\
\bottomrule
\end{longtable}
如果我删除#| tbl-cap: Test
块选项,它可以编译,但显然没有表格标题。
如果有人能帮忙我将不胜感激。
谢谢
[✓] 检查 Quarto 安装......OK 版本:1.2.475 路径:/Applications/quarto/bin
[✓] 检查基本 Markdown 渲染....确定
[✓] 检查 R 安装...........OK 版本:4.2.2 路径:/usr/local/Cellar/r/4.2.2/lib/R LibPaths:- /usr/local/Cellar/r/4.2.2/lib/R/library rmarkdown:2.21
[✓] 检查 Knitr 引擎渲染......OK
Darwin mac-mini.lan 22.4.0 Darwin 内核版本 22.4.0:2023 年 3 月 6 日星期一 21:00:17 PST;root:xnu-8796.101.5~3/RELEASE_X86_64 x86_64
tlmgr 修订版 66798 (2023-04-08 02:15:21 +0200) tlmgr 使用安装:/usr/local/texlive/2023 TeX Live (https://tug.org/texlive) 版本 2023
TeX 3.141592653 (TeX Live 2023) kpathsea 版本 6.3.5
knitr_1.42 rmarkdown_2.21 janitor_2.2.0 kableExtra_1.3.4 R 版本 4.2.2(2022-10-31)
答案1
没有答案,但我无法在评论中很好地解释:
由于某种原因,您的问题无法重现。是不是有些东西过时了?您正在制作一些未显示的内容?(始终显示可“按原样”编译的最小工作示例,而不是包含没有结束标记的 R 块等代码片段)。
重建您的文件,或类似的东西(?)带有标题的表格的标题在我的计算机中是正确的:
\begin{longtable}{>{}l>{\centering\arraybackslash}p{3em}>{\centering\arraybackslash}p{3em}>{\centering\arraybackslash}p{3em}c}
\caption{\label{tbl-test}Test }\tabularnewline
\toprule
...
表格呈现正确:
用于此输出的整个 Quarto:
---
title: "REPREX - QMD KABLE CAPTION COMPILATION PROBLEM"
format:
pdf:
documentclass: article
code-line-numbers: true
latex-output-dir: ../output
keep-tex: true
---
```{r}
#| label: setup
#| include: false
library(tidyverse)
library(kableExtra)
tbl <- tribble(
~"pos_neg", ~"<8", ~"8-10", ~">10",
"Negative", 95, 23, 45,
"Positive", 49, 22, 25,
)
```
```{r}
#| tbl-cap: Test
#| label: tbl-test
tbl %>%
janitor::adorn_totals(where = c("row", "col")) %>%
kable(
longtable = TRUE,
booktabs = TRUE,
linesep = "",
align = c("l", "c", "c", "c", "c"),
col.names = c("", names(.)[-1]),
) %>%
add_header_above(c(" " = 1, "Inoculum volume (mL)" = 3, " " = 1), bold = TRUE) %>%
row_spec(0, bold = TRUE) %>%
column_spec(1, bold = TRUE) %>%
column_spec(2:4, width = "3em")
```