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

如何在R Markdown中先于unicode-math加载指定LaTeX包?

解决R Markdown Beamer中在unicode-math前加载LaTeX包的问题

要让mathtools这类包在unicode-math之前加载,现有includes:in_header或header_includes方法无效,因为它们的内容会被插入到Pandoc默认加载的包之后。可以用以下两种方法解决:

方法1:利用Pandoc的latex-preamble变量

Pandoc的latex-preamble变量允许在默认包加载代码之前插入自定义LaTeX代码,正好满足需求。在YAML配置中通过pandoc_args传递这个变量:

---
title: "title"
author: "author"
date: "date"
documentclass: beamer
output: 
  beamer_presentation: 
    latex_engine: xelatex
    theme: metropolis
    keep_tex: yes
    pandoc_args: ["--variable", "latex-preamble=\\usepackage{mathtools}"]
---

这样生成的.tex文件中,\usepackage{mathtools}会出现在unicode-math加载代码之前,消除警告。

方法2:自定义Beamer模板

如果需要更复杂的导言区控制,可以自定义LaTeX模板:

  • 首先导出R Markdown使用的默认Beamer模板:
    在R控制台运行:
    rmarkdown::pandoc_template("beamer", output = "custom_beamer.tex")
    
  • 打开导出的custom_beamer.tex,找到加载unicode-math的代码行(通常是\usepackage{unicode-math}),在它前面添加\usepackage{mathtools}。
  • 在YAML头中指定使用自定义模板:
    ---
    title: "title"
    author: "author"
    date: "date"
    

documentclass: beamer
output:
beamer_presentation:
latex_engine: xelatex
theme: metropolis
keep_tex: yes
template: custom_beamer.tex

验证:生成.tex文件后,检查`mathtools`的加载语句是否在`unicode-math`之前,此时`mathtools`的加载警告应不再出现。

内容的提问来源于stack exchange,提问作者Syu Gau
相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 03:42:19