# Aligned TOC with titletoc

I'm trying to get my toc build with the titletoc package as I'm using it to do other content tables with it. I get it to work almost like i want it to, but i have some issues:

First of all i have do calculate the intentation manually as shown below.

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


I tried different approaches of trying to calculate the maximum width by redefining the \subsubsection command but that didn't work for me. I would love to have LaTeX calculate this value for me instead of typing it in.

The second problem I have are my unnumbered sections, they are supposed to align with the numbers. I got it to work with the paragraph definition, but that just makes so much headaches concerning the hyperref package:

• 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

Following the MWE:

\documentclass[10pt,]{scrartcl}
\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}
\end{document}


Hope you guys can help me out!

EDIT

with the help of @Bernard i've come so far thet the width is now calculated by using the eqmakebox in the labeldefinition:

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

\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}
\end{document}


It works but also the sectiontitles and subsectiontitles get pushed away in the normal text.

left according to the above code with the \eqmakebox, right without the \eqmakebox how i would like it to look like.

I also tried setting the width by using \eqsetminwidthto{TOC}{\arabic{section}} in the labeldefinition but that just results in errors. Ideas would be greatly appreciated

• I suppose that you actually don't want to have paragraphs in your table of of contents? – Bernard Dec 13 '18 at 19:40
• that's right, it is just a workaround I implemented to get the numberless entries without the indent. – celi Dec 13 '18 at 20:17

Use the argument of titlecontents devoted to unnumbered section (which you've left blank).

Unrelated: you should load hyperref as the last package, with very few exceptions (most notably, cleveref should be loaded aafter hyperref).

\documentclass[10pt,]{scrartcl}
\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}}
{\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}

\end{document}


• Thank you, that solves one Problem :) Any idea if the automated calculation of the witdh \tocindent might be possible? – celi Dec 13 '18 at 20:43
• Maybe inserting the subsubsection labels in an \eqmakebox command, and using the\eqboxwidth command? Take a look at the eqparbox package documentation. – Bernard Dec 13 '18 at 20:56

So i managed to fix my problem myself. (yay good for me)

I actually defined the \eqmakebox for each sectiongroup and added them to a total indentation for the TOC. Therefore each Label only has a box around the corresponding number:

• 1 Section 1
• 1.1 Section 1.1
• 1.23.13 Section 1.23.13

And it does not affect the sections in the Text.

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

\newlength{\tocindent}

\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}