Pandoc生成PDF无法显示5级以上标题的问题求助
解决Pandoc生成PDF时目录无法显示5级以上标题的问题
问题原因
Pandoc默认将Markdown标题映射到LaTeX的标准标题层级:
#→\section##→\subsection- ...
######→\subparagraph
LaTeX标准仅支持5级标题(从\section到\subparagraph),超过6级的Markdown标题会被解析为普通文本,自然不会出现在目录中。此外,即使设置了toc-depth=10,LaTeX本身没有对应层级的定义,参数也无法生效。
解决方案
要显示10级标题,需要自定义LaTeX标题层级,将Markdown深层标题映射到这些自定义层级,同时配置目录深度。
步骤1:在Markdown中添加LaTeX自定义标题配置
在example.md的YAML前置块中添加以下代码,定义6到10级的标题样式、编号规则,并设置目录和编号深度:
--- title: Test date: 2024/07/29 version: v0.0.1 header-includes: - \usepackage{titlesec} - \usepackage{tocloft} # 定义第6级标题(对应原Markdown的#######) - \titleclass{\subsubparagraph}{straight}[\paragraph] - \newcounter{subsubparagraph}[paragraph] - \renewcommand{\thesubsubparagraph}{\theparagraph.\arabic{subsubparagraph}} - \titleformat{\subsubparagraph}{\normalfont\normalsize\bfseries}{\thesubsubparagraph}{1em}{} - \titlespacing*{\subsubparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} # 定义第7级标题(对应原Markdown的########) - \titleclass{\subsubsubparagraph}{straight}[\subsubparagraph] - \newcounter{subsubsubparagraph}[subsubparagraph] - \renewcommand{\thesubsubsubparagraph}{\thesubsubparagraph.\arabic{subsubsubparagraph}} - \titleformat{\subsubsubparagraph}{\normalfont\normalsize\bfseries}{\thesubsubsubparagraph}{1em}{} - \titlespacing*{\subsubsubparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} # 定义第8级标题 - \titleclass{\subsubsubsubparagraph}{straight}[\subsubsubparagraph] - \newcounter{subsubsubsubparagraph}[subsubsubparagraph] - \renewcommand{\thesubsubsubsubparagraph}{\thesubsubsubparagraph.\arabic{subsubsubsubparagraph}} - \titleformat{\subsubsubsubparagraph}{\normalfont\normalsize\bfseries}{\thesubsubsubsubparagraph}{1em}{} - \titlespacing*{\subsubsubsubparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} # 定义第9级标题 - \titleclass{\subsubsubsubsubparagraph}{straight}[\subsubsubsubparagraph] - \newcounter{subsubsubsubsubparagraph}[subsubsubsubparagraph] - \renewcommand{\thesubsubsubsubsubparagraph}{\thesubsubsubsubparagraph.\arabic{subsubsubsubsubparagraph}} - \titleformat{\subsubsubsubsubparagraph}{\normalfont\normalsize\bfseries}{\thesubsubsubsubsubparagraph}{1em}{} - \titlespacing*{\subsubsubsubsubparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} # 定义第10级标题 - \titleclass{\subsubsubsubsubsubparagraph}{straight}[\subsubsubsubsubparagraph] - \newcounter{subsubsubsubsubsubparagraph}[subsubsubsubsubparagraph] - \renewcommand{\thesubsubsubsubsubsubparagraph}{\thesubsubsubsubsubparagraph.\arabic{subsubsubsubsubsubparagraph}} - \titleformat{\subsubsubsubsubsubparagraph}{\normalfont\normalsize\bfseries}{\thesubsubsubsubsubsubparagraph}{1em}{} - \titlespacing*{\subsubsubsubsubsubparagraph}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} # 设置目录显示深度为10 - \setcounter{tocdepth}{10} # 设置标题编号深度为10 - \setcounter{secnumdepth}{10} ---
步骤2:替换Markdown深层标题为自定义LaTeX命令
将原Markdown中6级以上的标题(#######及更多#)替换为对应的LaTeX命令:
# 1. Top Level ## 1.1. First Sublevel ### 1.1.1. Second Sublevel #### 1.1.1.1. Third Sublevel ##### 1.1.1.1.1. Fourth Sublevel ###### 1.1.1.1.1.1. Fifth Sublevel \subsubparagraph{1.1.1.1.1.1.1. Sixth Sublevel} \subsubsubparagraph{1.1.1.1.1.1.1.1. Seventh Sublevel} \subsubsubsubparagraph{1.1.1.1.1.1.1.1.1. Eighth Sublevel} \subsubsubsubsubparagraph{1.1.1.1.1.1.1.1.1.1. Ninth Sublevel} \subsubsubsubsubsubparagraph{1.1.1.1.1.1.1.1.1.1.1. Tenth Sublevel}
步骤3:检查并调整Eisvogel模板(可选)
如果使用Eisvogel模板后仍不生效,需要确认模板中是否硬编码了tocdepth值:
- 找到Eisvogel模板文件(通常路径为
~/.pandoc/templates/eisvogel.latex) - 搜索
\setcounter{tocdepth}{,如果设置的是固定值(如5),将其改为\setcounter{tocdepth}{$toc-depth$},这样会读取你命令中设置的toc-depth=10变量。
步骤4:执行Pandoc命令
保持原命令不变,确保-V toc-depth=10参数存在:
pandoc example.md -o example.pdf --template eisvogel --from markdown --toc -V titlepage=true --listings --pdf-engine=xelatex -V toc-depth=10
关键说明
titlesec宏包用于自定义标题样式和层级,tocloft用于控制目录显示secnumdepth控制标题编号显示的深度,必须和tocdepth设置一致才能让深层标题带编号- 若觉得手动替换标题麻烦,可以用正则表达式批量替换Markdown中的多级
#为对应LaTeX命令,或者编写Pandoc过滤器自动转换
内容的提问来源于stack exchange,提问作者nk0
相关产品推荐
相关产品推荐

