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?
Asked
Active
Viewed 4,626 times
2 Answers
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}

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
\bEdefined as suggested: everyEin 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 changeEinto\bEin 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.
user52474
- 11
-
1What about the
E's in an equation environment or within\( \),\[...\]oraligned/align? ;-) – May 26 '14 at 15:51
Ein math mode, right? – Mico May 26 '14 at 07:39E? – May 26 '14 at 07:40Echaracter would look strange when printed with\mathbb{E}, although not forbidden ;-) – May 26 '14 at 18:28