2

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?

1 Answer 1

2

The problem is that \StrSubstitute fully expands its argument, which might not work out for every content. The following uses the expl3 function \tl_remove_all:Nn to remove the ampersands instead.

\documentclass{article}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{xparse}

%strip any alignment characters and display as an inline equation
\ExplSyntaxOn
\tl_new:N \l_jpdomann_tmp_tl
\cs_new_protected:Npn \cleanLabel #1
  {
    $
    \tl_set:Nv \l_jpdomann_tmp_tl { #1 }
    \tl_remove_all:Nn \l_jpdomann_tmp_tl { & }
    \l_jpdomann_tmp_tl
    $
  }
\ExplSyntaxOff

\makeatletter
\newcommand\myLabel[2]{%
  \label{#1}%
  \global\@namedef{label@store@content@#1}{#2}%
  #2}
\newcommand{\displayEquation}[1]
  {%
    \cleanLabel{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}

enter image description here

2
  • Fantastic, that's exactly what I was looking for! Thank you.
    – jpdomann
    Jun 25, 2020 at 20:18
  • @jpdomann there is a small caveat, if the ampersand is hidden inside any other macro this will fail to remove it.
    – Skillmon
    Jun 25, 2020 at 20:19

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.