10 如何丝滑实现小说目录,回目标题的双行显示?

发布于 2025-03-24 12:00:32

排版小说的时候,(对仗句式的)回目标题较长,想要在目录实现双行显示,如何让换行后的目录整体显示协调,观感舒适?以下是我的代码:

\documentclass[b5paper,twoside,openany,UTF8,no-math]{ctexbook}
\usepackage[left=2.54cm,right=2.54cm,top=2.54cm,bottom=2.54cm]{geometry}
\usepackage{zhlipsum,indentfirst,hyperref}
\usepackage{subcaption,graphicx} 

%==================回目编号与标题风格==================%
\ctexset{
    contentsname = {\texorpdfstring{\makebox[5\ccwd][s]{目录}}{目录}},    
    chapter/name = {,}, 
    chapter/number = {\texorpdfstring{\makebox[5\ccwd][s]{第\chinese{chapter}回}}{第\chinese{chapter}回}},
    %定义回目编号及显示样式
}

\usepackage{titlesec}
\titleformat{\chapter}[display]{\centering\Large\vskip -1cm}{\makebox[5\ccwd][s]{第\chinese{chapter}回}}{0.5ex}{\vskip 15pt\centering}
%设定每一回的chapter标题样式

%===========================%中文目录配置%========================%
\usepackage[titles,subfigure]{tocloft}
\renewcommand{\cftdot}{$\cdot$}
\renewcommand{\cftdotsep}{2}
\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}}
\renewcommand{\cftchapdotsep}{\cftdotsep}
\makeatletter
\renewcommand{\numberline}[1]{%
    \settowidth\@tempdimb{#1\hspace{0.5em}}%
    \ifdim\@tempdima<\@tempdimb%
    \@tempdima=\@tempdimb%
    \fi%
    \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
\makeatother

\begin{document}
    \tableofcontents  
    
    
    \chapter[栊翠庵茶品梅花雪\\怡红院劫遇母蝗虫]{栊翠庵茶品梅花雪\hspace{2ex}怡红院劫遇母蝗虫}
    
    \zhlipsum[1-4]
    
    \chapter{这一回不需要双行目录}
    
    \zhlipsum[1-2]
    
\end{document}

呈现效果
my.jpg
期望实现的效果
Scan_20250324_115526.jpg
希望得到各位大神们的指点,谢谢!

查看更多

关注者
1
被浏览
345
尊咾哎哟
尊咾哎哟 2025-03-24
¥a^2+b^2=c^2¥. . .

一个不skilled的方法。

核心思路是使用盒子:

\parbox[l]{<字数>\ccwd}{<第一行内容>\newline{}<第二行内容>}
% <字数>的大小对应两行中字数较大者

这边无法确定正文是否也需要想目录那样对齐,所以下面分类解决.

所有情况下MWE的结果均为:
image.png

目录中的为双行、正文中的随意

即:正文中两行之间要么用符号分隔,要么直接连成一句话.
\documentclass{ctexbook}

\ctexset{
    chapter={
        name={第,回},
        number={\chinese{chapter}}
    }
}

\usepackage[hidelinks,bookmarksnumbered=true, breaklinks=true]{hyperref}

\begin{document}

\tableofcontents

\chapter[\texorpdfstring{\parbox[l]{8\ccwd}{甄士隐梦幻识通\newline{}贾雨村风尘怀闺秀}}{甄士隐梦幻识通灵,贾雨村风尘怀闺秀}]{甄士隐梦幻识通灵,贾雨村风尘怀闺秀}

\chapter{这一回不需要双行目录}

\chapter[\texorpdfstring{\parbox[l]{8\ccwd}{甄士隐梦幻识通灵\newline{}贾雨村风尘怀闺秀}}{甄士隐梦幻识通灵贾雨村风尘怀闺秀}]{甄士隐梦幻识通灵贾雨村风尘怀闺秀}

\end{document}

正文、目录的标题样式均双行

从你给的图来看,不是所有的章标题都要双行,所以直接封装一个新的章标题命令\twolinechapter

  • 参数1:字数最多行的字数
  • 参数2:第一行的内容
  • 参数3:第二行的内容
\documentclass{ctexbook}

\ctexset{
    chapter={
        name={第,回},
        number={\chinese{chapter}}
    }
}

% ------------- 封装部分 -------------- %
\newcommand{\twolinechapter}[3]{
\chapter{\texorpdfstring{\parbox[l]{#1\ccwd}{#2\newline{}#3}}{#2,#3}} % \texorpdfstring命令是为了照顾到pdf书签中的输出内容,下同
} 
% ====================================== %

\usepackage[hidelinks,bookmarksnumbered=true, breaklinks=true]{hyperref}

\begin{document}

\tableofcontents

\twolinechapter{8}{甄士隐梦幻识通灵}{贾雨村风尘怀闺秀}

\chapter{这一回不需要双行目录}

\twolinechapter{8}{甄士隐梦幻识通}{贾雨村风尘怀闺秀}

\end{document}

正文中的按逗号分隔、目录的为双行

还是封装一个新的章标题命令\twolinechapter

\documentclass{ctexbook}

\ctexset{
    chapter={
        name={第,回},
        number={\chinese{chapter}}
    }
}

% ------------- 封装部分 -------------- %
\newcommand{\twolinechapter}[3]{
\chapter[\texorpdfstring{\parbox[l]{#1\ccwd}{#2\newline{}#3}}{#2,#3}]{#2,#3}
}
% ====================================== %

\usepackage[hidelinks,bookmarksnumbered=true, breaklinks=true]{hyperref}

\begin{document}

\tableofcontents

\twolinechapter{8}{甄士隐梦幻识通灵}{贾雨村风尘怀闺秀}

\chapter{这一回不需要双行目录}

\twolinechapter{8}{甄士隐梦幻识通}{贾雨村风尘怀闺秀}

\end{document}

小插曲

\settowidth命令对中文文本长度的获取貌似不准确,不然\twolinechapter命令的第一个参数可以省去。

2 个回答
Sagittarius Rover
Sagittarius Rover 2025-03-24
我要成为Typst糕手/(ㄒoㄒ)/~~

off-topic:

李清老师在2012年重排过一版《金瓶梅》,可见这个需要魔法的链接

为避免网络问题,这里上传相关的文件以供参考:

其中实现了类似的效果,可惜...我看不太懂李清在.sty中的设置:

image.png

期待题主的排版大作早日问世~

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览