欧拉与减号

欧拉与减号

我正在尝试定义一个宏来排版欧拉复数 e-power,无论其参数中是否带有减号。我希望宏能够自动检测其参数是否以 开头-。我有以下 MWE,它使用可选参数:

\documentclass[a4paper,12pt,fleqn]{article}

\def\imaginaryunit{j}                  % the imaginary unit, i for mathematician and theoretical physicist, j for the rest of the world.
\def\imunit{\mathrm{\imaginaryunit}}   % ... in upright math
\def\ce{\mathrm{e}}                    % the constant e, upright of course
\makeatletter
\def\epowim{\@ifnextchar[{\epowimi}{\epowimi[]}}       % e to-the-power-of imaginary unit
\def\epowimi[#1]#2{\ce^{#1\if\imaginaryunit j\relax\,\fi\imunit#2}}       % e to-the-power-of imaginary unit
\makeatother

\begin{document}

\begin{equation}
\epowim{\alpha}\qquad \epowim[-]{\alpha} \qquad \ce^{-\imunit\alpha}
\end{equation}

\end{document}

所以我想要一个宏来检测它的参数是否以-:开头

\epowim{-\alpha}

应该检测-并将其放在虚数单位之前,而不是放在虚数单位之后。

所以问题是是否可以做到以及如何做到。

答案1

像这样吗?

\documentclass[a4paper,12pt,fleqn]{article}
\def\imaginaryunit{j}                  % the imaginary unit, i for mathematician and theoretical physicist, j for the rest of the world.
\def\imunit{\mathrm{\imaginaryunit}}   % ... in upright math
\def\ce{\mathrm{e}}                    % the constant e, upright of course
\newcommand\epowim[1]{\ce^{\epowimaux#1\relax\endep}}
\def\epowimaux#1#2\endep{\ifx-#1\relax-\imunit\else%
  \if j\imaginaryunit\relax\,\fi\imunit#1\fi#2}
\begin{document}
\[
\epowim{\alpha}\qquad \epowim{-\alpha} \qquad \ce^{-\imunit\alpha}
\]
\[
\epowim{x+t}\quad\epowim{-x+t}\quad\epowim{-}\quad\epowim{}
\]
\def\imaginaryunit{i}
\[
\epowim{\alpha}\qquad \epowim{-\alpha} \qquad \ce^{-\imunit\alpha}
\]
\[
\epowim{x+t}\quad\epowim{-x+t}\quad\epowim{-}\quad\epowim{}
\]
\end{document}

在此处输入图片描述

答案2

避免\def,你的生活将会更轻松。

由于您似乎了解\@ifnextchar

\documentclass[a4paper,12pt,fleqn]{article}

% the imaginary unit, j for engineers and i for the rest of the world
\newcommand\imaginaryunit{j}
% in upright type as engineers do; also Euler's constant
\newcommand\imunit{\mathrm{\imaginaryunit}}
\newcommand\ce{\mathrm{e}}

\newcommand{\fiximunit}{\if\imaginaryunit j\,\fi}

\makeatletter
\newcommand{\epowim}[1]{\ce^{\epowim@#1}}
\newcommand{\epowim@}{\@ifnextchar-{\epowim@@}{\epowim@@{\fiximunit}}}
\newcommand{\epowim@@}[1]{#1\imunit}
\makeatother

\begin{document}

\begin{equation}
\epowim{\alpha}\qquad \epowim{-\alpha} \qquad \ce^{-\imunit\alpha}
\qquad \epowim{} \qquad \epowim{-}
\end{equation}

\end{document}

解释:如果找到,则\@ifnextchar-使用下一个参数,否则使用后续参数,但是-没有删除-。因此,如果-调用结果将是

\epowim@@-\alpha

-作为参数传递给\epowim@@。否则\epowim@@以 作为参数进行调用\fiximunit

这不会因\epowim{}或分别\epowim{-}排版 e j和 e −j而失败。

一个可能更简单的实现是xparse

\documentclass[a4paper,12pt,fleqn]{article}

\usepackage{xparse}

% the imaginary unit, j for engineers and i for the rest of the world
\newcommand\imaginaryunit{j}
% in upright type as engineers do; also Euler's constant
\newcommand\imunit{\mathrm{\imaginaryunit}}
\newcommand\ce{\mathrm{e}}

\newcommand{\fiximunit}{\if\imaginaryunit j\,\fi}

\NewDocumentCommand{\epowim}{m}{\ce^{\powim#1}}
\NewDocumentCommand{\powim}{t-}{\IfBooleanTF{#1}{-\imunit}{\fiximunit\imunit}}

\begin{document}

\begin{equation}
\epowim{\alpha}\qquad \epowim{-\alpha} \qquad \ce^{-\imunit\alpha}
\qquad \epowim{} \qquad \epowim{-}
\end{equation}

\end{document}

在此处输入图片描述

相关内容