Euler and minus sign
Something like this?
\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}
Avoid \def
, your life will be easier.
Since you seem to know about \@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}
Explanation: \@ifnextchar-
uses the next argument if -
is found, the successive one otherwise, but without removing -
. So if -
is called the result will be
\epowim@@-\alpha
which passes -
as argument to \epowim@@
. Otherwise \epowim@@
is called with \fiximunit
as argument.
This would not fail with \epowim{}
or \epowim{-}
to typeset ej and e−j respectively.
A perhaps simpler implementation with 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}