如何在目录中实现:当编号过长时,编号条目悬挂缩进

发布于 2026-06-29 23:17:02

希望对目录条目作如下图的修改,使(用一、二、三个字符编号的)条目悬挂缩进:

Screenshot_20260629-230958.png

\documentclass[UTF8,punct=banjiao,fontset=fandol]{ctexart}
%% 载入各种宏包 -----------------------------
\usepackage{tocloft,zhlipsum,geometry}
%% 自定义命令 -------------------------------
\newcommand{\setsecpage}[2]{
  \setcounter{section}{#1}
  \setcounter{page}{#2}}
%% ctex 相关设定 ----------------------------
\ctexset{
  section/number={\zhdigits{\arabic{section}}},
  section/format={\bfseries}}
%% geometry 相关设定 ------------------------
\geometry{a6paper, hmargin=5mm, vmargin=1cm, headsep=2.5mm, footskip=7.5mm, marginparwidth=5mm, marginparsep=0pt, showframe}%
%% 页眉页脚相关设定 --------------------------
\makeatletter
\renewcommand{\@oddhead}{}
\renewcommand{\@evenhead}{}
\renewcommand{\@oddfoot}{\hfill\thepage}
\renewcommand{\@evenfoot}{\thepage\hfill}
\makeatother
%% 目录相关设定 ------------------------------
%% 目录标题名 : 
\renewcommand{\contentsname}{\bfseries 目\quad 录}
\renewcommand\cfttoctitlefont{\null\hfill}
\renewcommand\cftaftertoctitle{\hfill\null}
%% 目录页码 : 
\renewcommand{\cftsecpagefont}{}
\renewcommand{\cftsecafterpnum}{}
%% 目录引导点 : 
\renewcommand\cftdot{$\cdot$}
\renewcommand{\cftdotsep}{0}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
%% 目录条目 : 
\renewcommand{\cftsecfont}{}
\begin{document} 

\tableofcontents

\setsecpage{3}{5}
\section{“挨”在“挨着坐”和“挨到天亮”中的读音}
\zhlipsum[1-2]

\setsecpage{17}{35}
\section{“屏”在“屏除杂念”中的读音}
\zhlipsum[1-2]

\setsecpage{101}{78}
\section{“空地”“空缺”中的“空”读四声 kòng,不读一声kōng}
\zhlipsum[1-2]

\setsecpage{128}{158}
\section{“宁缺勿滥”“宁死不屈”中的“宁”读四声nìng,不读二声níng}
\zhlipsum[1-2]

\setsecpage{188}{259}
\section{“肖像”“生肖”中的“肖”读四声 xiào,不读一声xiāo}
\zhlipsum[1-2]

\end{document}

查看更多

关注者
0
被浏览
48
1 个回答
雾月
雾月 2小时前
这家伙很懒,什么也没写!

可以使用 \cftsetindents{⟨entry⟩}{⟨indent⟩}{⟨numwidth⟩}entry 就是 section 这些,indent 就是第一行的缩进,numwidth 就是标题数字的宽度,也是第一行之后的缩进。

% ctex 五号字的是 10.5bp,一共三个字,ctex 还会在数字后面加上半个字宽的间距
\cftsetindents{section}{0pt}{\dimeval{10.5bp*3+10.5bp/2}}

这样的结果是
image.png
比较短的数字后有额外的间距。ctex 做了补丁,它把数字的宽度设置为其自然宽度(加上半个字宽)和 numwidth 中的较大值,如果没有这个补丁,数字的宽度就是 numwidth

如果要保持自然宽度,需要重定义

\usepackage{calc}
\makeatletter
\protected\def\CTEX@toc@width@n#1{\setlength\@tempdima{\widthof{#1}+\f@size\p@/2}}
\makeatother

image.png

完整代码:

\documentclass[UTF8,punct=banjiao,fontset=fandol]{ctexart}
%% 载入各种宏包 -----------------------------
\usepackage{tocloft,zhlipsum,geometry}
%% 自定义命令 -------------------------------
\newcommand{\setsecpage}[2]{
  \setcounter{section}{#1}
  \setcounter{page}{#2}}
%% ctex 相关设定 ----------------------------
\ctexset{
  section/number={\zhdigits{\arabic{section}}},
  section/format={\bfseries}}
%% geometry 相关设定 ------------------------
\geometry{a6paper, hmargin=5mm, vmargin=1cm, headsep=2.5mm, footskip=7.5mm, marginparwidth=5mm, marginparsep=0pt, showframe}%
%% 页眉页脚相关设定 --------------------------
\makeatletter
\renewcommand{\@oddhead}{}
\renewcommand{\@evenhead}{}
\renewcommand{\@oddfoot}{\hfill\thepage}
\renewcommand{\@evenfoot}{\thepage\hfill}
\makeatother
%% 目录相关设定 ------------------------------
%% 目录标题名 : 
\renewcommand{\contentsname}{\bfseries 目\quad 录}
\renewcommand\cfttoctitlefont{\null\hfill}
\renewcommand\cftaftertoctitle{\hfill\null}
%% 目录页码 : 
\renewcommand{\cftsecpagefont}{}
\renewcommand{\cftsecafterpnum}{}
%% 目录引导点 : 
\renewcommand\cftdot{$\cdot$}
\renewcommand{\cftdotsep}{0}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
%% 目录条目 : 
\renewcommand{\cftsecfont}{}

\cftsetindents{section}{0pt}{\dimeval{10.5bp*3+10.5bp/2}}
\usepackage{calc}
\makeatletter
\protected\def\CTEX@toc@width@n#1{\setlength\@tempdima{\widthof{#1}+\f@size\p@/2}}
\makeatother
\begin{document}

\tableofcontents

\setsecpage{3}{5}
\section{“挨”在“挨着坐”和“挨到天亮”中的读音}
\zhlipsum[1-2]

\setsecpage{17}{35}
\section{“屏”在“屏除杂念”中的读音}
\zhlipsum[1-2]

\setsecpage{101}{78}
\section{“空地”“空缺”中的“空”读四声 kòng,不读一声kōng}
\zhlipsum[1-2]

\setsecpage{128}{158}
\section{“宁缺勿滥”“宁死不屈”中的“宁”读四声nìng,不读二声níng}
\zhlipsum[1-2]

\setsecpage{188}{259}
\section{“肖像”“生肖”中的“肖”读四声 xiào,不读一声xiāo}
\zhlipsum[1-2]

\end{document}

我没有测试以上代码在 ctex 前几天发的新版中是否仍然生效,但旧版可行。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览