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.

1 Answer 1


enter image description here



\def\mymacro{\vanishprotect{HELLO }}


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

  • Thanks a lot! Would you mind to shortly explain how \vanishprotect works? Apr 25, 2013 at 21:29
  • 1
    @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 Apr 25, 2013 at 21:35
  • 1
    The space does not vanish in the table of contents (\numberline{1}\ xxx). Apr 25, 2013 at 21:59
  • @HeikoOberdiek true thanks I'll move it inside Apr 25, 2013 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .