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

如何在Quarto中隐藏正文章节标题,同时保留页眉显示

问题

已通过YAML配置Quarto的PDF输出,让页眉显示在页面右上角,但正文内插入的章节标题(如# Overall Results)会在页面左上角显示,造成冗余。需要隐藏这些正文里的章节标题,但不能删除它们,因为页眉配置依赖这些标题信息。

示例代码:

\newpage

# Overall Results

当前YAML配置:

---
title: |
  ![](myimage.png){width=1in}\linebreak
  Title\linebreak\linebreak
  ![](image.png){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: |
  ![](myimage.png){width=1in}\linebreak
  Title\linebreak\linebreak
  ![](image.png){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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 14:20:31