将目录与章节标题和页码之间的空间对齐

将目录与章节标题和页码之间的空间对齐

有没有办法像这些图片一样将目录居中在章节标题和页码之间的空间中(不是对齐到页面的右侧,而是整个目录相对居中,或者至少可以定位,相对于章节和页码之间的空白)?非常感谢您的帮助!

\documentclass{book}
\usepackage{titlesec}
\pagestyle{plain}

%no chapter numbers in ToC
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@chapter}{\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}{%
                      \addcontentsline{toc}{chapter}{\protect\numberline{}#1}}{\typeout{Success}}{\typeout{Failed!}}
                      
%page number next to chapter name in ToC
\usepackage{tocbasic}
\DeclareTOCStyleEntry[numwidth=2em,indent=0pt]{tocline}{part}
\usepackage{expl3}
\ExplSyntaxOn
  \clist_map_inline:nn  {part,chapter,section,subsection,subsubsection,paragraph,subparagraph}
  {\DeclareTOCStyleEntry[
    linefill=\enskip,
    raggedpagenumber,
    pagenumberbox=\mbox
  ]{tocline}{#1}}
  \ExplSyntaxOff

\begin{document}

\chapter{chapter1}

\chapter{chapter2}

\chapter{chapter3}

\end{document}

在此处输入图片描述

答案1

看来您还希望所有条目都加粗。因此我已将\textbf其添加到entryformat。如果您不想要这个,只需将其删除即可。

我认为你不需要\clist_map_inline:nn,因为 KOMA-Script 提供\DeclareTOCStyleEntries一次配置多个条目的功能。

恕我直言,您也不需要修补\@chapter,因为可以使用 删除该号码entrynumberformat

变量缩进定心条目的排列有点棘手。不幸的是,类似这样的方法indent=\fill不起作用。但您可以添加\hfillentryformat将条目移到右侧。为了不完全将条目移到右侧,而是以某种方式移到中间,我建议使用一个框(如果已定义页码的宽度)。您可以使用此框的宽度来操作条目的中心轴。较低的宽度会将所有条目移到右侧。较大的宽度会将所有条目移到左侧:

\documentclass{scrbook}

\usepackage{mwe}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCStyleEntries[
  indent=0pt,% don't use an extra indent of the entries
  numwidth=0pt,% no numbers so don't reserve space for the entry numbers
  linefill=\enskip,% space between text and page number
  raggedpagenumber,% don't use right aligned page number column (not really needed)
  entryformat=\hfill\textbf,% extra fill space at the left of the entries +
                            % bold entries
  pagenumberbox={\makebox[.333\linewidth][l]},% widths used for the left aligned page numbers
  entrynumberformat=\NoNumber,% don't print entry numbers (see definition above)
  pagenumberformat=\textbf,% bold page numbers
]
{tocline}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\end{document}

以某种方式居中的目录条目

我应该说,如果目录中的条目太长,这种方法就行不通了。但目前您还没有指定是否需要处理多行条目以及如何处理。

此建议也适用于标准类,例如book,但在这种情况下不适用于部分条目,因为在编写条目时book不使用。在这种情况下,您首先必须额外加载包才能使用。要从标准类的部分条目中删除数字,您必须修补:\numberlineparttocbasic\DeclareTOCStyleEntries\@part

\documentclass{book}

\usepackage{mwe}
\usepackage{tocbasic}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{#1}%
}{}{\PatchFailure}
\makeatother

\newcommand*{\NoNumber}[1]{}
\DeclareTOCStyleEntries[
  indent=0pt,% don't use an extra indent of the entries
  numwidth=0pt,% no numbers so don't reserve space for the entry numbers
  linefill=\enskip,% space between text and page number
  pagenumberbox={\makebox[.333\linewidth][l]},% widths used for the left aligned page numbers
  entryformat=\hfill\textbf,% extra fill space at the left of the entries +
                            % bold entries
  entrynumberformat=\NoNumber,% don't print entry numbers (see definition above)
  pagenumberformat=\textbf,% bold page numbers
]
{tocline}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\end{document}

标准类也一样

顺便说一句:另一个建议可能是定义您自己的 ToC 条目样式:

\documentclass{book}

\usepackage{mwe}
\usepackage{tocbasic}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{#1}%
}{}{\PatchFailure}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCEntryStyle{centered}[{%
  % Following definition by cases is only needed for automatic setup of the
  % level option with the standard classes. With KOMA-Script classes, the
  % levels are already initialized and the cases are not needed. These case definition has been copied from tocbasic.sty.
  \@ifundefined{#1tocdepth}{%
    \Ifstr{#1}{part}{\@namedef{#1tocdepth}{-1}}{%
      \Ifstr{#1}{chapter}{\@namedef{#1tocdepth}{0}}{%
        \Ifstr{#1}{section}{\@namedef{#1tocdepth}{1}}{%
          \Ifstr{#1}{subsection}{\@namedef{#1tocdepth}{2}}{%
            \Ifstr{#1}{subsubsection}{\@namedef{#1tocdepth}{3}}{%
              \Ifstr{#1}{paragraph}{\@namedef{#1tocdepth}{4}}{%
                \Ifstr{#1}{subparagraph}{\@namedef{#1tocdepth}{5}}{%
                  \Ifstr{#1}{figure}{\@namedef{#1tocdepth}{1}}{%
                    \Ifstr{#1}{table}{\@namedef{#1tocdepth}{1}}{%
                      \Ifstr{#1}{lstlisting}{\@namedef{#1tocdepth}{1}}{%
                        \expandafter\let\csname #1tocdepth\expandafter\endcsname
                        \csname #1numdepth\endcsname
                      }%
                    }%
                  }%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
  }{}%
}]{%
  \renewcommand*\numberline[1]{}% no numbers
  % Use a \parbox for the text (and number). The width of the \parbox specifies the horizontal position of the page number
  \noindent\bfseries\parbox[b]{.667\linewidth}{\raggedleft\strut ##1\strut}\enskip ##2\par
}
\makeatother

\DeclareTOCStyleEntries
{centered}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\section{A section with a much to long entry to fit into one line}
\blindtext
\end{document}

在此处输入图片描述

再次,如果您使用 KOMA-Script 类,这将会更容易:

\documentclass{scrbook}

\usepackage{mwe}

\newcommand*{\NoNumber}[1]{}
\DeclareTOCEntryStyle{centered}{%
  \renewcommand*\numberline[1]{}% no numbers
  \noindent\bfseries\parbox[b]{.667\linewidth}{\raggedleft\strut ##1\strut}\enskip ##2\par
}

\DeclareTOCStyleEntries
{centered}{part,chapter,section,subsection,paragraph,subparagraph}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\section{A section with a much to long entry to fit into one line}
\end{document}

在此处输入图片描述

\DeclareTOCEntryStyle有关更多信息,请参阅KOMA-Script 手册,例如,如果您需要每个条目级别的附加格式。

相关内容