# Generate labels based on macro argument

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.

-
What errors are popping up? –  Werner Dec 2 '11 at 15:07
I found the problem. It turns out that I indeed called the macro passing something with other macro invocations as the first argument. That was causing problems. Sorry for the trouble. For the curious, the call was \Step{R\textsuperscript{+}4.a}{blah, blah}. –  Nikola Knezevic Dec 2 '11 at 15:25

## 2 Answers

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.

-
Wow, this is great, and exactly what I was looking for. –  Nikola Knezevic Dec 2 '11 at 15:57

I'd do in a different way:

\usepackage{calc}
\newcommand{\Step}[2]{%
\par\addvspace{1.1ex}
\noindent\hspace*{1em}\fbox{\parbox[t]{\columnwidth-2em-2\fboxsep-2\fboxrule}
{\textbf{Step #1.}\ \emph{#2}}}\label{step:mode:#1}\\*[1.1ex]}


In this way the \fbox will be centered on the line and have equal space before and after it (correct the spacings, if you don't like them as they are now).

Pay attention that spaces are allowed in labels, but not accented characters or commands. So \Step{R\textsuperscript{+}4.a}{blah, blah} will surely give problems. You can solve it by saying

\newcommand{\Step}[3]{%
\par\addvspace{1.1ex}
\noindent\hspace*{1em}\fbox{\parbox[t]{\columnwidth-2em-2\fboxsep-2\fboxrule}
{\textbf{Step #2.}\ \emph{#3}}}\label{step:mode:#1}\\*[1.1ex]}


and call this problematic entry as

\Step[R+4.a]{R\textsuperscript{+}4.a}{blah, blah}


referring then to

\pageref{R+4.a}

-
I ended up coding something like this at the end. Since I have a minority of calls where I have textsuperscript, I just created another macro. Thanks. –  Nikola Knezevic Dec 2 '11 at 15:57