I don't understand completely what you're getting at here - why would you want plain text versions of your section titles in the table of contents?
And the bookmarks which are automatically generated by hyperref
are already plain text, aren't they?
Do you have a concrete example where bookmark generation goes wrong? Then one should maybe look specifically at this!
That said, the macro \pdfstring
which generates the bookmark text is absolutely the nearest you will get to your \getPlainText
from within TeX.
Let's experiment with your example:
\documentclass{article}
\usepackage{color}
\usepackage{hyperref}
\def\mystring{Test of math $a^{b}$, \textbf{bold} and some {\color{lightgray}coloring}}
\pdfstringdef\myplainstring{\mystring}
\typeout{"\myplainstring"}
This produces the output
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `math shift' on input line 11.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `superscript' on input line 11.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `math shift' on input line 11.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\@ifnextchar' on input line 11.
"Test of math ab, bold and some lightgraycoloring"
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
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `math shift' on input line 11.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `superscript' on input line 11.
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `math shift' on input line 11.
"Test of math ab, bold and some coloring"
As \color
has an expandable definition now, it can be cleanly converted.
On the whole, this still means some manual work to 'fix' all the commands you're using in section titles, but at least you don't need to clutter the sections themselves with \texorpdfstring
.
I don't think a more general solution is possible at all from within TeX, as it's nearly impossible to write a meta interpreter in TeX.
You don't need the flag: just define the macro to print the complete name and then globally redefining itself to print the abbreviated name. The global definition is needed because the first appearance might be in a group (an environment, for instance).
\documentclass{article}
\usepackage{xspace}
%% Meta-Command for defining new species macros
\newcommand{\species}[3]{%
\newcommand{#1}{\gdef#1{\textit{#3}\xspace}\textit{#2}\xspace}}
%% Defining new species
% The first argument is the name of the macro you will call in the document.
% The second argument is what is written the first time the macro is called
% The third argument is what is written every subsequent time the macro is called.
\species{\ecoli}{Escherichia coli}{E.~coli}
\species{\rsphaeroides}{Rhodobacter sphaeroides}{R.~sphaeroides}
\species{\abrasilense}{Azospirillum brasilense}{A.~brasilense}
\species{\celegans}{Caenorhabditis elegans}{C.~elegans}
\species{\pseudomonads}{Pseudomonads}{Pseudomonads}
%%
\begin{document}
\ecoli is an example of a model species. People study \ecoli because
people have studied \ecoli.
\end{document}
Best Answer
New answer
Since it sounds like you want to modify your source code, I've written a new nonanswer.
This is not something that you want to be doing from within TeX. You might be tempted to try to write a regular expression for it, but you're not going to succeed using real regular expressions.
A better solution might be to instruct your editor to do what you want. For example, in Vim, with the cursor over the
\
in\edit{...}
, entering5xma%x`ax
deletes the first 5 characters (the\edit
), marks the current location (the{
), moves to the matching}
, deletes it, moves back to the marked location and deletes it. Combined with/\\edit{
as part of a VIM macro makes deleting them pretty easy.Credit to Justin Smith's answer for the
ma%x`ax
. (Or as the edit notes,%x``x
has the same effect asma%x`ax
.)Old answer
This answer assumed you wanted to simply make
\edit
not do anything which it's now clear is not what you want.You want to (re)define
\edit
to just output its argument without change.Here's an example.
If I uncomment the
\renewcommand
line, I get the following.In this case, the two paragraphs are identical.