5

I'm trying to customize my TOC as in this post (second example) and I'm having two issues.

The first one is documented but I can't figure out what I'm doing wrong : I want to write "Appendix" instead of "Chapter". If I don't customize \titlecontents{chapter} then everything works fine, but if I do, I get a "Chapter" instead of "Appendix". I've tried all sorts of configs (I think it's due to the book class), with/without \begin{appendices}, loading (or not) the appendix package with the titletoc options... Any help would be very appreciated !

The second "problem" I'm having is I'm trying to customize the look of the subsections in the TOC according to the chapter they're in. Each chapter has its own color and I would like the sections to have the same color as the chapter they're in.

Here is my MWE and the result.

\documentclass[11pt]{book}
\usepackage[table]{xcolor}
\usepackage{ifthen}
\usepackage{tikz,pgf}

\definecolor{chapa}{HTML}{15CA7F}
\definecolor{chapb}{HTML}{CA16BD}
\definecolor{chapc}{HTML}{DE1841}

\makeatletter
\newcommand{\ch@p}{chapa}
\newcommand{\ch@ngeCouleurs}[1]
{
    \ifthenelse{\equal{#1}{1}}{\renewcommand{\ch@p}{chapa}}{}
    \ifthenelse{\equal{#1}{2}}{\renewcommand{\ch@p}{chapb}}{}
    \ifthenelse{\equal{#1}{3}}{\renewcommand{\ch@p}{chapc}}{}
}


\usepackage[titletoc]{appendix}
\usepackage{titletoc}
\usepackage{titlesec}

\contentsmargin{-1.5cm}
\titlecontents{chapter}[4pc]
    {\addvspace{30pt}%
    \ch@ngeCouleurs{\thecontentslabel}%
    \begin{tikzpicture}[remember picture, overlay]%
        \draw[\ch@p,fill=\ch@p] (-4.2,-.1) rectangle (-.8,.5);%
        \pgftext[left,x=-3.7cm,y=0.2cm]{\color{white}\large\bfseries\sc\@chapapp\ \thecontentslabel};%
    \end{tikzpicture}%
    \large\bfseries\color{\ch@p!80!black}}%
    {}
    {}
    {\titlerule\, Page \thecontentspage
    \begin{tikzpicture}[remember picture, overlay]
       \draw[\ch@p] (2pt,0) rectangle (6,0.1pt);
    \end{tikzpicture}%
    }
    [\addvspace{.2cm}]%

\titlecontents{section}[0pc]
    {\addvspace{5pt}}
    {
        \color{\ch@p}\contentslabel[\thecontentslabel]{2pc}\color{black}}
    {}
    {\hfill\small\color{black}\thecontentspage}
    [\addvspace{3pt}]
\makeatother

\begin{document}
\tableofcontents
\chapter{One chapter}
\section{First section}
\section{First subsection}
\chapter{Another chapter}
\section{Foo}
\section{Bar}
\begin{appendices}
    \chapter{First appendix}
\end{appendices}
\end{document}

The result (2.1 and 2.2 should be in purple, and "Chapter A" should be "Appendix A").

Result

I think I know how to work around the coloring problem of the appendix (comes from the fact that the chapter counter has been reset which can be avoided using packages totcount and assoccnt).

But I don't understand why my \ch@ngeCouleurs macro has no effect on the \titlecontents{section}. If I could access the chapter number in the \titlecontents{section}, that would solve my problem.

Thank you for reading my (long) post ! And thanks to anyone who can provide help !

Alexis
  • 439
  • I think one issue is that \ch@p isn't redefined globally -- \renewcommand is not global (unless done in global level) –  Jun 12 '16 at 11:32
  • I don't understand why you want to use assoccnt here (I am the author of assoccnt, by the way -- and switch to xassoccnt please;-) –  Jun 12 '16 at 11:40
  • I've been using it to identify whether I'm dealing with an appendix or a chapter and coloring my document accordingly. It's the only way I found to add appendices to my course. See here (no appendix because I've just worked it out this week) : http://alexisfles.ch/files/enseignement/mt11/cours.mta1.etudiant.pdf I want the colored squared to read 1/2/3/4.../A/B/C/... – Alexis Jun 12 '16 at 11:55
  • Oh, that's nice, and very colourful. How did you achieve the Proposition boxes? –  Jun 12 '16 at 12:43
  • Sorry Christian, I didn't see your comment earlier, but better late than never : you can find the style file for the boxes here : http://alexisfles.ch/en/latex/boites2.html – Alexis Sep 16 '16 at 11:05

1 Answers1

4

The \renewcommand{\ch@p} does not survive the typesetting phase of the chapter title in the ToC and as such, section ToC entries use the wrong color setting.

\renewcommand is not global (unless being specified directly in the preamble or in the document body outside of nested groups. If the colour settings should be persistent, use \gdef\ch@p instead.

Now the Appendix issue:

At the time of writing the ToC lines, \@chapapp defaults to Chapter, so even with an appendix environment, this would come too early in the ToC to change to \appendixname (i.e. 'Appendix'). This can be cured by explicitly writting the redefinition to theToC`.

It won't have an effect for later \@chapapp usages in the body, since \tableofcontents uses \@starttoc, which in turn loads the .toc file in a group.

\documentclass[11pt]{book}
\usepackage[table]{xcolor}
\usepackage{ifthen}
\usepackage{tikz,pgf}

\definecolor{chapa}{HTML}{15CA7F}
\definecolor{chapb}{HTML}{CA16BD}
\definecolor{chapc}{HTML}{DE1841}

\usepackage{titletoc}
\usepackage{titlesec}

\usepackage[titletoc]{appendix}

\makeatletter
\newcommand{\ch@p}{chapa}
\newcommand{\ch@ngeCouleurs}[1]
{%
  \ifthenelse{\equal{#1}{1}}{\gdef\ch@p{chapa}}{}
  \ifthenelse{\equal{#1}{2}}{\gdef\ch@p{chapb}}{}
  \ifthenelse{\equal{#1}{3}}{\gdef\ch@p{chapc}}{}
}


\usepackage{etoolbox}
\AtBeginEnvironment{appendices}{%
  \makeatletter
  \addtocontents{toc}{\protect\renewcommand{\protect\@chapapp}{\protect\appendixname}}
  \makeatother
}


\contentsmargin{-1.5cm}
\titlecontents{chapter}[4pc]
    {\addvspace{30pt}%
      \ch@ngeCouleurs{\thecontentslabel}%
      \begin{tikzpicture}[remember picture, overlay]%
        \draw[\ch@p,fill=\ch@p] (-4.2,-.1) rectangle (-.8,.5);%
        \pgftext[left,x=-3.7cm,y=0.2cm]{\color{white}\large\bfseries\sc\@chapapp\ \thecontentslabel};%
      \end{tikzpicture}%
      \large\bfseries\color{\ch@p!80!black}}%
    {}
    {}
    {\titlerule\, Page \thecontentspage
    \begin{tikzpicture}[remember picture, overlay]
       \draw[\ch@p] (2pt,0) rectangle (6,0.1pt);
    \end{tikzpicture}%
    }
    [\addvspace{.2cm}]%

\titlecontents{section}[0pc]
    {\addvspace{5pt}}
    {
      \color{\ch@p}\contentslabel[\thecontentslabel]{2pc}}%
    {}
    {\hfill\small\color{black}\thecontentspage}
    [\addvspace{3pt}]
\makeatother

\begin{document}
\tableofcontents
\chapter{One chapter}
\section{First section}
\section{First subsection}
\chapter{Another chapter}
\section{Foo}
\section{Bar}
\begin{appendices}
  \chapter{First appendix}
\end{appendices}
\end{document}

enter image description here

  • Works fine thanks ! Any idea about the Appendix problem (maybe I shoud have posted two questions) – Alexis Jun 12 '16 at 11:39
  • @Alexis: See the update, please –  Jun 12 '16 at 11:53
  • Thanks a lot ! The MWE works, I now need to figure out why it doesn't work in my main document :). – Alexis Jun 12 '16 at 12:42
  • @Alexis: I see. You can send it to me by mail, and I'll take a look, but probably not this day, yet!) –  Jun 12 '16 at 12:44
  • Thanks I've worked it out ! One question though : it's now written "Appendix A Appendix First appendix" (which is not the case for chapters). Is it possible to fix it ? – Alexis Jun 12 '16 at 13:23
  • 1
    @Alexis: Remove the titletoc option when loading appendix (At least, I think this is what you want to achieve: Removing the Appendix in the ToC entry line`?) –  Jun 12 '16 at 13:27
  • For the appendix issue, I finally used this : \BeforeBeginEnvironment{appendices}{% \immediate\write@auxout{\noexpand@writefile{toc}{\noexpand\gdef\noexpand@chapapp{\noexpand\appendixname}}} } as I'm using a \input. Plus, the \newcommand didn't work as expected and I had to changed it to \gdef (thanks again for the tip). – Alexis Sep 15 '16 at 19:12