# Titlesec creates entry in ToC for a starred part

I have the following code, creating section titles starting with "Section [number].", exactly as I wish:

\documentclass{article}
\usepackage[explicit]{titlesec}
\newcommand\sectiontext{}
\titleformat{\section}%
{\refstepcounter{section}%
\renewcommand\sectiontext{Section \thesection. #1}%
\Large\bfseries}
{}
{0pt}
{\sectiontext}
\begin{document}
\tableofcontents
\section*{A section}
\section*{Another section}
\end{document}


(The macro sectiontext is used for clarity.) Also in the table of contents the sections are displayed as I want (except for the Contents itself, but I know how to fix that), and hyperref creates exactly the right links:

% Contents entry for the contents itself left out
\contentsline {section}{Section 2. A section}{1}{section.2}
\contentsline {section}{Section 3. Another section}{1}{section.3}


(Don't mind the Contents entry, it is numbered as the first section.) Note that starred sections must be used. Summarizing, there is no problem here.

A problem arises when I use a completely analogous code for parts instead of sections:

\documentclass{article}
\usepackage[explicit]{titlesec}
\newcommand\parttext{}
\titleformat{\part}%
{\refstepcounter{part}%
\renewcommand\parttext{Part \thepart. #1}%
\Huge\bfseries}
{}
{0pt}
{\parttext}
\begin{document}
\tableofcontents
\part*{A part}
\part*{Another part}
\end{document}


Using this, the ToC file becomes:

% Contents entry for the contents itself left out
\contentsline {part}{Part I. A part}{1}{part.1}
\contentsline {part}{A part}{1}{part.1}
\contentsline {part}{Part II. Another part}{1}{part.2}
\contentsline {part}{Another part}{1}{part.2}


As you can see, titlesec creates an additional entry for each part. (This is also the case when I don't load hyperref.)
This is really strange, because it is almost the same code as for sections. How can I remove those entries?

• \part is understood, and defined, to be at a "higher" level than \section, so it will automatically be included in the toc for any \tocdepth greater than 1. what you should consider instead is using \subsection instead of \part, but changing the text of the heading to read "Part" if you want that, or omit that name if you don't. – barbara beeton Sep 5 '14 at 19:11

The main reason here is that titlesec just handles \parts differently to all other sectional units within a document. It sets up a separate handler to manage the titles and ToC-related entries, always writing the latter for \part. As such, your "after code" which adds the ToC-related entry for \part is followed by titlesec writing it as well, hence the dual entries in your ToC.

How do you obtain the output you're after? Patch \ttl@part@i (with the aid of etoolbox) to insert your \parttext as part of its natural ToC-writing and drop the manual writing:

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\ttl@part@i}% <cmd>
{{#1}{#3}}% <search>
{{#1}{Part \thepart. #3}}% <replace>
{}{}% <success><failure>
\makeatother

\newcommand\parttext{}
\titleformat{\part}%
{\refstepcounter{part}\Huge\bfseries}
{}
{0pt}
{\parttext}
[]

\newcommand\sectiontext{}
\titleformat{\section}%
{\refstepcounter{section}%
\renewcommand\sectiontext{Section \thesection. #1}%
\Large\bfseries}
{}
{0pt}
{\sectiontext}

• Thanks. There is one more issue. I wish the very last part not to be numbered, i.e. have a title 'Title' and not 'Part IV: Title'. I tried to use {{#1}{\iftoggle{partnum}{Part \thepart. }{}#3}} instead of {{#1}{Part \thepart. #3}} (partnum is toggled false right before that last part), but this doesn't work, at least not for the ToC. Perhaps I should put a \edef or \expandafter or something before \iftoggle? – Schneider Sep 6 '14 at 9:36
• Never mind, I used \patchcmd again right before that part. It's an ugly solution but I only have to do it once, so I can live with that. – Schneider Sep 6 '14 at 11:13