For different treatments, I need to have two independent main files for tables of contents using the titlesec/titletoc packages (I already use partial ToCs with the .ptc file).
In order to write the ToC-titles entries of sections in these two files, one can simply redefine the \ttl@addcontentsline macro from the titlesec package (see MWE), but this does not work for chapters.
Following the egreg's advice (somewhere here), I don't use the explicit option and I define separately the layout macros. So, I'm able to add a ToC line for chapters, but this can be done only with the mandatory argument.
For further formatting purposes, I need to access to the optional argument of the \chapter[<ToC-title>]{<Title>}. The internal mechanisms of the titlesec package are too obscure for me (shy tiny beginner with the internals) and I failed (are the \ttl@top@i, \ttl@top@ii and/or \ttl@select macros have to be considered here?).
Moreover however (may be another question?), I'm aware of the possibility to add more optional arguments to sectioning commands with titlesec (Separate sectioning titles for main text, headings, and toc for line breaking purposes), like having different headers (commented code below). Is this could be achieved for chapters too in order to have something like \chapter[<ToC-title>]{<Title>}[<Header-title>]? (cf. How to set rightmark after \section command?)
MWE
\documentclass{book}
\usepackage[margin=2cm,a4paper,showframe]{geometry}
\usepackage{titlesec,titletoc}
%\usepackage{lipsum}
\usepackage{xparse}
\makeatletter
\def\ttl@addcontentsline#1#2{% From titlesec: Works for sections but not for chapters
\addcontentsline{toc}{#1}{\ifttl@toclabel\ttl@a\fi#2}%
\addcontentsline{tdm}{#1}{\ifttl@toclabel\ttl@a\fi#2}%
\nobreak}
\newcommand\secondtableofcontents{% From book.cls
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\contentsname \ two
\@mkboth{\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
\@starttoc{tdm}% TDM stands for "Table des matières" in French
\if@restonecol\twocolumn\fi
}
%\RenewDocumentCommand{\ttl@straight@i}{m R[]{} o m}{%
% \def\@currentlabelname{#2}% for nameref
% \gdef\ttl@savemark{\csname#1mark\endcsname{#4}}%
% \let\ttl@savewrite\@empty
% \IfNoValueTF{#3}
% {\def\ttl@savetitle{#4}}% Optional #3 argument NOT supplied
% {\def\ttl@savetitle{#3}}% Optional #3 argument supplied
% \gdef\thetitle{\csname the#1\endcsname}%
% \if@noskipsec \leavevmode \fi
% \par
% \ttl@labelling{#1}{#2}%
% \ttl@startargs\ttl@straight@ii{#1}{#4}}
\makeatother
% Layout commands
\newcommand{\numberedChapterLayout}[1]{% Original complex code
\Huge\bfseries #1
\addcontentsline{tdm}{chapter}{\protect\numberline{\thechapter}\protect#1}
}
\newcommand{\numberlessChapterLayout}[1]{% Original complex code
\Huge\bfseries #1}
\newcommand{\sectionLayout}[1]{\Large\bfseries #1}
\titleformat{\chapter}[display]{}{}{0pt}{\numberedChapterLayout}[]
\titleformat{name=\chapter,numberless}[display]{}{}{0pt}{\numberlessChapterLayout}[]
\titleformat{\section}
[hang]{}{\thesection}{16pt}{\sectionLayout}% #1 implicit
[]% after the title body
\begin{document}
\tableofcontents
\secondtableofcontents
\chapter[First chapter title]{Title of the first chapter}
\section[Section A]{Section A, first chapter}
\section[Section B]{Section B, first chapter}
\chapter[Second chapter title]{Title of the second chapter}
\section[Section A]{Section A, second chapter}
\section[Section B]{Section B, second chapter}
\end{document}
\let\oldaddcontentsline\addcontentsline \renewcommand{\addcontentsline}[3]{% \def\tmp{toc}\def\ttmp{#1}% \oldaddcontentsline{#1}{#2}{#3}% \ifx\tmp\ttmp \oldaddcontentsline{tdm}{#2}{#3}% \fi}instead of\def\ttl@addcontentsline...– touhami Mar 21 '17 at 15:58