[Tex/LaTex] Generate labels based on macro argument

macros

I'm not proficient in LaTeX enough to solve the following. I have a macro that takes two parameters, creates a nice box, puts the first argument in bold, and the second argument in an emphasized text:

\newcommand{\Step}[2]{%
\noindent{\\[0pt] \rule{0pt}{0.5ex}%
\hspace*{1em}\fbox{\parbox[t]{0.92\columnwidth}{{\bfseries Step #1.\ }\emph{#2}}}}
    \vspace*{1.1ex} }

I call this macro 10-15 times in text (that many steps I have). In order to be able to refer to the page where this macro is expanded, I would now like to place a label. If I change the last line to:

    \vspace*{1.1ex} \label{step:mode:#1} }

errors start to pop up. I get this, on a completely unrelated line:

! Missing \endcsname inserted.
<to be read again> 
               \protect 
l.160 ...aults Occur\relax }{figure.caption.52}{}}

This line probably comes from some auxiliary file.

How to properly expand the first argument and use it as an argument to label? First argument does not have spaces, and will never have them.

Best Answer

You may redefine \textsuperscript while setting the label. Here \@firstofone would be a good redefinition. This command simply reads the argument and uses it, as it is.

An additional \csname phantomsection\endcsname may also be a good idea to have working links with package hyperref:

\documentclass{article}
\usepackage{lipsum}% for demonstration only
\usepackage{hyperref}% to show, that this works

\makeatletter
\newcommand{\Step}[2]{%
  \par\noindent{\rule{0pt}{0.5ex}%
    \csname phantomsection\endcsname
    {\let\textsuperscript\@firstofone\label{#1}}%
    \hspace*{1em}\fbox{\parbox[t]{\dimexpr \linewidth-2em-2\fboxsep-2\fboxrule\relax}{%
        {\bfseries Step #1.\ }\emph{#2}}}}\par
  \vspace*{1.1ex} 
}
\makeatother

\begin{document}
\Step{R\textsuperscript{+}4.a}{blah, blah}
See R\textsuperscript{+}4.b on page \pageref{R+4.b}.
\lipsum

\Step{R\textsuperscript{+}4.b}{blah, blah}
See R\textsuperscript{+}4.a on page \pageref{R+4.a}.
\lipsum

\end{document}

The \csname phantomsection\endcsname would simply be \relax, if hyperref is not used, so it's almost nothing.

Related Question