1

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}
1
  • try \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
1

Instead of redefine \ttl@addcontentsline one can redefine the original \addcontentsline

\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\oldaddcontentsline{#1}{#2}{#3}%
\def\tmp{toc}\def\ttmp{#1}%
\ifx\tmp\ttmp
\oldaddcontentsline{tdm}{#2}{#3}%
\fi}

this way it work for chapters and sections.

2) One can patch \@chapter command to acces

\usepackage{etoolbox}
\patchcmd\@chapter{\chaptermark{#1}}{\chaptermark{#1}\def\@currentlabelname{#1}}{}{}

\documentclass{book}
\usepackage[margin=2cm,a4paper,showframe]{geometry}
\usepackage{titlesec,titletoc}
%\usepackage{lipsum}
\usepackage{xparse}

\let\oldaddcontentsline\addcontentsline
\renewcommand{\addcontentsline}[3]{%
\def\tmp{toc}\def\ttmp{#1}%
\oldaddcontentsline{#1}{#2}{#3}%
\ifx\tmp\ttmp
\oldaddcontentsline{tdm}{#2}{#3}%
\fi}

\makeatletter
\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
    }
\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}
3
  • Many thanks for your answer. That works fine with the MWE, but still not with my real case for the moment. I have to investigate more in deep... Thanks also for the macro to patch (it was relatively logical...)
    – ejazz
    Mar 21 '17 at 17:16
  • @ejazz you're welcome :-). I am sorry i can't answer what i can not see. You should know that titlesec changes only makechapterhead
    – touhami
    Mar 21 '17 at 17:25
  • After verification, your solution works very efficiently (silly I am: an obvious wrong placement of macros with hyperref). So, fully accepted! Thanks again.
    – ejazz
    Mar 23 '17 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.