# ToC after chapter configuration

The following mwe example showcases some issues that I am currently facing. Any help would be appreciated.

\documentclass[letterpaper,11pt,openany]{book}

\usepackage[showframe]{geometry}
\usepackage{titletoc}
%--------------------------------------------------------------------------------------------------------------
% ToC formt of book is affecting mini ToC intended to go after each chapter
%--------------------------------------------------------------------------------------------------------------
\newlength\mychaplength
\settowidth\mychaplength{\bfseries\sffamily Effect on TOC\quad} % calculate width of indentation for chapter heading

\titlecontents{section}
[\mychaplength]
{}
{{\thecontentslabel}\enskip}
{}
{\hfill\contentspage}

\begin{document}
\chapter{Sample}
\noindent
\begin{minipage}{0.48\linewidth}%
\noindent
TEMP TOC~\hrulefill
{\begingroup%
\startcontents[chapters]%
\printcontents[chapters]{}{1}{\setcounter{tocdepth}{1}}%
\endgroup%
}%
\end{minipage}
\newpage
\section{Algebra, Geometry and Statistics.}
\section{Some random section that is long.}
\end{document}


In the process of creating a mini ToC for every chapter, I ran into a problem. For some reason or the other the settings I have in my main ToC are affecting the current ToC for the first chapter. Note that the settings for the main ToC are necessary for some spacing requirements I need. How can I restore/redefine or create a new settings for the mini ToC. What is necessary to be done?

On another note, is it possible to only have the section number like 1 and 2 instead of 1.1 and 1.2 and,

Is it possible to remove the page numbers and dotted line from the mini ToC?

Ultimately, I am looking for three possible configurations:

1. Restore the ToC locally to create the new mini ToC without it being affected by the main ToC. Default ToC settings.

1. Section numbering does not have the chapter numbering; 1 instead of 1.1 and,

1. Remove dotted lines and page number and leave only section number and section title.

You can redefine \titlecontents in the context of the minitoc.

\documentclass[letterpaper,11pt,openany]{book}

\usepackage[showframe]{geometry}
\usepackage{titletoc}
%--------------------------------------------------------------------------------------------------------------
% ToC formt of book is affecting mini ToC intended to go after each chapter
%--------------------------------------------------------------------------------------------------------------

\newlength\mychaplength
% calculate width of indentation for chapter heading
\settowidth{\mychaplength}{whatever} % for main toc

\titlecontents{section}[\mychaplength]
{}
{\thecontentslabel\enskip}
{}
{\hfill\contentspage}

\makeatletter
\newcommand{\myminitoc}{%
\begin{minipage}{0.48\columnwidth}
\titlecontents{section}[2em]
{}
{\thecontentslabel\enskip}
{}
{}%
% save the value of tocdepth
\chardef\savedtocdepth=\value{tocdepth}%
% redefine \numberline
\let\savedcontentsline\contentsline
\def\contentsline##1##2##3{%
\begingroup
\let\numberline\numberlinegobble
\protected@edef\x{\endgroup
\noexpand\savedcontentsline{##1}{##2}{}}\x
}%
% the minitoc proper
TEMP TOC~\hrulefill
\startcontents[chapters]
\printcontents[chapters]{}{1}{\setcounter{tocdepth}{1}}
% restore the value of tocdepth
\setcounter{tocdepth}{\savedtocdepth}
\end{minipage}\newpage
}
\newcommand{\numberlinegobble}[1]{\numberline@gobble#1\@nil}
\def\numberline@gobble#1.#2\@nil{\noexpand\numberline{#2}}
\makeatother

\begin{document}
\chapter{Sample}

\myminitoc

\section{Algebra, Geometry and Statistics.}
\section{Some random section that is long.}

\end{document}


I used a dirty trick for removing the chapter number, via a redefinition of \numberline. However titletoc redefines itself \numberline, so we need to act before control is taken by titletoc.

Note also that I saved the value of tocdepth, because \setcounter acts globally.

Some words of comment.

The package titletoc redefines \contentsline which, in turn, redefines \numberline each time. So my idea of redefining \numberline to gobble the first dot together with what precedes it didn't work. So I resorted to an indirect method.

I locally save the current definition of \contentsline in \savedcontentsline. Then I redefine \contentsline to open a group, redefine locally \numberline to do the job described above; the new definition of \contentsline will just push on the input stream the same tokens as before, with the exception of \contentsline that becomes \savedcontentsline and the argument to \numberline that will have been reduced. This way, titletoc will do its work as if nothing had happened.

How's the new \numberline defined? It becomes \numberlinegobble, so something like \numberline{1.1} becomes

\numberline@gobble1.1\@nil


that in turn will yield \noexpand\numberline{1}. This will appear in \protected@edef, so \noexpand will disappear, but will also leave \numberline untouched. It will be processed by titletoc with its own definition.

• Seems great! What would be the code if I want the 1.1 instead of 1 for section numbering? – azetina Jul 5 at 21:53
• @azetina Comment out the lines between % redefine \numberline and % the minitoc proper – egreg Jul 5 at 22:01
• Ok great. One more question? How can I fix the paragraph settings of the section definition since the word Statistics does not align with Algebra nor does long with Some? :) – azetina Jul 5 at 22:15