LaTeX Beamer演示文稿目录编号与标题间距统一调整诉求
解决Beamer多位数章节编号的目录间距问题
问题分析
你当前的代码中,secnumwidth等长度是基于\beamer@sectionmax计算的,但这个值在文档开头(生成目录前)还未被正确赋值(此时章节尚未全部定义),导致实际宽度仅适配1位数字编号。后续多位数编号无法容纳在预设宽度内,进而出现间距过小甚至重叠的问题。同时子章节模板中使用的\rlap会让编号内容无宽度限制,进一步加剧重叠。
解决方案
通过手动预设足够容纳最长编号的宽度,并调整目录模板的对齐方式,确保所有位数的编号都能和1位编号保持一致的标题缩进:
修改后的完整代码
\documentclass[9pt,trans,xcolor={table},envcountsect]{beamer} % Plain \usefonttheme[onlymath]{serif} \usetheme[shownavsym,right]{Aalborg} \setbeamertemplate{navigation symbols}{\insertframenavigationsymbol \insertsectionnavigationsymbol \insertbackfindforwardnavigationsymbol} % Nav symbols \makeatletter \newcommand{\ShiftSectionNumber}[1]{% \beamer@tocsectionnumber=\numexpr#1+\beamer@tocsectionnumber} \makeatother \makeatletter % 预设能容纳最长编号的宽度(这里按4位数字+句号+空格设置) \newlength{\secnumwidth} \settowidth{\secnumwidth}{1000. } % 适配4位章节编号 \newlength{\subsecnumwidth} \settowidth{\subsecnumwidth}{.10 } % 适配2位子章节编号 \newlength{\subsubsecnumwidth} \settowidth{\subsubsecnumwidth}{.10 } % 适配2位子子章节编号 \setbeamertemplate{section in toc}{% \leavevmode% \ifnum\beamer@tempcount>\beamer@toclastsection \else \ifnum\beamer@tempcount>0 \makebox[\secnumwidth][l]{\inserttocsectionnumber.}% 固定宽度盒子左对齐 \fi\fi% \inserttocsection\par% } \setbeamertemplate{subsection in toc}{% \leavevmode \leftskip=\secnumwidth % 章节编号宽度的缩进 \makebox[\subsecnumwidth][l]{\inserttocsectionnumber.\inserttocsubsectionnumber}% 固定宽度盒子 \inserttocsubsection\par% } \setbeamertemplate{subsubsection in toc}{% \leavevmode \leftskip=\dimexpr\secnumwidth+\subsecnumwidth\relax % 章节+子章节编号宽度的缩进 \makebox[\subsubsecnumwidth][l]{\inserttocsectionnumber.\inserttocsubsectionnumber.\inserttocsubsubsectionnumber}% 固定宽度盒子 \inserttocsubsubsection\par% } \makeatother \begin{document} \begin{frame}%[allowframebreaks] \frametitle{Overview} \tableofcontents \end{frame} \section{First Section (1-digit section number)} \subsection{Subsection} \subsubsection{Subsubsection} \ShiftSectionNumber{8} \section{Tenth Section (2-digit section number)} \subsection{Subsection} \subsubsection{Subsubsection} \ShiftSectionNumber{89} \section{Hundredth Section (3-digit section number)} \subsection{Subsection} \subsubsection{Subsubsection} \ShiftSectionNumber{899} \section{Thousandth Section (4-digit section number)} \subsection{Subsection} \subsubsection{Subsubsection} \begin{frame}[<+->]{Spacing in TOC} Hello. \end{frame} \end{document}
关键修改点
- 预设固定宽度:
\secnumwidth设置为能容纳1000.的宽度,确保4位章节编号完全放下\subsecnumwidth和\subsubsecnumwidth分别设置为能容纳.10的宽度,适配两位数的子章节/子子章节编号
- 替换
\rlap为固定宽度盒子:- 子章节和子子章节模板中,用
\makebox[宽度][l]{内容}替代\rlap,确保编号占据固定宽度,标题从统一位置开始,避免重叠
- 子章节和子子章节模板中,用
- 调整缩进逻辑:
- 子章节的左缩进等于章节编号宽度,子子章节的左缩进等于章节+子章节编号宽度,保证层级对齐一致
效果说明
修改后,无论章节编号是1位、2位、3位还是4位,目录中的标题都会从相同的水平位置开始,和1位编号时的间距保持一致,不会出现重叠或间距过小的问题。
内容的提问来源于stack exchange,提问作者Peter Jensen
相关产品推荐
相关产品推荐

