这是此处问题的修改:@namedef 和数学模式。
我正在尝试定义一个名为的宏,myLabel
该宏 1) 为方程式定义标准标签,2) 保存方程式,以便可以使用第二个名为的宏轻松重新打印displayEquation
。 MWE 在这里:
\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}
当我有“简单”方程式时,宏可以正常工作。但是,如果我有任何带有反斜杠字符的方程式,它似乎会失败。有没有办法修改我的设置,以便它可以适用于更复杂的方程式?
答案1
问题是,它\StrSubstitute
完全展开了它的参数,这可能不适用于所有内容。下面使用该expl3
函数\tl_remove_all:Nn
来删除 & 符号。
\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}