我觉得更自然地引用\Phi
,,,\alpha
\sim
ETC。外部数学模式与内部数学模式相同。IE写作:
Consider \Phi, \alpha{} and \sim{} in the following equation:
\begin{equation}
\Phi \sim \alpha
\end{equation}
代替:
Consider $\Phi$, $\alpha$ and $\sim$ in the following equation:
\begin{equation}
\Phi \sim \alpha
\end{equation}
我的幼稚方法是用它们自己的变体覆盖我需要的所有宏,如下所示这个帖子, 和:
\let\NPhi\Phi % native phi
\def\Phi{\ensuremath{\NPhi}}
\let\Nalpha\alpha % native alpha
\def\alpha{\ensuremath{\Nalpha}}
\let\Nsim\sim
\def\sim{\ensuremath{\Nsim}}
..
但这很繁琐。我该如何循环呢?这样:
\magiccommand{
Phi,
alpha,
sim,
..}
会产生同样的结果吗?
答案1
您所需要的只是一个逗号分隔值的解析器。以下示例使用\comma@parse
包kvsetkeys
。宏会自动删除值开头和结尾的空格。
\documentclass{article}
\usepackage{kvsetkeys}
\makeatletter
\newcommand*{\EnsureMathDef}[1]{%
% Throw error, if "\N#1" is already defined.
\expandafter\@ifdefinable\csname N#1\endcsname{%
% Save old meaning
\expandafter
\let\csname N#1\expandafter\endcsname
\csname #1\endcsname
% Define new macro
\expandafter\edef\csname #1\endcsname{%
\noexpand\ensuremath{%
\expandafter\noexpand\csname N#1\endcsname
}%
}%
}%
}
\comma@parse{
Phi,
alpha,
sim,
}\EnsureMathDef
\makeatother
\begin{document}
Consider \Phi, \alpha{} and \sim{} in the following equation:
\begin{equation}
\Phi \sim \alpha
\end{equation}
\end{document}
无附加包装
宏内核提供\@for
。必须对行尾进行注释以避免出现多余的空格。
\documentclass{article}
\makeatletter
\@for\x:=%
Phi,%
alpha,%
sim%
\do{%
% Throw error if macro with prefix N is already defined.
\expandafter\@ifdefinable\csname N\x\endcsname{%
% Save old meaning
\expandafter
\let\csname N\x\expandafter\endcsname
\csname\x\endcsname
% Define new macro
\expandafter\edef\csname\x\endcsname{%
\noexpand\ensuremath{%
\expandafter\noexpand\csname N\x\endcsname
}%
}%
}%
}
\makeatother
\begin{document}
Consider \Phi, \alpha{} and \sim{} in the following equation:
\begin{equation}
\Phi \sim \alpha
\end{equation}
\end{document}