# Why does this use of etoc overwrite margins and how to get the contents correctly aligned?

I am trying to simulate default TOC using package etoc(I am happy with default TOC but I want some customizations)

I came up with the following setup (see MWE 1 and screenshot 1), but it has some problems:

• Etoc is exceeding left/right margin. How to fix this?;
• How to get section automatically aligned below the chapter label? I currently use a hspace for this (0.375cm) which I found out by experimenting, but it does not work in this MWE anymore (different font/size). How to get this always working (so no hard offset?)
• Chapter 2 alignment goes also wrong here.

How can I emulate default TOC (see MWE 2/screenshot 2) using etoc/etocsetstyle?

MWE 1:

\documentclass{extbook}
\usepackage[a4paper,left=3cm,showframe]{geometry}
\usepackage{titlesec}
\usepackage{etoc}
\etocsetstyle{chapter}
{}
{\llap{\etocnumber\hspace{0.75cm}}\etocname\hspace{10pt}\titlerule*[1pc]{.}\hspace{10pt}%
\makebox[0cm][l]{\makebox[0pt]{\etocpage}}\par}
{}
\etocsetstyle{section}
{}
{\llap{}\textit{\etocnumber\hspace{0.375cm}\etocname}\hspace{10pt}\titlerule*[1pc]{.}\hspace{10pt}%
\makebox[0cm][l]{\makebox[0pt]{\etocpage}}\par}
{}
\etocsettocstyle{\chapter*{Contents}}{}
\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{2}
\begin{document}
\tableofcontents
\chapter{Test1}
\section{Test1.1}
\section{Test1.2}
\chapter{Test2}
\section{Test2.1}
\end{document}


MWE2:

\documentclass{extbook}
\usepackage[a4paper,left=3cm,showframe]{geometry}
\begin{document}
\tableofcontents
\chapter{Test1}
\section{Test1.1}
\section{Test1.2}
\chapter{Test2}
\section{Test2.1}
\end{document}


The headings are shifted by \parindent, except the first one. The other issues are because etoc does what it is instructed to do. Here is a quick reworking of the code:

\documentclass{extbook}
\usepackage[a4paper,left=3cm,showframe]{geometry}
\usepackage{titlesec}
\usepackage{etoc}
\etocsetstyle{chapter}
{}
{\llap{\makebox[0.75cm][l]{\etocnumber}}\etocname
\hspace{10pt}\titlerule*[1pc]{.}\hspace{10pt}\etocpage\par}
{}
\etocsetstyle{section}
{}
{\llap{\makebox[1cm][l]{\textit{\etocnumber}}}\etocname
\hspace{10pt}\titlerule*[1pc]{.}\hspace{10pt}{\bfseries\etocpage}\par}
{}
\etocsettocstyle{\chapter*{Contents}}{}
\setcounter{tocdepth}{1}
\setcounter{secnumdepth}{2}
\begin{document}
\tableofcontents
\chapter{Test1}
\section{Test1.1}
\section{Test1.2}
\chapter{Test2}
\section{Test2.1}
\end{document}


I have chosen somewhat arbitrarily the left margins of the various levels (here chapter and section).

Side note: etoc documentation etoc now contains code snippets as file attachment annotations which you can view in suitable pdf viewers. Among them, there is a rather complete pseudo-emulation of the looks of the book class, which one can take as starting point for customization.

• the use of \titlerule to set the dots will possibly have to be improved. Simplest would be to remove it for chapters, using \nobreak\hfillinstead. But this is not etoc issue. – user4686 Oct 10 '16 at 7:44