# How to define a command \exponent that transforms $\exponent^e$ to $a^eb^ec^e$?

I have tried to define a command $\exponent$ that would look for next exponent and would put a copy of this exponent after a, another after b and a last one after c.

$\exponent^e$ would transform to $a^e b^e c^e$.

And if this is possible, I would like $\exponent^e_f$ and $\exponent_f$ to generate $a^e_f b^e_f c^e_f$ and $a_f b_f e_f$.

The difficult part is to make $\exponent^\frac12$ be automatically replaced by $a^\frac12 b^\frac12 c^\frac12$, but comments are suggesting that ^\frac12 is forbidden in TeX.

• Are a, b, c hard-coded? If so, what you seem to want to do is to grab the argument of ^ and/or _ and add it to a pre-defined list: is that correct? – Joseph Wright Dec 4 '13 at 8:20
• They are hardcoded only to simplify the answers. I will be then able to adapt the answers. I edited my post, looking for the argument of ^ and _ is not a simple task. – user2987828 Dec 4 '13 at 8:33
• On your edit: official syntax for ^ requires a brace around the argument: \exponent^{\frac{1}{2}}, a^{\frac{1}{2}}, etc. so should pose no additional difficulty. – Joseph Wright Dec 4 '13 at 8:35
• @user2987828 a^\frac12 is illegal anyway; the fact that it seems to work doesn't mean it's good syntax. – egreg Dec 4 '13 at 8:35
• Thanks Claudio Fiandrino for your fast and useful edits. – user2987828 Dec 4 '13 at 8:36

How's this?

\documentclass[12pt]{article}

\usepackage{ifthen}

\newcommand{\exponent}
{%
\ifthenelse{\equal{#1}{^}}%
{a^{#2}b^{#2}c^{#2}}%
{\ifthenelse{\equal{#1}{_}}%
{a_{#2}b_{#2}c_{#2}}%
{\message{## Warning: exponent command misused ##}#1#2}%
}}

\begin{document}
Here is the exponent command: $\exponent^5=\exponent^2\exponent^3$.

Here is the subscript version: $\exponent_3=\exponent_4+\exponent_5$.

Here it is misused: $\exponent 3$

\end{document} • Thanks. I did not knew the command \equal, I am more used to \ifx. Is it possible to make $\exponent^e_f$ also working ? – user2987828 Dec 4 '13 at 9:00
• I guess it would be possible, but you run the risk of making something more complicated than the effort you may be saving. If you do the obvious thing and modify the above to take 4 arguments and then #1 and #3 are then tested to match ^ and/or _, it will be pretty fragile unless you're very careful. e.g. something like \exponent^2+\exponent^3 will try with #1=^, #2=2, #3=3 and #4=\exponent, which will leave you in a right royal mess to get out of. The other danger with anything like this (even my original code) is to the clarity of the TeX source to the document. – Andrew Kepert Dec 4 '13 at 12:57
• ... oh and try $\exponent^{\the\count255 \global\advance\count255 5}$ ;-) – Andrew Kepert Dec 4 '13 at 12:58

One could define a macro called, say, \supsub, that takes two arguments -- the repeated material that goes in the superscripts and subscripts, resp. One or the other argument or even both arguments may be empty. \documentclass{article}
% exploit the fact that \null is defined as '{}'
\newcommand{\supsub}{ a \if#1\null\else^{#1}\fi \if#2\null\else_{#2}\fi
b \if#1\null\else^{#1}\fi \if#2\null\else_{#2}\fi
c \if#1\null\else^{#1}\fi \if#2\null\else_{#2}\fi }
\begin{document}
$\supsub{e}{}$, $\supsub{}{f}$, $\supsub{e}{f}$, and $\supsub{}{}$.

\medskip % just for this example
$\supsub{\frac12}{}$, $\supsub{1/2}{}$
\end{document}

• +1. Clear interface, no cryptic stuff that behaves in a bizzare way and nobody knows what it's actually doing. – yo' Dec 4 '13 at 17:07
• @tohecz - wow, thanks for the compliment! – Mico Dec 4 '13 at 17:25