I want to use markup commands such as \pkg{classicthesis} that not only typeset the entry, but that have side-effects such as adding a margin note or index entry. (See the example below.)
My problem is that I would like to be able to use these in sectioning commands \chapter{The \pkg{classicthesis} Package}, but doing so sends the extra stuff to the .toc file as well, which can cause problems when the table of contents is formatted.
Question: How can I clean everything but the text (and formatting information) from what is sent to the .toc file?
I know that I could do something like \chapter[The \pkg*{classicthesis} Package]{The \pkg{classicthesis} Package} where I define the starred form to omit the extra info, but this means unnecessarily duplicating the contents.
Note: I am doing this for a class and have no problem redefining \chapter etc. to do the cleaning, I just need the internals.
Here is a MWE. In the second case, the index command appears both in the section header and in the ToC: the latter introducing an undesired entry in the index and the marginpar also appears in the ToC. There are usually workarounds for each individual issue (in this case the scrindex package solves the index problem and one could disable \marginpar in the toc), but I would still like a general solution.
\documentclass{scrbook}
\usepackage{makeidx}
%\usepackage{scrindex} % This will fix the index issue.
\usepackage{suffix}
\usepackage{makerobust}
\usepackage{classicthesis}
\newcommand*{\pkg}[1]{\texttt{#1}\marginpar{\texttt{#1}}\index{environments!#1@\texttt{#1}}}
\WithSuffix\newcommand\pkg*[1]{\texttt{#1}}
\MakeRobustCommand\pkg
\makeindex
\begin{document}
\tableofcontents
\chapter{The Problem}
\section[The \pkg*{classicthesi} Package]{The \pkg{classicthesis} Package}
This works, but is error-prone and violates the DRY principle.
\section{The \pkg{classicthesis} Package}
This fails because the indexing commands are sent to the .toc file.
\printindex
\end{document}