我正在嘗試定義一個巨集來排版歐拉複數 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}