5

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}
7

One way is to have a helper macro so that you can easily jump forward with \expandafter to use the expansion of the macro rather than the macro itself.

\documentclass{article}

\makeatletter
\let\@mymacro\@empty%
\newcommand\myEquation[1]{%
  \begin{equation}%
    \label{eq:\theequation}%
    #1%
  \end{equation}%
  \edef\EquationLabel{eq:\theequation}%
  \expandafter\addref\expandafter{\EquationLabel}%
}%

\def\addref#1{%
 \g@addto@macro\@mymacro{%
  \item There is an equation with number \ref{#1}.
  }}

\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}
3

With LaTeX3 features it's easier to control what's expanded and what is not.

\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}

\ExplSyntaxOn
\tl_new:N \g_gherardo_items_tl

\NewDocumentCommand\myEquation{ o m }
 {
  \begin{equation}
    % the optional value is for an explicit label
    \IfValueT{#1}{\label{#1}}
    \label{eq:\theequation}
    #2
  \end{equation}
  \gherardo_add_eqn:
 }
\cs_new_protected:Npn \gherardo_add_eqn:
 {
  % everything is fully expanded
  \tl_gput_right:Nx \g_gherardo_items_tl
   {
    % but not what's inside \exp_not:n
    \exp_not:n { \item There~is~an~equation~with~number~\ref }
     {eq:\theequation}.
   }
 }

\NewDocumentCommand\refequations{ }
 {
  \subsection*{Check~equations}
  \begin{itemize}
    \g_gherardo_items_tl
  \end{itemize}
 }
\ExplSyntaxOff

\begin{document}

\section*{Two equations}
\lipsum*[2]
\myEquation{1+1=2}
\lipsum*[3]
\myEquation[mylabel]{2+2=4}

Here's the reference~\ref{mylabel}.

\refequations

\end{document}

You see that you can also add a personal label to your equations for referencing it without the need to know the equation number.

enter image description here

  • Thanks, it works! But then I should study LaTeX3! :-) – Gherardo Feb 20 '13 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.