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.
