如何创建带有虚线至页码的目录?

如何创建带有虚线至页码的目录?

我正在尝试使用软件包创建一个目录tocloft;该目录有两列,位于彩色框架框内,并一直到页码都有虚线。默认行为是虚线在页码之前停止得太早,留下了太多空白,不符合我的要求。

我对设置进行了一些尝试@pnumwidth,但这是一个固定值,页码可以是单个或多个数字。在下面的示例中,我将其设置@pnumwidth为 6pt,这对于单个数字页码来说很好,但对于两位数页码则不起作用,例如第 6 节“南极洲”以及以下章节。

附加问题是:我怎样才能使“安提瓜和巴巴多斯”部分标题中的“和”更接近安提瓜?

\documentclass[twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multicol} % For having two-columns in TOC and in text
\usepackage{tocloft} % For styling the table of contents
\usepackage{xcolor} % We want to use \fcolorbox
\usepackage{blindtext} % To generate dummy text

%\setcounter{tocdepth}{1} %Control depth of table of contents

% This removes the default TOC heading by redefining the cftmaketoctitle command in the tocloft package
\makeatletter
\renewcommand{\@cftmaketoctitle}{}
\renewcommand{\@pnumwidth}{6pt}
\makeatother

\renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}
\renewcommand\cftsecdotsep{\cftdot}
\renewcommand\cftsubsecdotsep{\cftdot}

\begin{document}

\setlength\fboxsep{20pt} % Set spacing for the TOC box
\setlength\fboxrule{2pt} % Set border with for the TOC box
\fcolorbox{red}{yellow}{\parbox{\dimexpr \linewidth-2\fboxsep-2\fboxrule}{%
  \centering{\textsc{\Large{Table Of Contents}}}
  \setlength\columnsep{25pt}
  \begin{multicols}{2}
    \tableofcontents
  \end{multicols}
}}

\section{Africa}
\subsection{Angola}
\subsection{Algeria}
\subsection{Benin}
\section{Asia}
\subsection{Afganistan}
\subsection{Armenia}
\subsection{Azerbaijan}
\section{Europe}
\subsection{Albania}
\subsection{Andorra} 
\subsection{Armenia}
\section{North America}
\subsection{Anguilla}
\subsection{Antigua and Barbuda}
\subsection{Aruba}
\section{South America}
\subsection{Argentia}
\subsection{Bolivia}
\subsection{Brazil}
\blindtext[75]
\section{Antarctica}
\subsection{South pole}
\section{Australia}
\subsection{Australia}
\subsection{Tasmania}
\end{document}

在此处输入图片描述

对以下答案的评论:感谢所有花时间回复的人!多亏了你们,我的问题解决了。

我更喜欢 Werner 对主要虚线问题的解决方案,其中命令\cftsecfillnum\cftsubsecfillnum得到了更新。

我更喜欢 Marijn 对我的奖励问题的解决方案,其中章节标题为“安提瓜和巴布达”,通过将目录中的标题排版为右侧参差不齐的方式。

我更喜欢这些答案,因为它们都是通用解决方案,并且都只修改了序言,而不修改文档正文。

答案1

您可以更新章节和小节编号填充符(\cftsecfillnum\cftsubsecfillnum)并删除框内的设置。

\renewcommand{\cftsecfillnum}[1]{%
  {\cftsecleader}\nobreak
  {\cftsecpagefont #1}\cftsecafterpnum\par
}
\renewcommand{\cftsubsecfillnum}[1]{%
  {\cftsubsecleader}\nobreak
  {\cftsubsecpagefont #1}\cftsubsecafterpnum\par
}

在此处输入图片描述

答案2

奖励问题可以从tocloft 手动的,第 5 页。这里的重要术语是右边参差不齐,这是 LaTeX/排版术语,表示“左对齐”。

将目录中的(部分)标题排版不正确,且没有连字符

\renewcommand{\@tocrmarg}{2.55em plus1fil}

其中 2.55em 的值可以根据需要改变。

实际上还有一个用户级命令\cftsetrmarg来重新定义这个宏,因为它不需要\makeatletter或,所以使用起来更容易一些\renewcommand

主要问题有点复杂。下面 MWE 中显示的方法重新定义了一个宏\@pnumwidth,其中使用,这样首先计算所需的宽度,存储在中\@pnumwidth,然后用于构建数字的框,这反过来会影响虚线的宽度。宏是全局设置的,因此它也适用于计算后使用该宏的其他地方。

该解决方案有点草率和粗略,因为 1. 它对 <10、<100 和 <1000 的值进行硬编码,因此无法正确处理包含 1000 页或更多页的文档;2. 计算是在章节标题的命令中完成的(因为这是来自 OP 的 MWE 中的顶级条目),这意味着它无法正确用于章节等,并且如果页码边界位于章节和子章节之间(例如,章节从第 9 页开始,子章节在第 10 页),则它将无法工作。如果需要,可以通过重新定义\cftsubsecfillnum和类似命令来解决这个问题,搜索tocloft源代码以\@pnumwidth查找所有章节命令。

梅威瑟:

\documentclass[twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multicol} % For having two-columns in TOC and in text
\usepackage{tocloft} % For styling the table of contents
\usepackage{xcolor} % We want to use \fcolorbox
\usepackage{blindtext} % To generate dummy text

%\setcounter{tocdepth}{1} %Control depth of table of contents

% This removes the default TOC heading by redefining the cftmaketoctitle command in the tocloft package
\makeatletter
\renewcommand{\@cftmaketoctitle}{}
% Calculate the desired width of the page number box to influence the length of the dotted line
\renewcommand{\cftsecfillnum}[1]{%
  \ifnum#1<10%
  \gdef\@pnumwidth{6pt}%
  \else%
  \ifnum#1<100%
  \gdef\@pnumwidth{12pt}%
  \else%
  \gdef\@pnumwidth{18pt}%
  \fi\fi%
  {\cftsecleader}\nobreak
  \makebox[\@pnumwidth][\cftpnumalign]{\cftsecpagefont #1}\cftsecafterpnum\par
}
\makeatother
\cftsetrmarg{2.55em plus1fil} % set left alignment for multiline titles

\renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}
\renewcommand\cftsecdotsep{\cftdot}
\renewcommand\cftsubsecdotsep{\cftdot}

\begin{document}

\setlength\fboxsep{20pt} % Set spacing for the TOC box
\setlength\fboxrule{2pt} % Set border with for the TOC box
\fcolorbox{red}{yellow}{\parbox{\dimexpr \linewidth-2\fboxsep-2\fboxrule}{%
  \centering{\textsc{\Large{Table Of Contents}}}
  \setlength\columnsep{25pt}
  \begin{multicols}{2}
    \tableofcontents
  \end{multicols}
}}

\section{Africa}
\subsection{Angola}
\subsection{Algeria}
\subsection{Benin}
\section{Asia}
\subsection{Afganistan}
\subsection{Armenia}
\subsection{Azerbaijan}
\section{Europe}
\subsection{Albania}
\subsection{Andorra} 
\subsection{Armenia}
\section{North America}
\subsection{Anguilla}
\subsection{Antigua and Barbuda}
\subsection{Aruba}
\section{South America}
\subsection{Argentia}
\subsection{Bolivia}
\subsection{Brazil}
\blindtext[75]
\section{Antarctica}
\subsection{South pole}
\section{Australia}
\subsection{Australia}
\subsection{Tasmania}
\end{document}

结果:

在此处输入图片描述

答案3

您可以\@pnumwidth在目录中间的\addtocontents相应页面上进行修改。

\documentclass[twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multicol} % For having two-columns in TOC and in text
\usepackage{tocloft} % For styling the table of contents
\usepackage{xcolor} % We want to use \fcolorbox
\usepackage{blindtext} % To generate dummy text

%\setcounter{tocdepth}{1} %Control depth of table of contents

% This removes the default TOC heading by redefining the cftmaketoctitle command in the tocloft package
\makeatletter
\renewcommand{\@cftmaketoctitle}{}
\renewcommand{\@pnumwidth}{5pt}
\makeatother

\renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}
\renewcommand\cftsecdotsep{\cftdot}
\renewcommand\cftsubsecdotsep{\cftdot}

\begin{document}
\setlength\fboxsep{20pt} % Set spacing for the TOC box
\setlength\fboxrule{2pt} % Set border with for the TOC box
\fcolorbox{red}{yellow}{\parbox{\dimexpr \linewidth-2\fboxsep-2\fboxrule}{%
  \centering{\textsc{\Large{Table Of Contents}}}
  \setlength\columnsep{25pt}
  \begin{multicols}{2}
    \tableofcontents
  \end{multicols}
}}

\section{Africa}
\subsection{Angola}
\subsection{Algeria}
\subsection{Benin}
\section{Asia}
\subsection{Afganistan}
\subsection{Armenia}
\subsection{Azerbaijan}
\section{Europe}
\subsection{Albania}
\subsection{Andorra} 
\subsection{Armenia}
\section{North America}
\subsection{Anguilla}
\subsection[Antigua~and~Barbuda]{Antigua and Barbuda}
\subsection{Aruba}
\section{South America}
\subsection{Argentia}
\subsection{Bolivia}
\subsection{Brazil}
\blindtext[75]
\makeatletter
\addtocontents{toc}{\string\renewcommand{\string\@pnumwidth}{11pt}}
\makeatother
\section{Antarctica}
\subsection{South pole}
\section{Australia}
\subsection{Australia}
\subsection{Tasmania}
\end{document}

演示


您还可以使用

\subsection[\hbox{Antigua and Barbuda}]{Antigua and Barbuda}

使其适合一行。

相关内容