如何在Quarto中隐藏正文章节标题,同时保留页眉显示
问题
已通过YAML配置Quarto的PDF输出,让页眉显示在页面右上角,但正文内插入的章节标题(如# Overall Results)会在页面左上角显示,造成冗余。需要隐藏这些正文里的章节标题,但不能删除它们,因为页眉配置依赖这些标题信息。
示例代码:
\newpage # Overall Results
当前YAML配置:
--- title: | {width=1in}\linebreak Title\linebreak\linebreak {width=4in}\linebreak\linebreak `r Company`\linebreak subtitle: | Quarter Year format: pdf: documentclass: scrartcl latex_engine: xelatex fig_caption: yes fig_crop: no fig_width: 6 fig_height: 4 number_sections: no toc: false latex_engine: xelatex header-includes: - "\\usepackage{fancyhdr}" - "\\pagestyle{fancy}" - "\\usepackage{fontspec}" - "\\newfontfamily\\tfont{Montserrat}" - "\\setkomafont{pageheadfoot}{\\tfont}" - "\\addtokomafont{disposition}{\\tfont}" - "\\usepackage[left=1in, top=1in, right=1in, bottom=1.0in, headheight=2cm, headsep=0.5cm]{geometry}" - "\\fancyheadoffset[R]{1cm}" - "\\fancyfoot[L]{\\includegraphics[width=0.5in]{my_image.png}}" - "\\fancyfoot[R]{Company}" - "\\renewcommand\\headrulewidth{0pt}" fontsize: 14pt mainfont: Myriad Pro ---
解决方案
要实现隐藏正文章节标题但保留其用于页眉的信息,可通过重新定义LaTeX的\section命令来实现。在YAML的header-includes中添加以下配置:
- "\\renewcommand{\\section}[1]{\\markright{#1}\\phantomsection}"
修改后的完整YAML配置如下:
--- title: | {width=1in}\linebreak Title\linebreak\linebreak {width=4in}\linebreak\linebreak `r Company`\linebreak subtitle: | Quarter Year format: pdf: documentclass: scrartcl latex_engine: xelatex fig_caption: yes fig_crop: no fig_width: 6 fig_height: 4 number_sections: no toc: false latex_engine: xelatex header-includes: - "\\usepackage{fancyhdr}" - "\\pagestyle{fancy}" - "\\usepackage{fontspec}" - "\\newfontfamily\\tfont{Montserrat}" - "\\setkomafont{pageheadfoot}{\\tfont}" - "\\addtokomafont{disposition}{\\tfont}" - "\\usepackage[left=1in, top=1in, right=1in, bottom=1.0in, headheight=2cm, headsep=0.5cm]{geometry}" - "\\fancyheadoffset[R]{1cm}" - "\\fancyfoot[L]{\\includegraphics[width=0.5in]{my_image.png}}" - "\\fancyfoot[R]{Company}" - "\\renewcommand\\headrulewidth{0pt}" - "\\renewcommand{\\section}[1]{\\markright{#1}\\phantomsection}" fontsize: 14pt mainfont: Myriad Pro ---
说明
\markright{#1}:将章节标题设置为页眉右侧的内容,匹配当前fancyhdr配置。\phantomsection:确保章节对应的书签位置正确(后续若启用TOC,可保留书签功能)。- 重新定义
\section命令后,Quarto解析# 标题时会执行新命令,不再输出可见的章节标题,但仍会更新页眉信息。
如果需要保留章节在目录中的条目(即使当前toc: false),可将重新定义的命令改为:
- "\\renewcommand{\\section}[1]{\\markright{#1}\\addcontentsline{toc}{section}{#1}\\phantomsection}"
内容的提问来源于stack exchange,提问作者Grey Gergen
相关产品推荐
相关产品推荐

