排版(多页)目录时,如何设置目录部分的页眉上,“目录”两字的间距(例如4ccwd分散对齐)?

发布于 2025-02-14 19:42:15

利用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}

运行后显示的效果

A.jpg
希望实现的效果
B.jpg

查看更多

关注者
0
被浏览
223
2 个回答
Sagittarius Rover
我要成为Typst糕手/(ㄒoㄒ)/~~

\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}

image.png


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
}

image.png

竹林第八贤
竹林第八贤 6天前
这家伙很懒,什么也没写!

发现一个有趣的问题:您提供的代码可以完美显示这个效果。但是,我希望仅仅在frontmatter部分使用该页眉页脚设置,其他部分设置别的页眉页脚显示方式。所以,以上代码用花括号将frontmatter部分的所有章节都括起来之后,发现目录部分的显示又出现异常,去掉花括号之后就恢复。

{    
        \frontmatter%
    \pagestyle{frontmatter} 
    \pagenumbering{roman}
    \tableofcontents %生成目录
}    

此时页眉中“目录”二字的显示不统一且字符间距在后续页面不起作用,如下图
微信截图_20250214223154.jpg

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览