TransWikia.com

How to define dot startpoint in TOC after renewcommand sections

TeX - LaTeX Asked by Katschka on March 14, 2021

As I am lazy and do not want to write the same stuff again and again while creating sections which have all the same prefix I tried the following:

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

%setcounter{tocdepth}{2}

    renewcommandthesection{Übung arabic{section}}
    renewcommandthesubsection{Aufgabe arabic{subsection}}
    renewcommandthesubsubsection{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}

It gives me the wanted prefix in the text, but it messes up the starting position of my dots in the table of contents:

MWE output

So, how do I set a prefix which doesn’t mess up my toc-dots? Or i.e.: How do I make sure LaTeX knows where my prefix ends?

2 Answers

You could use

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}

or

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}

Example:

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}

renewcommandthesection{Übung arabic{section}}
renewcommandthesubsection{Aufgabe arabic{subsection}}
renewcommandthesubsubsection{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}

Run three times to get

enter image description here

Correct answer by esdd on March 14, 2021

Let's start with the following MWE, in which the dotfill still overlaps with the section numbers:

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

renewcommandthesection{Übung arabic{section}}
renewcommandthesubsection{Aufgabe arabic{subsection}}
renewcommandthesubsubsection{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}

Upon compiling the aforementioned code, we end up with some warnings such as:

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.

Following the advice from the warnings, we can use

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

in order to adjust the widths of the section, subsection and subsubsection entries accordingly in order to avoid overlap and excessive white space.

Full MWE:

enter image description here

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

renewcommandthesection{Übung arabic{section}}
renewcommandthesubsection{Aufgabe arabic{subsection}}
renewcommandthesubsubsection{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}

Answered by leandriis on March 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP