利用tableofcontents指令排版(多页)目录时,如何设置目录部分的页眉上,“目录”两字的间距(例如4ccwd分散对齐)?以下是我的代码
\documentclass[b5paper,twoside,openany,UTF8]{ctexbook}
\usepackage{fancyhdr}
% 设置全局页眉页脚格式
\pagestyle{fancy}
\fancyhf{} % 清空默认页眉页脚
% 章节首页(奇数页)的页眉显示章节名称
\fancyhead[CO]{\zihao{5}\rightmark}
% 偶数页页眉显示“第一回、第二回……”
\fancyhead[CE]{\zihao{5}\leftmark}
% 偶数页左下角、奇数页右下角显示页码
\fancyfoot[RO,LE]{--~\thepage~--}
% 设置页眉横线
\renewcommand{\headrulewidth}{0.5pt}
% 设定章节标记:
% - 左页(偶数页)显示“第一回、第二回……”
% - 右页(奇数页)显示章节名称
\renewcommand{\chaptermark}[1]{%
\markboth{\texorpdfstring{\makebox[5\ccwd][s]{第\zhnumber{\thechapter}回}}{第\zhnumber{\thechapter}回}}{#1}
}
% 全文章节首页样式(无页眉和页眉横线,仅保留页码)
\fancypagestyle{plain}{%
\fancyhf{} % 清空页眉页脚
% 偶数页左下角、奇数页右下角显示页码
\fancyfoot[RO,LE]{--~\thepage~--}
\renewcommand{\headrulewidth}{0pt} % 去掉页眉横线
}
\fancypagestyle{frontmatter}{ %frontmatter部分的页眉页脚定制
\pagestyle{plain}
\fancyhf{}
\fancyhead[CO]{\zihao{5}\rightmark}
\fancyhead[CE]{\zihao{5}\makebox[6\ccwd][s]{这是一本书}}
\renewcommand{\headrulewidth}{0.5pt} % 确保页眉横线宽度为0.5pt
\fancyfoot[LE,RO]{--~\thepage~--} %奇数页右下角、偶数页左下角显示页码
}
\begin{document}
{ \frontmatter
\pagestyle{frontmatter}
\pagenumbering{roman}
\tableofcontents %生成目录
}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\end{document}
运行后显示的效果
希望实现的效果
将\fancyhead[CO]{\zihao{5}\rightmark}
替换为\fancyhead[CO]{\zihao{5}\makebox[6\ccwd][s]{\rightmark}}
。
另外你似乎多删除了hyperref
宏包的调用。或许可以试试这个:
\documentclass[b5paper,twoside,openany,UTF8]{ctexbook}
\usepackage{fancyhdr}
\usepackage{hyperref}%
\pagestyle{fancy}
\fancyhf{}
\fancyhead[CO]{\zihao{5}\rightmark}
\fancyhead[CE]{\zihao{5}\leftmark}
\fancyfoot[RO,LE]{--~\thepage~--}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\chaptermark}[1]{%
\markboth{\texorpdfstring{\makebox[5\ccwd][s]{第\zhnumber{\thechapter}回}}{第\zhnumber{\thechapter}回}}{#1}
}
\fancypagestyle{plain}{%
\fancyhf{} % 清空页眉页脚
% 偶数页左下角、奇数页右下角显示页码
\fancyfoot[RO,LE]{--~\thepage~--}
\renewcommand{\headrulewidth}{0pt} % 去掉页眉横线
}
\fancypagestyle{frontmatter}{ %frontmatter部分的页眉页脚定制
\pagestyle{plain}
\fancyhf{}
\fancyhead[CO]{\zihao{5}\makebox[6\ccwd][s]{\rightmark}}
\fancyhead[CE]{\zihao{5}\makebox[6\ccwd][s]{这是一本书}}
\renewcommand{\headrulewidth}{0.5pt} % 确保页眉横线宽度为0.5pt
\fancyfoot[LE,RO]{--~\thepage~--} %奇数页右下角、偶数页左下角显示页码
}
\begin{document}
\frontmatter%
\pagestyle{frontmatter}
\pagenumbering{roman}
\tableofcontents %生成目录
\mainmatter% 我认为作为一本完整的书
% \frontmatter \mainmatter \backmatter总是要用好的
\pagestyle{fancy}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\chapter{title} \chapter{title} \chapter{title} \chapter{title} \chapter{title}
\end{document}
BTW,如果追求代码更简洁的话,也许可以把\fancypagestyle
缩短,让他继承自plain
样式:
\fancypagestyle{frontmatter}[plain]{%frontmatter部分的页眉页脚定制
\fancyhead[CO]{\zihao{5}\makebox[6\ccwd][s]{\rightmark}}
\fancyhead[CE]{\zihao{5}\makebox[6\ccwd][s]{这是一本书}}
\renewcommand{\headrulewidth}{0.5pt} % 确保页眉横线宽度为0.5pt
}
我不知道为什么加上
{}
之后会有这一问题。不过你这里已经有:fancy
样式plain
样式frontmatter
样式我个人觉得应该尽量避免把
\tableofcontents
这一与输出例程(output rountine)有关的代码放在编组内,这样做有可能产生意料之外的效果(但我暂时提供不了更多的技术细节)。我想,你可以纵览全书,想想一共需要几种页眉页脚样式,应该总是可以通过
\pagestyle
来实现切换的,而且这个编组感觉比较突兀。尝试了很多方法都没有较好解决。后期请教DeepSeek大模型得到一个解决方案,代码如下:
@u6746
@u6746 能实现那确实挺好的(赞),但总有种拼接感,这样的
fancyhdr
方案很不fancy
也不elegant
。可以在【frontmatter部分】使用
plain
作为目录的首页样式;使用frontmatter
作为目录的其他页面样式之后在其他部分使用
fancyA
,fancyB
,...,fancyN
作为样式,使用\pagestyle{fancyN}
界定进行且切换即可,这样的缺陷在哪里呢。不太明白为什么 非要“使用这种编组内套目录的代码”不可。如果您有时间,可否提供一个不能使用例子?
下面是
\cleardoublepage
的原始定义