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

R Markdown用tinytex/lualatex时URW Grotesq字体未自动安装问题

TinyTeX/LuaLaTeX 环境配置URW Grotesq粗体字体方案

字体安装操作

TinyTeX默认不会自动安装非全局依赖的CTAN字体包,手动执行安装即可:

  • 打开R控制台,运行命令安装字体包:tinytex::tlmgr_install("urw-grotesq")
  • 安装完成后运行tinytex:::rebuild_map()刷新LaTeX字体映射缓存,避免编译时找不到字体

原MWE的问题修正

原代码存在两个会导致字体调用失败的问题:

  1. 注意:LuaLaTeX搭配fontspec使用时,不需要加载\usepackage[T1]{fontenc},该包是适配pdfLaTeX的编码包,和fontspec同时加载会破坏Unicode字体映射规则,导致字体找不到、乱码
  2. 传统\fontfamily{ugq}的NFSS调用方式在fontspec环境下无法正确识别Type1格式的URW字体,需要提前通过fontspec命令定义专属字体族

可直接运行的修正后代码

将原R Markdown内容替换为以下版本即可实现「正文全局使用默认罗马字体,仅标题部分使用URW Grotesq粗体」的效果:

---
output:
  pdf_document:
    number_sections: true
    latex_engine: lualatex
classoption: a4paper
geometry: left=2cm,right=1cm,top=1.5cm,bottom=1cm
fontsize: 11pt
lang: de
header-includes:
- \usepackage{textpos}
- \usepackage{fontspec}
- \newfontfamily\urwgrot{URWGrotesk-Bold}[NFSSFamily=ugq]
---

\renewcommand{\familydefault}{\rmdefault}

Some Text here in rmdefault...

\setlength{\TPHorizModule}{1mm}
\setlength{\TPVertModule}{1mm}

\begin{textblock}{165}[0,1](30,140)
    \begin{minipage}[b]{120mm}
      \flushleft
        {\urwgrot
        \fontsize{32pt}{32}
        \selectfont
        SOME TITLE HERE \bigskip \\
        2022}           
    \end{minipage}
\end{textblock}

\newpage

Some Text here in rmdefault...

异常排查

如果编译时仍提示找不到字体,按以下步骤处理:

  • 运行tinytex::tlmgr_update()将TinyTeX所有组件更新到最新版本
  • 删除项目目录下自动生成的_cache缓存文件夹、之前编译生成的中间LaTeX文件,重新触发knit渲染
  • 可在系统终端执行kpsewhich ugqb8a.pfb验证字体安装状态,命令返回字体的本地存储路径即代表安装正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 16:36:43