I want to dynamically define some macros that essentially create a list of \ref
s. I seem to be able to define these macros but then I am unable to print them.
Here is my M(non)WE:
\documentclass{article}
\makeatletter
\def\Assign#1{% if \DP@#1 is defined append to it otherwise create it
\ifcsname DP@#1\endcsname\def\DP@tmp{\csname DP@#1\endcsname,\@currentlabel}
\else\def\DP@tmp{\@currentlabel}
\fi
\expandafter\edef\csname DP@#1\endcsname{\DP@tmp}
\typeout{ASSIGN: \csname DP@#1\endcsname}
}
\def\Assigned#1{#1: \csname DP@#1\endcsname}
\typeout{ASSIGNED: \csname DP@#1\endcsname}
}
\makeatother
\begin{document}
\begin{enumerate}
\item Apples \Assign{fred}
\item Oranges \Assign{julie}
\item Bananas \Assign{fred}
\item Mangos \Assign{julie}
\item Strawberries \Assign{julie}
\end{enumerate}
\Assigned{fred} % should print 1,3
\Assigned{julie} % should print 2,4,5
\end{document}
I want the \Assigned{}
macro to print out the item numbers "assigned" to that person. That is, after the itemise
environment above I should just get:
fred: 1,3
julie: 2,4,5
Unfortunately, I am missing the item numbers and I just get:
fred:
julie:
The M(non)WE latex's without any problems but, for reasons that I don't understand, the \Assigned
macros are not expanding. I have tried many variations using \expandafter
1 or more times, \edef
, ... but nothing seems to work.
In the log file the \typeout{}
's calls above are producing:
ASSIGN: 1
ASSIGN: 2
ASSIGN: 1,3
ASSIGN: 2,4
ASSIGN: 2,4,5
ASSIGNED: `\DP@fred`
ASSIGNED: `\DP@julie`
So it looks as if the \DP@#1
's are bing created correctly and it is the \Assigned{}
macros that is not working, which is probably an expansion problem. Using \edef
doesn't work...but the fact that the expansion issue occurs INSIDE the \typeout
suggests to me that I am doing something more fundamentally wrong.
Can anyone tell me how to fix this?
Next on my wish-list would be to have it print "1,3-6,8" rather than "1,3,4,5,6,8", for example. But first things first!
`
to mark your inline code as I did in my edit. Also you don't have to sign with your name since it automatically appears in the lower right corner of your post.\typeout
's were for debugging. I gave the tick to Heiko because he explained what the problem was and how to fix it.csname
/expand
type question 'all by myself' before!