10

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.

8

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

  • 1
    Exactly what i need. I changed the definition to \def\epowimaux#1#2\endep{\ifx-#1\relax-\imunit\else\if\imaginaryunit j\relax\,\fi\imunit#1\fi#2} to compensate for the fact that the j is too close to the e but not when using i as imaginary unit – Jesse op den Brouw Feb 21 at 20:18
  • @JesseopdenBrouw Thanks. And feel free to adapt to your need. – Steven B. Segletes Feb 21 at 20:23
  • @JesseopdenBrouw I might only suggest using \if j\imaginaryunit rather than \if\imaginaryunit j. Or better still, \expandafter\ifx\imaginaryunit j – Steven B. Segletes Feb 21 at 20:29
  • I see nothing wrong in \if\imaginaryunit j, provided \imaginaryunit is either i or j. Oh, and did you try \epowim{-}? ;-) – egreg Feb 21 at 22:57
  • @egreg I detect no problems with \epowim{-}. As to the \if test, you are right if the choices are always i or j. But users have a tendency to do strange stuff, like something that expands to i or j, or a different letter or symbol altogether, etc. I was trying to head off crazy stuff. – Steven B. Segletes Feb 22 at 0:29
7

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}

enter image description here

  • Nice answer. Nice comment too about mathematicians and all that... – Jesse op den Brouw Feb 22 at 17:47
  • @JesseopdenBrouw You provoked me. ;-) – egreg Feb 22 at 18:11
  • Yes, I did... ;-) – Jesse op den Brouw Feb 23 at 5:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.