I'm trying to associate some text (dynamically generated with counters) to a label, so that I can refer to that label and print the text associated(without re-generating it, that would cause problems with counters).
Example of usage:
\textLabel{key}{\dynamicText}
Somewhere, after, in the document i want to have a new macro that links to key and displays the text generated by \dynamictext.
This macro does not quite work:
\makeatletter
\newcommand{\textLabel}[2]{%
\protected@edef\var{\expandafter\noexpand#2}%
\label{#1}\var%
\global\expandafter\def\csname#1\endcsname{\hyperref[#1]{\var}}%
}
\makeatother
The macro \var seems to work just fine.
The problem is that the macro identified with \#1 is nor visible in the document, and produces an error (control sequence not found).
Where am I goingwrong?
Context: I have a long list of items that need an unique ID in some strange format. I'm automatically generating the ID for the items in the list, with a call to a macro that increases some counters and returns the IDs in function of the values of the counters.
Now, I want to write the IDs generated and refer to the items, without the need of recall the macro used to build them. (It would mean to call the \getID in the same order and number of times, and I don't want it)
Here a minimal (not) working example:
\documentclass[11pt]{report}
\makeatletter
\newcommand{\textLabel}[2]{%
\protected@edef\var{\expandafter\noexpand#2}%
\label{#1}\var%
\global\expandafter\def\csname #1\endcsname{\hyperref[#1]{\var}}
}
\makeatother
\newcounter{req}
\newcommand{\getid}[1]{%
\stepcounter{req}%
#1.\arabic{req}%
}
\begin{document}
\begin{itemize}
\item \textLabel{itemkeyone}{\getid{ITEM}}
\item \textLabel{itemkeytwo}{\getid{ITEM}}
\end{itemize}
\itemkeyone %I want: ITEM.1, instead produces command not found
\end{document}
Here a minimal working example, but with the wrong behaviour:
\documentclass[11pt]{report}
\usepackage{hyperref}
\newcommand{\textLabel}[2]{%
\label{#1}#2%
\global\expandafter\def\csname#1\endcsname{\hyperref[#1]{#2}}
}
\newcounter{req}
\newcommand{\getid}[1]{%
\stepcounter{req}%
#1.\arabic{req}%
}
\begin{document}
\begin{itemize}
\item \textLabel{itemkeyone}{\getid{ITEM}}
\item \textLabel{itemkeytwo}{\getid{ITEM}}
\end{itemize}
\itemkeyone %I want: ITEM.1, instead produces ITEM.3
\end{document}
Thank you very much



\documentclasscommand, have a minimal preamble and then\begin{document}...\end{document}. Unless the problem is a compilation error, the code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will! – May 06 '16 at 09:06item.3is that\getIdisn't expandable and does not have the value of the counter at the time of definition, but at the time of calling\itemkeyone(plus +1, since it's stepped then) – May 06 '16 at 12:38