# How can I temporarily change title of contents of subsection formatting using titletoc

I have a common document type (grant application) which expects listed aims, usually enumerated as Aim 1, Aim 2, etc. In the MWE below, an article class works fine to format the subsection headings appropriately within the document, but I'm having trouble with the TOC. I'm changing \thesubsection and using \titleformat from the titlesec package in anticipation of using the titletoc package to change the toc formatting of entries from within a defined aims environment. I want the aims to be listed in the TOC as Aim 1, Aim 2, etc.

What I'm running into:

1. The \titlecontents settings I set as part of the aims environment appears to persist after the end of the environment. This is different from the \titleformat settings, which are local.
2. Unlike the \titleformat settings in the titlesec package, the default \titlecontents in titletoc are not listed in the documentation, so my attempt to alter the default settings is trial and error (at this point, all error, since I can't get it working). The titlesec examples are invaluable; are there similar examples for titletoc?

MWE follows

\documentclass[11pt]{article}
\usepackage {blindtext}
\usepackage {titlesec}
\usepackage {titletoc}

\newenvironment {aims} {
\renewcommand{\thesubsection}{{\arabic{subsection}}}
\titleformat{\subsection}{\normalfont\large\bfseries}{Aim
\thesubsection}{1em}{}
% example from titletoc docs to show that \titlecontents
% changes persist outside the group
\titlecontents{subsection} [3.8em]
{}{\contentslabel{5em}}{\hspace*{-5em} Aim }
{\titlerule*[1pc]{.}\contentspage}
}{}
\title {A document}
\author{me}
\begin{document}
\maketitle
\tableofcontents

\section {Introduction}
\blindtext
\begin {aims}
\section {Aims}
\subsection {This is an aim}
\blindtext
\subsection {This is another aim}
\blindtext
\end {aims}
\section {Details}
\blindtext
\subsection {Some more details}
\blindtext
\end{document}

-
Remember that the ToC is set at the beginning of the document (outside your aims environment). So, of course, modifying \titlecontents would have an effect outside the aims environment. Why not create a different macro for your aims so that you can treat them differently in the ToC? – Werner Jan 7 '13 at 6:28
@Werner, thanks. I was initially headed down that path, but for some reason (my memory is fuzzy here) changed my mind. I suppose it's because I went this way that I was longing for some examples of the default \titlecontents that I could slot in at the closing of the aims environment. Even if I created a different macro for aims, rather than temporarily hijacking the \subsection formatting, I'd still use the examples if I had them. – GTK Jan 7 '13 at 6:48

Here is a mild implementation of \aims that duplicates the behaviour of \subsection from article.cls:

\documentclass[11pt]{article}
\usepackage {blindtext}% http://ctan.org/pkg/blindtext

\makeatletter
% Taken from article.cls's \subsection and introduced it as \aim
\newcounter{aim}
\renewcommand{\theaim}{\arabic{aim}}
\newcommand\aim{\@startsection{aim}{2}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\large\bfseries \hspace*{-\parindent}Aim~}}
\newcommand{\l@aim}{\@dottedtocline{2}{1.5em}{2.3em}}
\newcommand{\aimmark}[1]{}% You don't need any header marks for aims
\makeatother

\title {A document}
\author{me}
\begin{document}
\maketitle
\tableofcontents

\section {Introduction}
\blindtext

\section {Aims}
\aim {This is an aim}
\blindtext
\aim {This is another aim}
\blindtext

\section {Details}
\blindtext
\subsection {Some more details}
\blindtext
\end{document}


Using \aim instead of \subsection seems more intuitive. Of course, it would also be possible for format the "Aim" headings differently, if needed; both in the text/document, as well as in the ToC. I've just used the default \subsection style (which is \dottedtocline, at depths equivalent to \subsection).

-