3

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}

enter image description here

  • 1
    don't do \immediate\write\@auxout{% use \protected@write – David Carlisle May 11 '14 at 17:35
  • @Acknowledged, I will change it – user31729 May 11 '14 at 17:36
  • No, don't change it otherwise you wouldn't have a question to answer:-) You can use the "new" \unexpanded primitive instead anyway as given in your answer – David Carlisle May 11 '14 at 17:38
  • @DavidCarlisle: I already noted, that changing leads to undefined references, but thank you very much. – user31729 May 11 '14 at 17:39
  • If you want to refer to them later on why do you need the aux file? You only need the aux file if you want to refer to things defined later. – David Carlisle May 11 '14 at 17:39
3

Oh, it is so easy, after thinking about it: The code has to be \unexpanded ;-)

\documentclass[12pt]{scrartcl}
\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}{\unexpanded{#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{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}

enter image description here

  • yes or as I mentioned in a comment latex has \protected@write (which uses the latex \protect mechanism rather than the etex system (none of the latex2e core used etex, it was too new at the time) – David Carlisle May 11 '14 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy