\def\mystring{Test of math $a^{b}$, \textbf{bold} and some {\color{lightgray}coloring}}

I would like to use/know of a macro, which could accept \mystring, and return a "plain" text version of it. I would be quite satisfied with the math being dropped - however, I'd like the letters pulled out, where I'd otherwise have text typeset. Let's say we call such a macro \getPlainText - then I'd be very satisfied with something that would behave like this:

Are you looking for a general solution or is this mainly for bookmarks? The way you use it in the optional argument of \section doesn't seem to make sense - why would you want plain text section headings in the table of contents. A general solution would be very hard, for bookmarks most of the work is already done by \pdfstringdef.
–
Stephan LehmkeApr 6 '12 at 12:12

Many thanks, @StephanLehmke ; I was looking for a general solution - good to know it is hard. Haven't known about \pdfstringdef, will check it out. The optional argument of \section makes sense if you're trying to force a document to build which otherwise crashes, as in say Problems building article with oolatex and hyperref - and you wouldn't want to manually change the \sections at that point.. Cheers!
–
sdaauApr 6 '12 at 12:23

You see that it deals reasonably well with math, but stumbles a bit over \color because color is looking for an optional argument with \@ifnextchar which is not expandable. This is because \color is not explicitly handeled by \pdfstringdef.

You can however add an explicit handler like this:

\pdfstringdefDisableCommands{\def\color#1{}}

You can call this several times to "declare" other special handlers for other commands, or you can declare several things at once. However, every definition stored by \pdfstringdefDisableCommands is executed just before \pdfstringdef, so now we get