TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When defining a new section or when adding a caption to a figure or a table, the content of that command is normally duplicated. One version is put directly into the text, i.e. as a heading or below a graphic, and one version often appears in a list, i.e. in the table of content or in the list of figures.

If both versions should be differently, the necessary commands have an optional argument which can be used to define different content for theses lists than for the normal text, e.g. \section[list content]{text content}. Sometimes I use a macro in the text content, which should not be part of the list content. Of course, I can always duplicate the text content and add a list content without the macro, e.g. \section[content]{\mymacro content}, when I do that in my own document. But as I need the functionality for a package, the users cannot (and should not) be forced to do that work themselves, thus I wonder how to avoid this.

The hyperref package has a macro \texorpdfstring which solves a very similar problem by putting different content into the PDF's table of content (bookmarks) than into the file. There also is the macro pdfstringdefDisableCommands, which gives a possibility to remove macros from the content. Redefining all the \section and caption commands to (locally) redefine \mymacro to an empty macro is probably not very practical, as there are so many commands. What would a good solution to the problem look like? Is there already a package or a command which removes macros from such lists?

As a motivation: I want to avoid that the macro gets executed twice. I also want to avoid that the macro is executed when loading the aux file during the next run. A solution is correct, imho, if the macro does not appear in files like .aux, .lof and .toc.

share|improve this question
up vote 4 down vote accepted

enter image description here



\def\mymacro{\vanishprotect{HELLO }}


\section{\mymacro xxx}
\section{\mymacro xxx}

share|improve this answer
Thanks a lot! Would you mind to shortly explain how \vanishprotect works? – Patrick Häcker Apr 25 '13 at 21:29
@MMM whenever \protect is doing anything non trivial (eg writing to a toc file) \protect is not \@typeset@protect so when writing to a file \vanishprotect expands to \@gobble so the construct expands to nothing. When typesetting the header \protect is \@typeset@protect so the construct expands to its argument and HELLO gets typeset – David Carlisle Apr 25 '13 at 21:35
The space does not vanish in the table of contents (\numberline{1}\ xxx). – Heiko Oberdiek Apr 25 '13 at 21:59
@HeikoOberdiek true thanks I'll move it inside – David Carlisle Apr 25 '13 at 22:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.