# Macro to be used in a \section command that has no effect in the table of contents

I need a macro that is executed only in the main document (within a section title) and then disappears (for the table of contents).

A minimal example where this occurs is this:

\documentclass[12pt]{memoir}
\usepackage{fixltx2e}[2006/09/13]
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{txfonts}

\begin{document}

\tableofcontents

\section{The only section}

Text.

\section{The only section [\textsl{continued}]}

More text.

\section{The only section [\textsl{continued\hspace{0.13ex}}]}

An attempted fix.

\end{document}


The first section title with "[continued]" in it is badly kerned. The manual adjustment (\hspace{0.13ex}) does the job, but it unfortunately leads to too much space in the table of contents. I would like the extra space to apply only to the actual section heading in the document's body. (I tried every conceivable combination of \/, \hspace, and \protect; nothing works.)

The culprit (what causes kerning to disappear) is clearly the loading of txfonts, which I need elsewhere in my document. (Yep, I need txfonts instead of newtxtext/newtxmath for an obscure reason having to do with font loading order I'd rather not get into, and circumventing the issue underlying that would be for another question anyways. And my sample code makes for a useful example.) If I omit txfonts, the problem disappears, meaning I don't need to insert a manual space in the first place. (This might point to another problem.)

Related:

-
for this particular problem, couldn't you use \section[<stuff for the toc>]{<Actual section heading>}? –  cmhughes Feb 22 '13 at 18:22
@cmhughes That works! Except I need to enclose the optional argument in a group ({ }) in addition to [ ] for the first ] not to terminate the optional argument ... Is this quirk documented anywhere? –  Lover of Structure Feb 22 '13 at 18:44
@tohecz The text to be used as the optional (as well as the non-optional) argument contains a ], and this character is not supposed to function as the optional argument terminator but as a matching square bracket/parenthesis. Enclosing this in a group works, but I don't know whether doing this has other side effects. There is no (obvious) way other than writing [{ }] because [ and ] are not normally escaped. That [ and ] are normal characters and bear syntactic function (as optional argument delimiters) creates this potentially problematic situation. –  Lover of Structure Feb 22 '13 at 19:01
@tohecz See this question about escaping ], btw. –  Lover of Structure Feb 22 '13 at 20:16

For this particular problem, you might use

\section[<stuff for the toc>]{<Actual section heading>}

-
+1 Best and most concise answer. Credit goes to the answers by users "tohecz" and "Stephen" as well for addressing other important things I have asked about. –  Lover of Structure Feb 22 '13 at 20:46

A bit different approach: we write \dcorr to the .toc file, but we \let it \relax during the \tableofcontents:

\documentclass[12pt]{memoir}
\usepackage{fixltx2e}[2006/09/13]
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{txfonts}

\DeclareRobustCommand{\dcorr}{\hspace*{0.13ex}}

\begin{document}

\begingroup
\let\dcorr\relax
\tableofcontents
\endgroup

\section{The only section}

Text.

\section{The only section [\textsl{continued}]}

More text.

\section{The only section [\textsl{continued}\dcorr]}
% If one keeps \dcorr within the \textsl macro,
% one needs either a larger value (0.23ex works)
% or one needs to put an italic correction (\/)
% at the beginning of the definition of \dcorr.

An attempted fix.

\end{document}

-
Either put the italic correction \/ at the beginning of \dcorr definition, or move \dcorr outsite the \textsl group. –  yo' Feb 22 '13 at 19:27
+1 for solving the OP's real problem. –  Stephen Feb 22 '13 at 20:06

Like tohecz wrote: "this is sort-of a XY Problem". His answer solves the OP's real problem, but to answer the question for a "macro that is executed only the first time and then disappears":

\documentclass{article}

% method 1:
\newcommand{\mnc}{example%
\renewcommand{\mnc}{}%
}

% method 2:
\def\mnd{elpmaxe%
\gdef\mnd{}%
}

\begin{document}

A\mnc B\mnd C
% output: AexampleBelpmaxeC

D\mnc E\mnd F
% output: DEF

A\mnc B\mnd C
% output: ABC

\end{document}


(Put {} behind \mnc and \mnd if the respective spaces shall be preserved.)

-
This does it! But it's not the opposite of \protect, because of the third line (which I've added to the example). Btw, if someone posts a different question about this (since I've changed this question's title and wording), it might make sense for you to migrate your answer there later on. I can ask this new question, but someone else might be a better person for this job, as I don't know \protect particularly well. –  Lover of Structure Feb 22 '13 at 20:26
@LoverofStructure: Yes, it is no "\unprotect", sorry. With one question (the original one), you mentioned three problems: 1. \unprotect, 2. execute and disappear, 3. the question as it is now. That is interesting, but you are right that three questions (and answers) would be understood more easily. Well, the answer lingering here doesn't do any harm, and maybe it helps somebody following an old link to the original question. –  Stephen Feb 22 '13 at 20:36
Update: people have reinterpreted a question of mine again, so I think it's fair to say that your answer is safe to be also posted (or moved) here. –  Lover of Structure Feb 22 '13 at 23:07
@LoverofStructure Peter Grill already posted a similar answer (but a more complete one) to your new question, thus there is no need to repost my short answer over there. But definitively your comment with the link to that question & answer should be kept here, too. –  Stephen Feb 23 '13 at 20:32