I am sorry for this is probably a current question with a well known answer, but I was not able to google it out.
I am trying to build a macro adding content step by step; at every step I need to pass to the macro the current content of a command. I made lots of attempts with \edef
and \expandafter
\global
, with no success. My macro always uses the content of the command which is current at the moment the macro is called, not at the moment it is created.
The question is motivated by my former question Exercises in lecture notes without predetermined placement (Follow-up) which is still unanswered. However here below I reproduce a simpler code to make this question self-contained.
\documentclass{article}
\makeatletter
\let\@mymacro\@empty%
\newcommand\myEquation[1]{%
\begin{equation}%
\label{eq:\theequation}%
#1%
\end{equation}%
\edef\EquationLabel{eq:\theequation}%
\g@addto@macro\@mymacro{%
\item There is an equation with number \ref{\EquationLabel}.
}%
}%
\newcommand\refequations{%
\subsection*{Check equations}%
\begin{itemize}%
\@mymacro%
\end{itemize}%
}
\begin{document}
\section*{Two equations}
\myEquation{1+1=2}
\myEquation{2+2=4}
\refequations
\end{document}