Quarto Typst自定义模板问题:副标题不显示+标题格式未生效
Quarto Typst自定义模板问题修复
问题1:副标题未显示
原因
模板中authors参数仅支持对象数组格式,但你在YAML中传入的是字符串格式作者列表,导致authors.len()和authors.map执行报错,中断渲染流程,进而影响副标题的显示。
修复方案
方案一:兼容字符串格式作者输入
修改模板中authors的处理逻辑,增加字符串格式的兼容分支:
if authors != none { if type(authors) == "string" { align(left)[#authors] } else { let count = authors.len() let ncols = calc.min(count, 3) grid( columns: (1fr,) * ncols, row-gutter: 1.5em, ..authors.map(author => align(left)[ #author.name \ #author.affiliation \ #author.email ] ) ) } }
方案二:使用对象数组格式传入作者
调整YAML中的作者配置为标准对象数组:
authors: - name: "Senyor Autor" affiliation: "你的机构" email: "autor@example.com" - name: "Senyora Autora" affiliation: "你的机构" email: "autora@example.com"
问题2:标题未使用Roboto字体
原因
- 全局文本字体设置
set text(font: font)(默认"Noticia Text")可能干扰局部字体设置; - Roboto字体未正确加载或字体名称指定不准确;
- 标题样式配置代码放在了文档内容渲染之后,导致样式未生效。
修复方案
- 调整代码顺序:将标题样式配置移到文档内容渲染之前:
// 先配置标题样式 set heading(numbering: "1.1.1.") show heading: it => locate(loc => { let levels = counter(heading).at(loc) let deepest = if levels != () { levels.last() } else { 1 } if it.level == 1 [ #set align(left) #set text(18pt, font: "Roboto") #v(24pt, weak: true) #if it.numbering != none { numbering("1.", deepest) h(7pt, weak: true) } #it.body #v(18pt, weak: true) ] else if it.level == 2 [ set align(left) #set text(14pt, font: "Roboto") #v(24pt, weak: true) #if it.numbering != none { numbering("1.", deepest) h(7pt, weak: true) } #it.body #v(12pt, weak: true) ] else if it.level == 3 [ #set align(left) #set text(14pt, font: "Roboto") #if it.numbering != none { numbering("1.", deepest) h(7pt, weak: true) } #it.body #v(10pt, weak: true) ] else [ _#(it.body):_ ] }) // 再渲染文档内容 if cols == 1 { doc } else { columns(cols, doc) }
确保Roboto字体可用:
- 确认系统已安装Roboto字体;
- 若未安装,可在模板开头添加字体加载(假设字体文件在模板目录):
font.add("Roboto", "Roboto-Regular.ttf") font.add("Roboto Bold", "Roboto-Bold.ttf")明确标题字体指定:
修改主标题的字体调用,使用更精准的字体名称:if title != none { align(left)[#block()[ #text(font: "Roboto Bold", weight: "bold", size: 33pt)[#title] ]] }
内容的提问来源于stack exchange,提问作者marcbosch
相关产品推荐
相关产品推荐

