Take the 2-minute tour ×
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.

I was trying to write a new command for expected value operator, such that if its argument is a uppercase letter then it will replace it with a \mathrm one, e.g. from E(X) to E(\mathrm{X}); and will expand lowercase letter as is. For a single letter, I can simply do the following

\documentclass{article}
\usepackage{mathtools}

\newcommand{\E}[1]{\mathrm{E}
\lparen
\ifnum `#1=\uccode`#1
\mathrm{#1}
\else #1
\fi
\rparen}

\begin{document}

\[\E{X}\]

\end{document}

However, if the input is a series of characters, say e^{tX}, this methods will blunder. I'd like to know if there is a method to check the "uppercaseness" of an argument and modify it accordingly.

Remark: This command is only for notation of random variables, so I still want uppercase letter italic elsewhere.

share|improve this question
    
It seems to me that you'd like to have French style, where uppercase letters in math are upright. Doing it that way is surely the wrong thing to try. –  egreg Sep 7 '13 at 20:27
    
@egreg: This command is only for notation of random variables, so I still want uppercase letter italic elsewhere. –  Francis Sep 7 '13 at 20:32
    
The best method is to define a command, say \newcommand{\rd}[1]{\mathrm{#1}} and use it. –  egreg Sep 7 '13 at 20:34
1  
so \E(aBc) will leave a and c slanted and make B upright? then you could use a method based on a math alphabet (but lowercase and uppercase would need two symbol fonts for simplicity; or, one may change the family bit on the fly using mathcode's.) Or \E could make the uppercase letters mathematically active and have them expand to what is needed. –  jfbu Sep 7 '13 at 20:39
    
@egreg: tried that before, however when the document gets lengthy proofreading becomes difficult :P that's why I am trying to set up a command that will do it automatically. –  Francis Sep 7 '13 at 20:43

3 Answers 3

up vote 5 down vote accepted
\documentclass{article}

\makeatletter
\newcommand{\change@uppercase@math}{%
  \count@=`\A
  \loop
    \mathcode\count@\count@
    \ifnum\count@<`\Z
    \advance\count@\@ne
  \repeat}

\newcommand{\E}[1]{
  \mathrm{E}
  (\begingroup\change@uppercase@math#1\endgroup)
}
\makeatother

\begin{document}
$\E{X}+\E{e^{tX}}$
\end{document}

Slow, of course. I stick with my opinion that it's better to mark what you want upright.

enter image description here

share|improve this answer
    
This is clever, it works perfectly now :) –  Francis Sep 7 '13 at 20:53

If you want all uppercase roman you can do something like this (or you can do it locally if that is needed)

enter image description here

\documentclass{article}

\makeatletter
\count@`A
\loop
\mathcode\count@\count@
\ifnum\count@<`Z
\advance\count@\@ne
\repeat
\makeatother
\begin{document}

$abc+ABC+def+DEF+ghijklmnop+GHIJKLMNOP$

$qQrRsStTuUvVwWxXyYzZ$

\end{document}
share|improve this answer
    
Ah shame you clarified in comments that you don't want it globally, shame:-) –  David Carlisle Sep 7 '13 at 20:39
    
sorry about the unclarity, will change the question :P –  Francis Sep 7 '13 at 20:41

I have not tested, but the following (incompatible with unicode engines) should be compatible with arbitrary math font contexts. In particular it does not assume that the \mathrm alphabet will choose the math family 0. The command \E, before issueing \mathrm on its arguments, first makes, in a group, all lowercase letters unresponsive to math alphabets. Package xint is used for easier computing (expandably).

\documentclass{article}
% not compatible with unicode engines

\usepackage{xint} 

\makeatletter

% this macro will make the lowercase letter unresponsive to
% math changing alphabets; and this is compatible to arbitrary
% math font setup.

%
\def\@makeunreactive #1{%
    \mathcode`#1=\xintRem{\the\mathcode`#1}{4096}
}

\newcommand*\E[1]{%
  \begingroup
     \@tfor\x:=\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z\do
     {\expandafter\@makeunreactive\x}% 
     \mathrm{#1}%
  \endgroup
}
\makeatother


\begin{document}


$\E{abcdefghijklmnopqrestuvwxyzABCDEFGHIJKLMNOPQRESTUVWXYZ}$

\end{document}

uppercase upright

share|improve this answer
    
there is a spurious e (in uppercase too) in the image... but fortunately it is also in the source, so ok. –  jfbu Sep 7 '13 at 21:24

Your Answer

 
discard

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.