Take the 2-minute tour ×
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.

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
add comment

1 Answer

up vote 4 down vote accepted

enter image description here

\documentclass{article}

\makeatletter
\def\vanishprotect{\ifx\protect\@typeset@protect
\expandafter\@firstofone\else
\expandafter\@gobble\fi}
\makeatother

\def\mymacro{\vanishprotect{HELLO }}


\begin{document}
\tableofcontents

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

\end{document}
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
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 –  David Carlisle Apr 25 '13 at 21:35
1  
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
add comment

Your Answer

 
discard

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.