9

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?

azetina
  • 28,884
Sean
  • 211
  • A tip for you: http://tex.stackexchange.com/questions/179171/remapping-a-single-glyph/179284#179284 – Malipivo May 26 '14 at 07:35
  • I assume you're referring only to instances of E in math mode, right? – Mico May 26 '14 at 07:39
  • @Malipivo: Would this not change every occurence of E? –  May 26 '14 at 07: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 07:43
  • @Malipivo: Agreed, and using logical markup from start would even be better ;-) –  May 26 '14 at 07:45
  • 2
    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 08:44
  • @ChristianHupfer Yes, I mean the capital E in math mode only. I do not intend to change every E if they appear just as character – Sean May 26 '14 at 18:20
  • @Sean: I supposed you would, otherwise a normal text E character would look strange when printed with \mathbb{E}, although not forbidden ;-) –  May 26 '14 at 18:28

2 Answers2

14

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\mathcode`E}
\begingroup\lccode`~=`E \lowercase{\endgroup
  \def~}{\mathbb{\mathchar\normalE}}
\mathcode`E="8000
%%%

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

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

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

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

egreg
  • 1,121,712
  • 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
1

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.

  • 1
    What about the E's in an equation environment or within \( \), \[...\] or aligned/align? ;-) –  May 26 '14 at 15:51