This is a modification of the question here: @namedef and math mode.
I am trying to define a macro called myLabel
that 1) defines a standard label for an equation, and 2) saves the equation so that it can be easily reprinted with a second macro called displayEquation
. The MWE is here:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xstring}
%strip any alignment characters and display as an inline equation
\newcommand{\cleanLabel}[1]{$ \StrSubstitute{#1}{&}{} $ }
\makeatletter
\newcommand\myLabel[2]{%
\label{#1}%
\global\@namedef{label@store@content@#1}{#2}%
#2}
\newcommand{\displayEquation}[1]{
\cleanLabel{%
\@nameuse{label@store@content@#1}%
}
}
\makeatother
\begin{document}
Define three equations
\begin{align}
\myLabel{simpleEq}{ f(x) &= 5x } \\
\myLabel{harderEq}{ f(x) &= \ln{5x} } \\
\myLabel{anothEq}{ \bm{a} &= \ddot{\bm{x}}}
\end{align}
Equation \ref{simpleEq} has tag simpleEq, and I can display it inline as: \displayEquation{simpleEq}
\\
Equation \ref{harderEq} has tag harderEq, and I can try to display it inline as: %\displayEquation{harderEq} %FAILS
\\
Equation \ref{anothEq} has tag anothEq, and I can try to display it inline as: %\displayEquation{anothEq} %FAILS
\end{document}
The macros work fine when I have 'simple' equations. However it seems to fail if I have any equation with a backslash character. Is there a way to modify my setup so it can work for more complex equations?