You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LaTeX编译PDF失败:wraptable语法错误求助

问题描述
  • 编译报错:LaTeX编译时提示 ! Extra }, or forgotten \endgroup.,错误定位在 \end{wraptable} 行
  • 伴随警告:Overfull \hbox (165.80571pt too wide),说明表格宽度超出排版区域
  • 复现场景:使用kableExtra的kable_styling(position = "float_right")生成右浮动表格时触发,已尝试更新/重装RStudio、TinyTeX,切换LaTeX引擎,更换设备均无效
复现代码
---
title: "Chapter 5"
author: "Moiken Hinrichs"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
header-includes:
   - \usepackage{caption}
   - \usepackage{float}
   - \captionsetup[figure]{font=footnotesize}
   - \usepackage{subfig}
   - \usepackage{flafter}
   - \usepackage{footmisc}
output: 
  bookdown::pdf_document2:
     extra_dependencies: ["flafter"]
     number_sections: true
     toc: false
latex_engine: xelatex
classoption: twoside
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(kableExtra)
library(dplyr, quietly=T)
library(tidyr)
column1 <- c("Bert", "Angela", "Nico", "Pam", "Max")
column2 <- c(33, 54, 85, 96, 57)
prox <- data.frame(column1, column2)
prox[nrow(prox)+1, ] <- c('Total', sum(prox$column2))

kable(prox, booktabs = T, linesep = "",
      col.names = c("Name", "Count"),
      caption = "Number of items") %>%
  kable_styling(latex_options = "striped", position = "float_right") %>%
  row_spec(5, hline_after = T) %>%
  row_spec(6, bold = T)
# 解决方案
## 1. 修正表格宽度溢出(核心解决方法)
`Overfull \hbox`是触发嵌套语法错误的直接原因——表格宽度超过`wraptable`分配的宽度,导致LaTeX解析混乱。给表格设置固定列宽:
```r
kable(prox, booktabs = T, linesep = "",
      col.names = c("Name", "Count"),
      caption = "Number of items") %>%
  kable_styling(latex_options = "striped", position = "float_right", width = "0.4\\linewidth") %>% # 设置表格总宽度为行宽的40%
  column_spec(1, width = "0.25\\linewidth") %>% # 指定第一列宽度
  column_spec(2, width = "0.15\\linewidth") %>% # 指定第二列宽度
  row_spec(5, hline_after = T) %>%
  row_spec(6, bold = T)

2. 清理冲突的LaTeX包

  • YAML中header-includes和output的extra_dependencies重复加载了flafter,删除其中一处
  • footmisc包与wrapfig(wraptable依赖的包)存在兼容性问题,若无需该包可直接移除;若必须使用,添加参数指定脚注位置避免冲突

修改后的YAML示例:

---
title: "Chapter 5"
author: "Moiken Hinrichs"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
header-includes:
   - \usepackage{caption}
   - \usepackage{float}
   - \captionsetup[figure]{font=footnotesize}
   - \usepackage{subfig}
   - \usepackage{flafter}
   - \usepackage[bottom]{footmisc} # 指定脚注位置避免冲突
output: 
  bookdown::pdf_document2:
     number_sections: true
     toc: false
latex_engine: xelatex
classoption: twoside
keep_tex: true
---

3. 替代右浮动方案(若上述方法无效)

放弃kable_styling(position = "float_right"),手动用LaTeX的table环境实现右浮动:

cat("\\begin{table}[!r]
\\centering")
kable(prox, booktabs = T, linesep = "",
      col.names = c("Name", "Count"),
      caption = "Number of items") %>%
  kable_styling(latex_options = "striped") %>%
  row_spec(5, hline_after = T) %>%
  row_spec(6, bold = T)
cat("\\end{table}")

内容的提问来源于stack exchange,提问作者Moiken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:10:14