TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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:


%%% Change E in math mode to \mathbb{E}
\begingroup\lccode`~=`E \lowercase{\endgroup

This is `E' in text, but here
it is in math $E$.

enter image description here

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


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

share|improve this answer
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


But I don't think this takes into account that the $'s may be on separate lines.

share|improve this answer
What about the E's in an equation environment or within \( \), \[...\] or aligned/align? ;-) – Christian Hupfer May 26 '14 at 15:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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