在目录中对齐章节标题的首字母

在目录中对齐章节标题的首字母

我正在尝试为带星号的章节创建目录。我的目标是将标题的所有首字母对齐到同一位置。我可以分别针对个位数和两位数实现此目的,但我无法同时创建适用于个位数章节(1-9)和两位数章节(10-99)的对齐方式。

为了在数字和标题之间添加水平间距,我使用了两种方法:不间断空格(~)和\hskip

带有不间断空格的代码(~):

\documentclass[final,11pt]{book}
\def \mychapter#1#2{%
\chapter*{#1 \\ #2}%
\addcontentsline{toc}{chapter}{#1~~~#2}%
}
\begin{document}
\tableofcontents
\mychapter{8}{This chapter title}
\mychapter{9}{This chapter title}
\mychapter{10}{This chapter title}
\mychapter{11}{This chapter title}
\mychapter{20}{This chapter title}
\end{document}

代码如下\hskip

\documentclass[final,11pt]{book}
\def \mychapter#1#2{%
\chapter*{#1 \\ #2}%
\addcontentsline{toc}{chapter}{#1\hskip 0.3cm#2}%
}
\begin{document}
\tableofcontents
\mychapter{8}{This chapter title}
\mychapter{9}{This chapter title}
\mychapter{10}{This chapter title}
\mychapter{11}{This chapter title}
\mychapter{20}{This chapter title}
\end{document}

我最接近的解决方案是对单数章节使用类似这样的方法(除了\hskip基本mychapter定义之外):

\mychapter{9\hskip 0.2cm}{This chapter title}

有没有比这更好的方法来精确对齐首字母?

答案1

尝试这个:

\documentclass[final,11pt]{book}
\def \mychapter#1#2{%
\chapter*{#1 \\ #2}%
\addcontentsline{toc}{chapter}{\hbox to 1cm{#1} #2}% change 1cm to any number
}
\begin{document}
\tableofcontents
\mychapter{8}{This chapter title}
\mychapter{9}{This chapter title}
\mychapter{10}{This chapter title}
\mychapter{11}{This chapter title}
\mychapter{20}{This chapter title}
\end{document}

相关内容