I stumbled this morning, when I tried to improve (a possible) solution answer I gave to some question.
I try to write automatically generated labels (using a unique naming scheme with prefixes etc.) with text (or even mathematical content etc) to the .aux file and refer to it later on, using the label name, via \nameref* from nameref.
The MWE below is reduced to the basic problem, I created a small command \WriteMyLabel doing the job.
However, I can not write something like
\WriteMyLabel{\textbf{some bold text}}
LaTeX provides the very fascinating error message
\text@command #1->\def \reserved@a { #1}\ifx \reserved@a \@empty \let \check@...
How can I cope around this problem? Is it possible at all?
\documentclass[12pt]{article}
\usepackage{blindtext}%
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{hyperref}
\newcounter{TotalLabelCounter}
%%%% A command to make the references clearer, just for debugging, not in
%%%% end production code
\newrobustcmd{\refcommand}[1]{\fbox{\textbf{\textcolor{blue}{\nameref*{#1}}}}}
\makeatletter
\newrobustcmd{\WriteMyLabel}[1]{%
\refstepcounter{TotalLabelCounter}%
\immediate\write\@auxout{%
\string\newlabel{MyLabel::\number\value{TotalLabelCounter}}{{\thesection}{\thepage}{#1}{}}
}% End of writing to AUX file
}%
\makeatother
\begin{document}
\section{First}
\WriteMyLabel{The Three Witches}
\WriteMyLabel{$E = mc^2$}
\blindtext[1]
\newpage
\section{Another section}
\blindtext[1]
\WriteMyLabel{\textbf{some bold text}}
\refcommand{MyLabel::1} from the play 'Macbeth' have a very important part, however, there are no nice formulas such as \refcommand{MyLabel::2} and
it would be better, if \refcommand{MyLabel::3} would work.
\end{document}


\immediate\write\@auxout{%use\protected@write– David Carlisle May 11 '14 at 17:35\sectionto store the section names as a label, together with a total section counter, so that I can provide 'named' links to previous or next sections, see for example (the yet not perfect) solution to this question http://tex.stackexchange.com/questions/175054/macro-to-automatically-save-section-name-of-previous-section-and-next-section/175146#175146 please. – May 11 '14 at 17:42