[Tex/LaTex] Euler and minus sign

math-modeoptional arguments

I'm trying to define a macro to typeset Eulers complex e-power with or without a minus sign in its argument. I would like the macro to automatically detect if its argument starts with a -. I have the following MWE which uses and optional argument:

\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}

So I would like a macro that detects if its argument starts with a -:

\epowim{-\alpha}

should detect the - and place it before the imaginary unit instead of placing it after the imaginary unit.

So the question is if it can be done and how.

Best Answer

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}

enter image description here

Related Question