将目录与 titletoc 对齐

将目录与 titletoc 对齐

我正在尝试使用 titletoc 包构建目录,因为我正在使用它来制作其他内容表。我让它几乎按照我想要的方式工作,但我遇到了一些问题: enter image description here

首先,我必须手动计算意图,如下所示。

\settowidth{\tocindent}{2.12.2}% calculation of the indentation

我尝试了不同的方法,通过重新定义命令来计算最大宽度\subsubsection,但对我来说不起作用。我希望 LaTeX 能帮我计算这个值,而不是直接输入。

我遇到的第二个问题是未编号的部分,它们应该与数字对齐。我让它与段落定义一起工作,但这只会给 hyperref 包带来很多麻烦:

  • Difference (3) between bookmark levels is greater (hyperref) than one, level fixed
  • The anchor of a bookmark and its parent's must not(hyperref) be the same. Added a new anchor

继 MWE 之后:

\documentclass[10pt,]{scrartcl}
\usepackage[hidelinks]{hyperref}
\usepackage{titletoc}
\usepackage{showframe}
\usepackage{calc}
\newlength{\tocindent}

%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\settowidth{\tocindent}{2.12.2}% calculation of the indentation
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

\titlecontents{section}[\dimexpr  \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr  \tocindent+0.5em}}
{}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsection}[\dimexpr  \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr  \tocindent+0.5em}}
{}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsubsection}[\dimexpr  \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr  \tocindent+0.5em}}
{}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{paragraph}[0pt]{}
{\contentslabel[{\thecontentslabel}]{0pt}}
{}
{\titlerule*[8pt]{.}\contentspage}

\begin{document}
\startcontents[sections]
{\Large Inhaltsverzeichnis}\\
\printcontents[sections]{l}{1}{\setcounter{tocdepth}{4}}

\section{Section A}
\section{Section B}
\subsection{Subsection B 1}
\subsection{Subsection B 2}
\subsection{Subsection B 3}
\subsection{Subsection B 4}
\subsection{Subsection B 5}
\subsection{Subsection B 6}
\subsection{Subsection B 7}
\subsection{Subsection B 8}
\subsection{Subsection B 9}
\subsection{Subsection B 10}
\subsection{Subsection B 11}
\subsection{Subsection B 12}
\subsubsection{Subsubsection B 13.1}
\subsubsection{Subsubsection B 13.2}
\subsection{Subsection B 14}
\subsection{Subsection B 15}
\section{Section C}
\subsection{Subsection C 1}
\subsection{Subsection C 2}
\subsubsection{Subsubsection C 2.1}
\subsubsection{Subsubsection C 2.2}
\subsection{Subsection C 3}
\addcontentsline{toc}{section}{List of Figures (section)}
\addcontentsline{toc}{paragraph}{List of Figures (paragraph)}
\end{document}

希望你们能帮助我!


编辑

在...的帮助下@Bernard我已经走到现在位置,宽度现在是通过使用eqmakebox标签定义来计算的:

\documentclass[10pt,]{scrartcl}
\usepackage{eqparbox}
\usepackage{titletoc}
\usepackage{showframe}
\usepackage{calc}
\usepackage[hidelinks]{hyperref}

\newlength{\tocindent}
\setlength{\tocindent}{0.8em}% calculation of the indentation

\titlecontents{section}[\dimexpr \eqboxwidth{TOC}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \eqboxwidth{TOC}+\tocindent}}
{\hspace*{-\dimexpr \eqboxwidth{TOC}+\tocindent}}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsection}[\dimexpr \eqboxwidth{TOC}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \eqboxwidth{TOC}+\tocindent}}
{}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsubsection}[\dimexpr \eqboxwidth{TOC}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \eqboxwidth{TOC}+\tocindent}}
{}
{\titlerule*[8pt]{.}\contentspage}

\renewcommand\thesection{\eqmakebox[TOC][l]{\arabic{section}}}
\renewcommand\thesubsection{\eqmakebox[TOC][l]{\arabic{section}.\arabic{subsection}}}
\renewcommand\thesubsubsection{\eqmakebox[TOC][l]{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}}

\begin{document}
\startcontents[sections]
{\Large Inhaltsverzeichnis}\\
\printcontents[sections]{l}{1}{\setcounter{tocdepth}{4}}

\section{Section A}
\section{Section B}
\section*{Section B*}
\subsection{Subsection B 1}
\subsection{Subsection B 2}
\subsection{Subsection B 3}
\subsection{Subsection B 4}
\subsection{Subsection B 5}
\subsection{Subsection B 6}
\subsection{Subsection B 7}
\subsection{Subsection B 8}
\subsection{Subsection B 9}
\subsection{Subsection B 10}
\subsection{Subsection B 11}
\subsection{Subsection B 12}
\subsubsection{Subsubsection B 13.1}
\subsubsection{Subsubsection B 13.2}
\subsection{Subsection B 14}
\subsection{Subsection B 15}
\section{Section C}
\subsection{Subsection C 1}
\subsection{Subsection C 2}
\subsubsection{Subsubsection C 2.1}
\subsubsection{Subsubsection C 2.2}
\subsection{Subsection C 3}
\addcontentsline{toc}{section}{List of Figures (section)}
\end{document}

它可以起作用,但是章节标题和小节标题在普通文本中被推开了。

enter image description here

左边是根据上面的代码带有的\eqmakebox,右边没有的,\eqmakebox我希望它看起来像什么。

我也尝试过在 labeldefinition 中使用来设置宽度,\eqsetminwidthto{TOC}{\arabic{section}}但这只会导致错误。非常感谢您的建议

答案1

所以我设法自己解决了这个问题。(对我来说太好了)

我实际上\eqmakebox为每个部分组定义了并将它们添加到目录的总缩进中。因此每个标签仅在相应数字周围有一个框:

  • 1第 1 部分
  • 1.1第 1.1 节
  • 1.23.十三第 1.23.13 节

并且它不会影响文本中的章节。

\documentclass[10pt,]{scrartcl}
\usepackage{eqparbox}
\usepackage{titletoc, titlesec}
\usepackage{showframe}
\usepackage{calc}
\usepackage[hidelinks]{hyperref}

\newlength{\tocindent}
\setlength{\tocindent}{0.2em}% additional indentation

\titlecontents{section}[%
    \dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{%
    \dimexpr
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\hspace*{%
    -\dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsection}[%
    \dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{%
    \dimexpr
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\hspace*{%
    -\dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsubsection}[%
    \dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent]{}
{\contentslabel[{\thecontentslabel}]{%
    \dimexpr
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\hspace*{%
    -\dimexpr 
    \eqboxwidth{TOCS}+
    \eqboxwidth{TOCSS}+
    \eqboxwidth{TOCSSS}+\tocindent}}
{\titlerule*[8pt]{.}\contentspage}

\renewcommand\thesection{\eqmakebox[TOCS][l]{\arabic{section}}}
\renewcommand\thesubsection{\arabic{section}.\eqmakebox[TOCSS][l]{\arabic{subsection}}}
\renewcommand\thesubsubsection{\arabic{section}.\arabic{subsection}.\eqmakebox[TOCSSS][l]{\arabic{subsubsection}}}

\begin{document}
\startcontents[sections]
{\Large Inhaltsverzeichnis}\\
\printcontents[sections]{l}{1}{\setcounter{tocdepth}{4}}

\section{Section A}
\section{Section B}
\subsection{Subsection B 1}
\setcounter{subsection}{12}
\subsection{Subsection B 1}
\subsubsection{Subsubsection B 13.1}
\subsubsection{Subsubsection B 13.2}
\subsection{Subsection B 14}
\subsection{Subsection B 15}
\section{Section C}
\subsection{Subsection C 1}
\subsection{Subsection C 2}
\subsubsection{Subsubsection C 2.1}
\setcounter{subsubsection}{12}
\subsubsection{Subsubsection C 2.2}
\subsection{Subsection C 3}
\addcontentsline{toc}{section}{List of Figures (section)}
\end{document}

enter image description here

答案2

titlecontents使用专用于未编号部分(您将其留空)的论点。

无关:你应该hyperref作为最后一个包加载,只有极少数例外(最值得注意的是,cleveref应该加载之后 hyperref)。

\documentclass[10pt,]{scrartcl}
\usepackage{titletoc}
\usepackage{showframe}
\usepackage{calc}
\usepackage[hidelinks]{hyperref}
\newlength{\tocindent}

%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\settowidth{\tocindent}{2.12.2}% calculation of the indentation
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

\titlecontents{section}[\dimexpr \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \tocindent+0.5em}}
{\hspace*{-\dimexpr \tocindent+0.5em}}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsection}[\dimexpr \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \tocindent+0.5em}}
{}
{\titlerule*[8pt]{.}\contentspage}

\titlecontents{subsubsection}[\dimexpr \tocindent+0.5em]{}
{\contentslabel[{\thecontentslabel}]{\dimexpr \tocindent+0.5em}}
{}
{\titlerule*[8pt]{.}\contentspage}

\begin{document}
\startcontents[sections]
{\Large Inhaltsverzeichnis}\\
\printcontents[sections]{l}{1}{\setcounter{tocdepth}{4}}

\section{Section A}
\section{Section B}
\subsection{Subsection B 1}
\subsection{Subsection B 2}
\subsection{Subsection B 3}
\subsection{Subsection B 4}
\subsection{Subsection B 5}
\subsection{Subsection B 6}
\subsection{Subsection B 7}
\subsection{Subsection B 8}
\subsection{Subsection B 9}
\subsection{Subsection B 10}
\subsection{Subsection B 11}
\subsection{Subsection B 12}
\subsubsection{Subsubsection B 13.1}
\subsubsection{Subsubsection B 13.2}
\subsection{Subsection B 14}
\subsection{Subsection B 15}
\section{Section C}
\subsection{Subsection C 1}
\subsection{Subsection C 2}
\subsubsection{Subsubsection C 2.1}
\subsubsection{Subsubsection C 2.2}
\subsection{Subsection C 3}
\addcontentsline{toc}{section}{List of Figures (section)}

\end{document} 

enter image description here

相关内容