# How to replace “E” by “\mathbb{E}” throughout out the article in LaTeX

I have a quick question about replacement in LaTeX. At first, I used capital E throughout my paper and now I really need to replace E by \mathbb{E}, somebody suggests that I can use Macros to achieve. But how can I do that?

-
–  Malipivo May 26 '14 at 7:35
I assume you're referring only to instances of E in math mode, right? –  Mico May 26 '14 at 7:39
@Malipivo: Would this not change every occurence of E? –  Christian Hupfer May 26 '14 at 7:40
@ChristianHupfer Yes, it would. It's highly probable that OP is referring to the math mode only, I agree with you. MWE or more details from OP would be great. –  Malipivo May 26 '14 at 7:43
Apologies if this sounds really stupid, but can you not just use a search and replace function in your text editor? I am guessing that your normal text is not smattered with a lot of random capital E's. In which case you could search for " E " and replace with " \mathbb{E}" - of course you'd probably need to do the equations manually, but that might not be such a bad idea anyway. I am always wary of automatic replace functions in equations as if it goes wrong it can be hard to work out what's wrong later! –  FionaSmith May 26 '14 at 8:44

If all occurrences of E in math mode must be changed, then you could use the following trick:

\documentclass{article}
\usepackage{amsmath,amssymb}

%%% Change E in math mode to \mathbb{E}
\edef\normalE{\the\mathcodeE}
\begingroup\lccode~=E \lowercase{\endgroup
\def~}{\mathbb{\mathchar\normalE}}
\mathcodeE="8000
%%%

\begin{document}
This is E' in text, but here
it is in math $E$.
\end{document}


However, my advice is to do a “find and replace”, with E replaced where necessary by \bE (use a better macro name based on the semantics of the symbol) and add

\newcommand{\bE}{\mathbb{E}}


in the preamble, so that you can easily change your mind later about the final form of the symbol.

-
This really works, thank you soooo much!! –  Sean May 26 '14 at 19:25
But how should I modify the code above to change "E" to "\bE"? (provided I have defined "\bE" before) –  Sean May 26 '14 at 19:46
@Sean I can't understand. If you use the first hack, you can't use \bE defined as suggested: every E in math mode will already become \mathbb{E} without further ado. But this is a route with no escape. That's why I suggest you not to go along this path, but rather to actually change E into \bE in the file. –  egreg May 26 '14 at 19:53
Now I understand, Thank you! –  Sean May 26 '14 at 22:29

You can use vim search and replace to find all E's that sit between $'s. Something like %s/$$$.*$$E$$.*$$$/\1\mathbb{E}\2/gc  But I don't think this takes into account that the$'s may be on separate lines.

-
What about the E's in an equation environment or within , $...$ or aligned/align`? ;-) –  Christian Hupfer May 26 '14 at 15:51