使用罗马页码的大页码未按目录中的预期对齐

使用罗马页码的大页码未按目录中的预期对齐

问题描述

使用罗马页码时,大页码在目录 (TOC) 中无法按预期对齐。我希望它们像阿拉伯数字一样右对齐。

问题预览

在此处输入图片描述

提出问题的快速解决方案@alephzero

@alephzero建议增加:

\renewcommand{\@pnumwidth}{<Required-Length-to-Fix-the-Problem>}

在文档开始之前 - 这确实有效。但是,此修复需要手动找到解决问题所需的长度 - 这取决于文档中遇到的最长罗马页码的大小。最重要的是,即使对于阿拉伯数字,它也会缩短虚线,而这显然不是必需的。

预览由@alephzero

在此处输入图片描述

问题

如何才能解决这个问题,使得罗马页码能够在所有可能的数字范围内自动右对齐,并且自动生成虚线来填充标题和页码之间的空白?

最小工作示例(无修复)

\documentclass{article}

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is not properly aligned}

\end{document}

最小工作示例(带修复)

\documentclass{article}

\makeatletter 
\renewcommand{\@pnumwidth}{5em} 
\makeatother

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is not properly aligned}

\end{document}

接受@Werner 提出的问题的解决办法(见下文答案)

在此处输入图片描述

\documentclass{article}

\usepackage{tocloft,etoolbox}

\makeatletter

\patchcmd{\cftsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubsubsecfillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftparafillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\patchcmd{\cftsubparafillnum}
{\makebox[\@pnumwidth][\cftpnumalign]}{}{}{}

\makeatother

\begin{document}

\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\tableofcontents

\newpage
\pagenumbering{arabic}
\setcounter{page}{555}
\section{Page numbering using Arabic numbers}
\newpage
\setcounter{page}{666}
\subsection{Sub-Section}
\newpage
\setcounter{page}{777}
\subsubsection{Sub-Sub-Section}
\newpage
\setcounter{page}{888}
\paragraph{Paragraph}
\newpage
\setcounter{page}{999}
\subparagraph{Sub-Paragraph}

\newpage
\pagenumbering{Roman}
\setcounter{page}{555}
\section{Page numbering using Roman numbers}
\newpage
\setcounter{page}{666}
\subsection{Sub-Section}
\newpage
\setcounter{page}{777}
\subsubsection{Sub-Sub-Section}
\newpage
\setcounter{page}{888}
\paragraph{Paragraph}
\newpage
\setcounter{page}{999}
\subparagraph{Sub-Paragraph}

\end{document}

答案1

您只需要更改带有前导符的目录条目。对于您的情况,我们可以\subsection通过删除放置页码的框来修补目录条目设置页码的方式。这还会删除任何可能可见的对齐方式,将其设置为与前导符一致:

在此处输入图片描述

\documentclass{article}

\usepackage{tocloft,etoolbox}

\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\cftsubsecfillnum}% <cmd>
  {\makebox[\@pnumwidth][\cftpnumalign]}% <search>
  {}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}

\tableofcontents

\pagenumbering{arabic}
\setcounter{page}{999}
\section{Page numbering using Arabic numbers}
\subsection{We can see that 999 is right-aligned as expected}

\newpage

\pagenumbering{Roman}
\setcounter{page}{999}
\section{Page numbering using Roman numbers}
\subsection{We can see that CMXCIX is properly aligned}

\end{document}

我们用tocloft因为它为 ToC 条目提供了可识别的结构,并且提供了相当容易(且可理解)修补的内容。

补丁(感谢etoolbox采用类似于的原始结构

\newcommand{\cftsubsecfillnum}[1]{%
  {\cftsubsecleader}\nobreak
  \makebox[\@pnumwidth][\cftpnumalign]{\cftsubsecpagefont #1}\cftsubsecafterpnum\par
}

并将其更改为

\newcommand{\cftsubsecfillnum}[1]{%
  {\cftsubsecleader}\nobreak
  {\cftsubsecpagefont #1}\cftsubsecafterpnum\par
}

所以不存在装箱或对齐问题。

如果存在领导者,则可以对目录中的其他条目执行相同的操作(例如\subsubsection或类似)。

相关内容