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

将RMarkdown渲染为Beamer演示时出现LaTeX未定义控制序列错误

RMarkdown渲染Beamer演示文稿时出现LaTeX未定义控制序列错误

错误信息

! Undefined control sequence.
l.16 \setbeamertemplate
                       {caption}[numbered] 

所用RMarkdown脚本

---
title: "Habits"
author: "John Doe"
date: "March 22, 2005"
output: beamer_presentation
---

# Goddag

----

已执行操作

  • 多次通过tinytex包安装并卸载TinyTeX
  • 制作PPT或HTML文件无异常,仅Beamer渲染出现上述错误

更新信息

(a) sessionInfo()输出

> sessionInfo() 
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.7

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.2.2  fastmap_1.1.0   cli_3.6.0      
 [4] htmltools_0.5.4 tools_4.2.2     rstudioapi_0.14
 [7] yaml_2.3.6      rmarkdown_2.19  knitr_1.41     
[10] xfun_0.36       digest_0.6.31   pacman_0.5.1   
[13] rlang_1.0.6     evaluate_0.19

(b) RStudio编织时的渲染输出

processing file: oplaeg.Rmd
  |.......................                                               |  33%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-1
  |......................................................................| 100%
  ordinary text without R code


/Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/pandoc +RTS -K512m -RTS oplaeg.knit.md --to beamer --from markdown+autolink_bare_uris+tex_math_single_backslash --output oplaeg.tex --lua-filter /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --highlight-style tango --pdf-engine pdflatex --embed-resources --standalone 
output file: oplaeg.knit.md

! Undefined control sequence.
l.16 \setbeamertemplate
                       {caption}[numbered] 

Error: LaTeX failed to compile oplaeg.tex. See oplaeg.log for more info.
Execution halted 

问题咨询

请问可能是什么原因导致该问题?

可能的原因及解决方法

  • Beamer版本过旧:\setbeamertemplate{caption}[numbered] 是Beamer较新版本才支持的命令,若TinyTeX中Beamer版本老旧,会触发未定义错误。
    解决:在R中运行命令更新所有LaTeX包:

    tinytex::tlmgr_update(all = TRUE)
    
  • Pandoc生成代码与Beamer版本不兼容:当前使用的rmarkdown 2.19生成的Beamer代码可能依赖新特性,但旧版Beamer不支持。
    解决:在RMarkdown的YAML头中添加自定义LaTeX导入,替换为旧版支持的命令:

    1. 修改YAML头:
    output:
      beamer_presentation:
        keep_tex: true
        includes:
          in_header: preamble.tex
    
    1. 在同目录创建preamble.tex,写入:
    \setbeamertemplate{caption}{\insertcaptionnumber. \insertcaption}
    
  • TinyTeX依赖包缺失:多次重装后仍可能遗漏Beamer相关依赖。
    解决:手动安装必要包:

    tinytex::tlmgr_install(c("beamer", "caption"))
    
  • Pandoc版本不匹配:当前使用的是RStudio内置的Quarto Pandoc,版本可能与rmarkdown不兼容。
    解决:更新RStudio至最新版本,或单独更新Pandoc。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:15:29