# Can (pdf)LaTeX recognize text strings and replace (expand) them?

I'd like LaTeX to replace e^ with \mathrm{e}^. I know I could just make a macro \e that does this, but I know I'll forget using it..

Can LaTeX find and replace and replace letter combinations (I don't mean a find and replace function of the editor, I mean a true TeX, macro-like expansion).

-
You could, in theory, make e an active character (like ~) and let it test if the next character is ^ and then expand to \mathrm{e}, but this would be kind of overkill and would break other usages of e, like it is no longer allowed in macro names! You could limit this just to mathmode, which would improve that situation. I personally would go for the mentioned \e or search&replace using your editor. –  Martin Scharrer Apr 13 '11 at 16:33
You could make e an active character, but I think that would be a disaster. Is this just in math mode? If so, make e active only in math mode may work, but again could be problematic. –  Joseph Wright Apr 13 '11 at 16:34
A search and replace of e^ with \mathrm{e}^ makes more sense. –  Herbert Apr 13 '11 at 17:37
If you are willing to use XeTeX, the xesearch package can do this. (But Herbert is probably right.) –  Aaron Apr 13 '11 at 17:56
hmm, yes... All the smart people say it's impossible.. Could someone make this into an answer ? This way I can commit and close the question. –  romeovs Apr 13 '11 at 18:07
show 1 more comment

## 3 Answers

One can activate e only in math mode and only as a math character with mathcode "8000, that doesn't interfere with macro name scanning:

\documentclass[a4paper]{article}

\begingroup\lccode~=e\lowercase{\endgroup
\edef~{\noexpand\mathrm{\mathchar\the\mathcodee}}}
\mathcodee="8000

\begin{document}
$e^x\leq e^y$
\end{document}


but I advise against using such an approach. It's better to have a macro \euler or any other name.

In order to use the special "8000 mathcode assigned to e, there must exist a valid definition of e as active character. Therefore the well-known \lowercase trick (many commands used there have e in their names).

-
add comment

(Just summarising the comments.)

You could technically do it by making e an active character, but that will break all sorts of things and probably is not worth the while.

Defining \e or just doing a search and replace is probably a better idea.

-
add comment

In plain TeX, ' is defined in a rather tricky way, so that multiple consecutive ' behave properly. For that (and probably other reasons), Knuth introduced the possibility for a character which is normally not active to be active in math mode. This is setup by \mathcode\e="8000. We define an active e to check for the next character and decide whether to become \mathrm{e} or not.

\documentclass{article}

\begingroup
\makeatletter
\catcode\~=13\relax
\lccode\~=\e% pretend that a lowercase "~" is "e".
\lccode\^=\^% make sure it doesn't get change (who knows...)
\lowercase{\endgroup % restore the former catcodes and lccodes.
\newcommand{~}{\@ifnextchar^{\@firstoftwo{\mathrm{\true@e}^}}{\true@e}}%
\edef\true@e{\mathchar\the\mathcode\e}% save the true e.
}
\mathcode\e="8000\relax

\begin{document}

As everyone knows, $e^{i\pi}+1=0$ and if $e=0$, $\frac{1}{e}$ blows up. Of course, we can try more complicated things:
$$\epsilon^{e^{e_a^b}}+e_a^{e^a}$$

\end{document}

-
so there's actually no problem in defining it this way? The other post made me worry about macro breaking etc. What's your take on that? –  romeovs Apr 13 '11 at 20:53
@romeovs: This will pose no problem as long as no one else is trying to define an active character e. This will normally not happen (because of the many problems it entails when done outside math mode, as described in other posts), so I think that you are safe. Although... I would advise against doing that for all the letters, or having crazy definitions, simply because it makes the code harder to understand. You may run into trouble if you try to do the same with e.g. ~. Or : in a french document, etc, when those are made active. –  Bruno Le Floch Apr 13 '11 at 23:22
add comment