Quarto PDF目录字体自定义问题:保留默认与样式调整诉求
Quarto PDF目录字体自定义方案
问题1:让目录保持默认Computer Modern字体
你全局设置sansfont: Lato后,部分目录元素会继承无衬线字体。要让目录单独使用默认的Computer Modern,可通过LaTeX前置代码覆盖设置:
- 在项目根目录创建
preamble.tex文件,写入以下内容:
% 将目录字体重置为默认罗马字体(Computer Modern) \addtocontents{toc}{\protect\renewcommand{\protect\familydefault}{\rmdefault}} % 确保目录之后的文档字体仍使用Lato无衬线 \AtEndDocument{\addtocontents{toc}{\protect\renewcommand{\protect\familydefault}{\sfdefault}}}
- 在
_quarto.yml的PDF配置中引入该前置文件:
format: pdf: documentclass: scrbook pdf-engine: lualatex toc: true sansfont: "Lato" include-in-header: preamble.tex
这样目录会保留默认的Computer Modern字体,文档正文的无衬线字体不受影响。
问题2:自定义目录字体(调大章节字号/统一无衬线)
方案A:调大目录内章节标题字号
针对scrbook文档类,通过preamble.tex重新定义目录章节的格式:
\makeatletter \renewcommand*\l@chapter[2]{% \ifnum \c@tocdepth >\m@ne \addpenalty{-\@highpenalty}% \vskip 1.0em \@plus\p@ \setlength\@tempdima{1.5em}% \begingroup \parindent \z@ \rightskip \@pnumwidth \parfillskip -\@pnumwidth \leavevmode \bfseries % 将字号从默认\normalsize改为\large,可按需调整为\Large等 \large \advance\leftskip\@tempdima \hskip -\leftskip #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par \penalty\@highpenalty \endgroup \fi } \makeatother
方案B:统一目录内章节与小节为无衬线字体
若要让目录所有内容使用Lato无衬线字体,在preamble.tex中添加:
% 目录全局使用无衬线字体 \addtocontents{toc}{\protect\renewcommand{\protect\familydefault}{\sfdefault}}
如果需要统一章节和小节的字号,可在上述代码基础上,再添加小节格式的定义:
\makeatletter % 定义章节目录项字号 \renewcommand*\l@chapter[2]{% \ifnum \c@tocdepth >\m@ne \addpenalty{-\@highpenalty}% \vskip 1.0em \@plus\p@ \setlength\@tempdima{1.5em}% \begingroup \parindent \z@ \rightskip \@pnumwidth \parfillskip -\@pnumwidth \leavevmode \bfseries \large \advance\leftskip\@tempdima \hskip -\leftskip #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par \penalty\@highpenalty \endgroup \fi } % 定义小节目录项字号,与章节保持一致 \renewcommand*\l@section[2]{% \ifnum \c@tocdepth >\z@ \addpenalty\@secpenalty \vskip 0.25em \@plus\p@ \setlength\@tempdima{2.5em}% \begingroup \parindent \z@ \rightskip \@pnumwidth \parfillskip -\@pnumwidth \leavevmode \bfseries \large \advance\leftskip\@tempdima \hskip -\leftskip #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par \penalty\@highpenalty \endgroup \fi } \makeatother
将上述代码写入preamble.tex后,在_quarto.yml中引入该文件即可生效。
内容的提问来源于stack exchange,提问作者julien.leroux5
相关产品推荐
相关产品推荐

