# Correctly handle Appendix Entries in TOC using TikZ graphics

I am trying to create LaTeX TOC entries (within the report class) using simple tikz boxes to add some flare. This topic was addressed awhile back (see How to customize the table of contents using TikZ?), bowever the solutions posted there do not handle the appendix environment correctly. To try to fix this, I used \renewcommand instead of their method. I created a style file (greenBoxTOC.sty, below) that looks like:

% Style file to get green boxes in TOC.
% mtg6193@gmail.com

\usepackage{tikz}
\usepackage{xcolor}
\usepackage{titletoc}
\usepackage[titletoc]{appendix}
\usepackage{etoolbox}
\usepackage{lmodern}

% define color
\definecolor{uvmgreen}{rgb}{0,0.35,0.23}

% ToC title with color box.
\patchcmd{\tableofcontents}{\contentsname}{
\vspace*{-70pt}%
\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=12cm,y=0.2cm]{\color{uvmgreen}\Huge\bfseries\contentsname};%
\draw[fill=uvmgreen,draw=uvmgreen] (13.5,-.75) rectangle (10,1);%
\clip (15.5,-.75) rectangle (10,1);
\pgftext[right,x=12cm,y=0.2cm]{\color{white}\Huge\bfseries \contentsname};%
\end{tikzpicture}}{}{}

% ToC Entry modification -- green boxes.
\titlecontents{chapter}[0em]
{\vspace*{20pt}}%
{\hspace{80pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=uvmgreen,draw=uvmgreen] (-4,-.1) rectangle (-0.7,.5);%
\pgftext[left,x=-3.7cm,y=0.2cm]{\color{white}\Large\bfseries Chapter\  \thecontentslabel};%
\end{tikzpicture}\color{uvmgreen}\large\bfseries%
\vspace{20pt}}{}{}


And when included in a report such as the following:

\documentclass{report}

% import style file
\usepackage{greenBoxTOC}

\begin{document}

\tableofcontents

\chapter{Quantum Harmonic Oscillator}
\section{test section}
\section{test section }
\subsection{test subsection}
\section{test section}
\subsection{test subsection}
\subsection{A test subsection with a long title spanning more than one line in the table of contents}
\section{test section}

\chapter{Is it a Riemann Integral?}
\section{test section}
\subsection{test subsection}
\subsection{test subsection}

\begin{appendices}
% fix the fact that appendices show up as "Chapter (A,B..)"
\titlecontents{chapter}[0em]
{\vspace*{20pt}}%
{\hspace{80pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=uvmgreen,draw=uvmgreen] (-4,-.1) rectangle (-0.5,.5);%
%\pgftext[left,x=-3.72cm,y=0.2cm]{\color{white}\Large\bfseries Appendix    \pgftext[left,x=-3.72cm,y=0.2cm]{\color{white}\Large\bfseries Appendix\
\thecontentslabel};%
\end{tikzpicture}\color{uvmgreen}\large\bfseries%
\vspace{20pt}}{}{}

\chapter{QHO and Riemann Integrals}
\section{test section}

\end{appendices}

\end{document}


this yields the same thing as what they had except for the appendices are handled correctly. The only problem here is that the appendices don't show up correctly in the TOC unless you put the snippet of code inside of the appendices environment within the body of the document (this seems unnecessary). How would one specify different behaviour of this command depending upon whether or not we are within the appendices environment? Another problem is that whenever the implementation above is used, the TOC entry for the appendix A is listed as

Appendix A Appendix QHO and Riemann Integrals

How can you set it up to not have the word Appendix before the name of the appendix chapter?

If anyone knows how to fix either of these problems, please let me know! Thanks in advance.

• About the latter question load appendix package without the option titletoc, i.e. \usepackage{appendix}. About the former I think there is no easy solution... – karlkoeller Sep 20 '13 at 19:27
• I think that the question can be reduced to: Is there a way to write a latex function that checks which environment that you are in and performs an action accordingly? – codeAndStuff Sep 20 '13 at 20:57
• Oh, and thanks for the help on the other problem, worked right away! – codeAndStuff Sep 20 '13 at 21:16

What you want is to redefine the appendices environment so that in addition to what it normally does it also issues your commands.

There are many ways to achieve that (for example using \let and \renewcommand) but in this case I would use the handy \g@addto@macro command that appends some commands to an already defined macro, in this case we need to append your snippet to the macro \appendices

\g@addto@macro\appendices{
\titlecontents{chapter}[0em]
{\vspace*{20pt}}%
{\hspace{80pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=uvmgreen,draw=uvmgreen] (-4,-.1) rectangle (-0.5,.5);%
%\pgftext[left,x=-3.72cm,y=0.2cm]{\color{white}\Large\bfseries Appendix\
\pgftext[left,x=-3.72cm,y=0.2cm]{\color{white}\Large\bfseries Appendix\
\thecontentslabel};%
\end{tikzpicture}\color{uvmgreen}\large\bfseries%
\vspace{20pt}}{}{}}


You may wonder what does \appendices have to do with the environment with the same name. Well the calls to \begin{env} and \end{env} are actually rewritten to \env and \endenv, so by redefining \env you redefine what gets called when opening the corresponding environment.

• Sir, you have made my afternoon a little better! Thank you, it worked like a charm. – codeAndStuff Sep 20 '13 at 21:44
• Glad I could help! \g@addto@macro saved my day a couple of times as well =) – Bordaigorl Sep 20 '13 at 21:54