[Tex/LaTex] How to remove section indentation in partial ToC using titletoc

indentationtable of contentstitletoc

See MWE below.

How can I remove the indent in front of the section entries in the partial ToC (only there, not in the normal ToC)?

\documentclass{book}
\usepackage{titletoc}
\usepackage{lipsum}
\begin{document}
\setcounter{tocdepth}{2}  
\tableofcontents
\chapter{A chapter}
\startcontents[chapters]
\printcontents[chapters]{}{1}{}
\section{Section}
\subsection{Subsection}
\lipsum[4]
\subsection{Another Subsection} 
\lipsum[1]
\section{Section 2}
\lipsum
\chapter{Second chapter}
\startcontents[chapters]
\printcontents[chapters]{}{1}{}
\section{Section}
\lipsum[2]
\section{Another section}
\lipsum
\end{document}

This question is related to this answer.

Best Answer

In order to do this, you need to create a separate format for the partial TOCs by using titletoc's prefix system.

Creating separate formatting for the partial TOCs

First we create the format for a psection instead of a section using the regular \titlecontents command. This can be formatted however you like. To make it flush with the left margin we make the left margin 2.3em (same width as the space set up for the section number). Then when we use the \printcontents command to produce the partial contents we use the {p} prefix argument to tell titletoc to use the format defined for psection instead of section. You need to determine the formatting for the subsections in the same way if you have subsections. I've removed some extra space that I added before.

Here's an example:

\documentclass[oneside]{book}
\usepackage{titletoc}
\usepackage{lipsum}

\setcounter{tocdepth}{2}  
\begin{document}
\titlecontents{psection}[2.3em]
{} {\contentslabel{2.3em}} {} {\titlerule*[1pc]{.}\contentspage}
\titlecontents{psubsection}[5.5em]
{} {\contentslabel{3.2em}} {} {\titlerule*[1pc]{.}\contentspage}

\tableofcontents
\chapter{A chapter}
\startcontents[chapters]
\printcontents[chapters]{p}{1}{}
\section{Section}
\lipsum[1]
\subsection{A subsection}
\lipsum[3]
\subsection{A second subsection}
\lipsum[4]
\section{Section 2}
\lipsum
\chapter{Second chapter}
\startcontents[chapters]
\printcontents[chapters]{p}{1}{}
\section{Section}
\lipsum[2]
\section{Another section}
\lipsum
\end{document}

partial output of code

Choosing the correct spacing values

The values you choose to format the margins for the various levels can be set by hand. The default values used by the book class are the following. These values are those passed to the \@dottedtocline command within the internal sectioning command definitions. They were obtained from the source code of book.cls.

   Level       Indent  Label width

section         1.5em     2.3em
subsection      3.8em     3.2em
subsubsection   7.0em     4.1em
paragraph      10.0em     5.0em  
subparagraph   12.0em     6.0em

You can use these values to determine the values for the \titlecontents parameters for the various sections. Since our partial TOCs don't have a chapter level, we start the left margin at the width of the label for section (2.3em) and then use the same value for the label width. To make the subsections indent exactly the same amount as in the main TOC relative to the section, we make the left margin = previous left margin + current label width, i.e. 5.5em. Lower levels proceed similarly.