如何在 renewcommand 部分之后在目录中定义点起点

如何在 renewcommand 部分之后在目录中定义点起点

由于我很懒,不想在创建具有相同前缀的部分时一遍又一遍地写相同的内容,所以我尝试了以下方法:

\documentclass[12pt,numbers=noendperiod,toc=sectionentrywithdots]{scrartcl}
\usepackage[ngerman]{babel}

%\setcounter{tocdepth}{2}

    \renewcommand\thesection{Übung \arabic{section}}
    \renewcommand\thesubsection{Aufgabe \arabic{subsection}}
    \renewcommand\thesubsubsection{\arabic{subsection}\alph{subsubsection})}

\begin{document}

    \tableofcontents
%   \newpage
    
    \section{\ }
    
    Text
    
    \subsection{\ }
    
    More text
    
    \subsection{~~~~~~~~~~~~~~}
    
    Hardcoded spaces does the job but is counterproductive
    
    \subsubsection{\ }
    
    Final text
    
    
\end{document}

它给了我在文本中想要的前缀,但它弄乱了目录中点的起始位置:

MWE 输出

那么,如何设置一个不会弄乱我的 toc-dots 的前缀?或者说:如何确保 LaTeX 知道我的前缀在哪里结束?

答案1

你可以使用

\DeclareTOCStyleEntries[
  numwidth=1em,% minimum width reserved for the entry number
  dynnumwidth% adjustes the width automatically (if more space than numwidth is needed)
]{tocline}{section,subsection,subsubsection}

或者

\RedeclareSectionCommands[
  tocnumwidth=1em,% minimum width reserved for the entry number
  tocdynnumwidth% adjustes the width automatically (if more space than numwidth is needed)
]{section,subsection,subsubsection}

例子:

\documentclass[12pt,numbers=noendperiod,toc=sectionentrywithdots]{scrartcl}
\usepackage[ngerman]{babel}

\setcounter{tocdepth}{\subsubsectiontocdepth}
\DeclareTOCStyleEntries[numwidth=1em,dynnumwidth]{tocline}{section,subsection,subsubsection}
\DeclareTOCStyleEntry[dynindent]{tocline}{subsubsection}

\renewcommand\thesection{Übung \arabic{section}}
\renewcommand\thesubsection{Aufgabe \arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{subsection}\alph{subsubsection})}

\begin{document}
\tableofcontents
%\clearpage
\section{\strut}
Text
\subsection{\strut}
More text
\subsection{\strut}
Hardcoded spaces does the job but is counterproductive
\subsubsection{\strut}
Final text
\end{document}

运行三次即可获得

在此处输入图片描述

答案2

让我们从以下 MWE 开始,其中点填充仍然与章节编号重叠:

\documentclass[12pt,numbers=noendperiod,toc=sectionentrywithdots]{scrartcl}
\usepackage[ngerman]{babel}

\renewcommand\thesection{Übung \arabic{section}}
\renewcommand\thesubsection{Aufgabe \arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{subsection}\alph{subsubsection})}

\begin{document}
    \tableofcontents
    \section{\hspace{0pt}}
      Text
    \subsection{\hspace{0pt}}
      More text
    \subsection{\hspace{0pt}}
      Hardcoded spaces does the job but is counterproductive
    \subsubsection{\hspace{0pt}}
      Final text
\end{document}

编译上述代码时,我们会收到一些警告,例如:

Package tocbasic Warning: number width of section toc entries should be increas
ed!
(tocbasic)                Currently used number width = 46.96692pt,
(tocbasic)                Wanted number separation    = 5.27994pt,
(tocbasic)                Reserved number width       = 19.80011pt on input lin
e 2.

Package tocbasic Warning: number width of subsection toc entries should be incr
eased!
(tocbasic)                Currently used number width = 52.5439pt,
(tocbasic)                Wanted number separation    = 4.69987pt,
(tocbasic)                Reserved number width       = 27.02475pt on input lin
e 3.

按照警告中的建议,我们可以使用

\RedeclareSectionCommand[tocnumwidth=50pt]{section}
\RedeclareSectionCommand[tocnumwidth=53pt]{subsection}
\RedeclareSectionCommand[tocnumwidth=17pt]{subsubsection}

以便相应地调整节、小节和小子节条目的宽度,以避免重叠和过多的空白。

完整 MWE:

在此处输入图片描述

\documentclass[12pt,numbers=noendperiod,toc=sectionentrywithdots]{scrartcl}
\usepackage[ngerman]{babel}

\renewcommand\thesection{Übung \arabic{section}}
\renewcommand\thesubsection{Aufgabe \arabic{subsection}}
\renewcommand\thesubsubsection{\arabic{subsection}\alph{subsubsection})}

\RedeclareSectionCommand[tocnumwidth=47pt]{section}
\RedeclareSectionCommand[tocnumwidth=53pt]{subsection}
\RedeclareSectionCommand[tocnumwidth=17pt]{subsubsection}

\begin{document}
    \tableofcontents
    \section{\hspace{0pt}}
      Text
    \subsection{\hspace{0pt}}
      More text
    \subsection{\hspace{0pt}}
      Hardcoded spaces does the job but is counterproductive
    \subsubsection{\hspace{0pt}}
      Final text
\end{document}

相关内容