# Automatic upright math when text is in italic

This is in a way a follow-up to this question about upright math.

I would like to have upright math (essentially variable names) when I use a theorem-like environment with statement in italic. This used to be done that way in certain 19th century mathematical publications, and I would like to reproduce that look.

1. I could use \mathrm everywhere I need it in the formulas.

2. I could remove the $...$ everywhere only isolated letters are used in the statement and surround them with appropriate commands.

3. I could use the mathastext LaTeX package with its "subdued" option

But

1. It's a pity to surcharge all the formulas for the special case of theorems

2. It's also not great to un-TeX math variables in theorems as well, also bad for proofreading, cuting and pasting.

3. The use of the mathastext package is quite heavy in consequences because of all its interaction with mathfonts, mathversions and seems a bit of a risk for a large document using already many packages. As its author himself writes:

Without the subdued option, the mathastextification applies by default to the whole of the document (and one may also define additional math versions in the preamble); with the subdued option the mathastextification is done only in math versions distinct from the standard and bold ones.

The previous description is in fact a bit optimistic: mathastext was not written initially in order to allow its action to be completely cancelled, and achieving this would require a complete rewrite of large portions of the code. In order to really have the displayed math (almost) as if mathastext had not been loaded, one must at a minimum also use the option defaultmathsizes. This does not quite suffice,[...]

So, is there a simple way I could define a theorem environment where (simple) math would be upright ?

EDIT

Thanks to jfbu's answers, here is a visual illustration. The case I wanted is the last one.

-
If you have a font with OML encoding and upright latin letters and greek letters, then it should be easy enough using \DeclareMathVersion to declare a math version called myuprightmath for example and then \SetSymbolFont {letters){myuprightmath}<font specification>. Else, one has to use two fonts, one with upright Greek letters and the other one with upright Latin letters. And this means a bunch of \DeclareMathSymbol to re-assign the Greek letters to the desired font, now distinct from the one for Latin letters. –  jfbu May 27 '13 at 16:39
That is something like greekletters should be declared with \DeclareSymbolFont, and in the normal and bold math versions one would use the standard font for greekletters whereas in the special myuprightmath math version one would then have greekletters (and letters) map to the two upright fonts. –  jfbu May 27 '13 at 16:42
What was done in 19th century typography isn't necessarily worth reproducing. This is one case. –  egreg May 27 '13 at 21:49
@egreg : although this is perhaps marginal, one use I have of TeX/LaTeX/ConTeXt, etc. is to reproduce faithfully extracts of the scientific literature of the past. But if one remembers that the use of italic for mathematical variables was precisely to make them stand out from the surrounding normal text, then you can justify reversing the pattern when surrounded by italic text. –  ogerard May 28 '13 at 3:11
@jfbu : thanks for your comments about OML and LGR encoding. I will need this kind of things anyway because my document contains both vanilla LaTeX math typesetting and several old-style math parts. –  ogerard May 28 '13 at 3:17

Here is what I explained in the comments. I also had to declare a Symbol Font also for the Latin letters, because there are some things in standard LaTeX, like rightharpoonup which are in letters; and we don't want to reassign them to a font with another encoding.

Update: added at the end the necessary code for LGR which was mentioned in comments.

\documentclass{article}
\DeclareSymbolFont {greekletters}{OML}{cmm}{m}{it}
\DeclareSymbolFont {latinletters}{OML}{cmm}{m}{it}
\DeclareMathVersion {upright}
\SetSymbolFont {latinletters}{upright}{OT1}{cmr}{m}{n}
\SetSymbolFont {greekletters}{upright}{U}{txmia}{m}{it} % using the upright
% Greek letters provided with the txfonts
% package

\DeclareMathSymbol{a}{\mathalpha}{latinletters}{a}
\DeclareMathSymbol{b}{\mathalpha}{latinletters}{b}
\DeclareMathSymbol{c}{\mathalpha}{latinletters}{c}
\DeclareMathSymbol{d}{\mathalpha}{latinletters}{d}
\DeclareMathSymbol{e}{\mathalpha}{latinletters}{e}
\DeclareMathSymbol{f}{\mathalpha}{latinletters}{f}
\DeclareMathSymbol{g}{\mathalpha}{latinletters}{g}
\DeclareMathSymbol{h}{\mathalpha}{latinletters}{h}
\DeclareMathSymbol{i}{\mathalpha}{latinletters}{i}
\DeclareMathSymbol{j}{\mathalpha}{latinletters}{j}
\DeclareMathSymbol{k}{\mathalpha}{latinletters}{k}
\DeclareMathSymbol{l}{\mathalpha}{latinletters}{l}
\DeclareMathSymbol{m}{\mathalpha}{latinletters}{m}
\DeclareMathSymbol{n}{\mathalpha}{latinletters}{n}
\DeclareMathSymbol{o}{\mathalpha}{latinletters}{o}
\DeclareMathSymbol{p}{\mathalpha}{latinletters}{p}
\DeclareMathSymbol{q}{\mathalpha}{latinletters}{q}
\DeclareMathSymbol{r}{\mathalpha}{latinletters}{r}
\DeclareMathSymbol{s}{\mathalpha}{latinletters}{s}
\DeclareMathSymbol{t}{\mathalpha}{latinletters}{t}
\DeclareMathSymbol{u}{\mathalpha}{latinletters}{u}
\DeclareMathSymbol{v}{\mathalpha}{latinletters}{v}
\DeclareMathSymbol{w}{\mathalpha}{latinletters}{w}
\DeclareMathSymbol{x}{\mathalpha}{latinletters}{x}
\DeclareMathSymbol{y}{\mathalpha}{latinletters}{y}
\DeclareMathSymbol{z}{\mathalpha}{latinletters}{z}
\DeclareMathSymbol{A}{\mathalpha}{latinletters}{A}
\DeclareMathSymbol{B}{\mathalpha}{latinletters}{B}
\DeclareMathSymbol{C}{\mathalpha}{latinletters}{C}
\DeclareMathSymbol{D}{\mathalpha}{latinletters}{D}
\DeclareMathSymbol{E}{\mathalpha}{latinletters}{E}
\DeclareMathSymbol{F}{\mathalpha}{latinletters}{F}
\DeclareMathSymbol{G}{\mathalpha}{latinletters}{G}
\DeclareMathSymbol{H}{\mathalpha}{latinletters}{H}
\DeclareMathSymbol{I}{\mathalpha}{latinletters}{I}
\DeclareMathSymbol{J}{\mathalpha}{latinletters}{J}
\DeclareMathSymbol{K}{\mathalpha}{latinletters}{K}
\DeclareMathSymbol{L}{\mathalpha}{latinletters}{L}
\DeclareMathSymbol{M}{\mathalpha}{latinletters}{M}
\DeclareMathSymbol{N}{\mathalpha}{latinletters}{N}
\DeclareMathSymbol{O}{\mathalpha}{latinletters}{O}
\DeclareMathSymbol{P}{\mathalpha}{latinletters}{P}
\DeclareMathSymbol{Q}{\mathalpha}{latinletters}{Q}
\DeclareMathSymbol{R}{\mathalpha}{latinletters}{R}
\DeclareMathSymbol{S}{\mathalpha}{latinletters}{S}
\DeclareMathSymbol{T}{\mathalpha}{latinletters}{T}
\DeclareMathSymbol{U}{\mathalpha}{latinletters}{U}
\DeclareMathSymbol{V}{\mathalpha}{latinletters}{V}
\DeclareMathSymbol{W}{\mathalpha}{latinletters}{W}
\DeclareMathSymbol{X}{\mathalpha}{latinletters}{X}
\DeclareMathSymbol{Y}{\mathalpha}{latinletters}{Y}
\DeclareMathSymbol{Z}{\mathalpha}{latinletters}{Z}
\DeclareMathSymbol{\alpha}{\mathord}{greekletters}{"0B}
\DeclareMathSymbol{\beta}{\mathord}{greekletters}{"0C}
\DeclareMathSymbol{\gamma}{\mathord}{greekletters}{"0D}
\DeclareMathSymbol{\delta}{\mathord}{greekletters}{"0E}
\DeclareMathSymbol{\epsilon}{\mathord}{greekletters}{"0F}
\DeclareMathSymbol{\zeta}{\mathord}{greekletters}{"10}
\DeclareMathSymbol{\eta}{\mathord}{greekletters}{"11}
\DeclareMathSymbol{\theta}{\mathord}{greekletters}{"12}
\DeclareMathSymbol{\iota}{\mathord}{greekletters}{"13}
\DeclareMathSymbol{\kappa}{\mathord}{greekletters}{"14}
\DeclareMathSymbol{\lambda}{\mathord}{greekletters}{"15}
\DeclareMathSymbol{\mu}{\mathord}{greekletters}{"16}
\DeclareMathSymbol{\nu}{\mathord}{greekletters}{"17}
\DeclareMathSymbol{\xi}{\mathord}{greekletters}{"18}
\DeclareMathSymbol{\pi}{\mathord}{greekletters}{"19}
\DeclareMathSymbol{\rho}{\mathord}{greekletters}{"1A}
\DeclareMathSymbol{\sigma}{\mathord}{greekletters}{"1B}
\DeclareMathSymbol{\tau}{\mathord}{greekletters}{"1C}
\DeclareMathSymbol{\upsilon}{\mathord}{greekletters}{"1D}
\DeclareMathSymbol{\phi}{\mathord}{greekletters}{"1E}
\DeclareMathSymbol{\chi}{\mathord}{greekletters}{"1F}
\DeclareMathSymbol{\psi}{\mathord}{greekletters}{"20}
\DeclareMathSymbol{\omega}{\mathord}{greekletters}{"21}
\DeclareMathSymbol{\varepsilon}{\mathord}{greekletters}{"22}
\DeclareMathSymbol{\vartheta}{\mathord}{greekletters}{"23}
\DeclareMathSymbol{\varpi}{\mathord}{greekletters}{"24}
\DeclareMathSymbol{\varrho}{\mathord}{greekletters}{"25}
\DeclareMathSymbol{\varsigma}{\mathord}{greekletters}{"26}
\DeclareMathSymbol{\varphi}{\mathord}{greekletters}{"27}

\begin{document}

This is standard: $\alpha^n+\beta^n+x^n+y^n=z^n$.

\mathversion {upright}

This is upright: $\alpha^n+\beta^n+x^n+y^n=z^n$.

\mathversion {normal}

This is standard:  $\alpha^n+\beta^n+x^n+y^n=z^n$.

\end{document}


Here is now what one can do for LGR. It got complicated as I wanted also to define \Alpha, \Beta, etc... which usually come from the Latin font, but as they are in the LGR encoded font, I needed to make them available in the standard non-upright math as well (and they are then picked in the cmr font, assuming standard set-up).

\DeclareFontEncoding {LGR}{}{}
\SetSymbolFont {greekletters}{upright}{LGR}{cmr}{m}{n}

% (the LGR encoded CMR font comes with the cbfonts package, and is
% pre-installed at least in TeXLive and presumably also other TeX
% distributions)

% This list defines also capital' Greek letters,
% including the ones usually represented by Latin letters.
% missing: \varphi, \varepsilon, \varpi, \vartheta, \varrho

%% 'uppercase'
\DeclareMathSymbol {\Alphalgr}   {\mathord}{greekletters}{65}
\DeclareMathSymbol {\Betalgr}    {\mathord}{greekletters}{66}
\DeclareMathSymbol {\Gammalgr}   {\mathord}{greekletters}{71}
\DeclareMathSymbol {\Deltalgr}   {\mathord}{greekletters}{68}
\DeclareMathSymbol {\Epsilonlgr} {\mathord}{greekletters}{69}
\DeclareMathSymbol {\Zetalgr}    {\mathord}{greekletters}{90}
\DeclareMathSymbol {\Etalgr}     {\mathord}{greekletters}{72}
\DeclareMathSymbol {\Thetalgr}   {\mathord}{greekletters}{74}
\DeclareMathSymbol {\Iotalgr}    {\mathord}{greekletters}{73}
\DeclareMathSymbol {\Kappalgr}   {\mathord}{greekletters}{75}
\DeclareMathSymbol {\Lambdalgr}  {\mathord}{greekletters}{76}
\DeclareMathSymbol {\Mulgr}      {\mathord}{greekletters}{77}
\DeclareMathSymbol {\Nulgr}      {\mathord}{greekletters}{78}
\DeclareMathSymbol {\Xilgr}      {\mathord}{greekletters}{88}
\DeclareMathSymbol {\Omicronlgr} {\mathord}{greekletters}{79}
\DeclareMathSymbol {\Pilgr}      {\mathord}{greekletters}{80}
\DeclareMathSymbol {\Rholgr}     {\mathord}{greekletters}{82}
\DeclareMathSymbol {\Sigmalgr}   {\mathord}{greekletters}{83}
\DeclareMathSymbol {\Taulgr}     {\mathord}{greekletters}{84}
\DeclareMathSymbol {\Upsilonlgr} {\mathord}{greekletters}{85}
\DeclareMathSymbol {\Philgr}     {\mathord}{greekletters}{70}
\DeclareMathSymbol {\Chilgr}     {\mathord}{greekletters}{81}
\DeclareMathSymbol {\Psilgr}     {\mathord}{greekletters}{89}
\DeclareMathSymbol {\Omegalgr}   {\mathord}{greekletters}{87}
\DeclareMathSymbol {\Digammalgr} {\mathord}{greekletters}{195}
%% 'lowercase'
\DeclareMathSymbol {\alphalgr}   {\mathord}{greekletters}{97}
\DeclareMathSymbol {\betalgr}    {\mathord}{greekletters}{98}
\DeclareMathSymbol {\gammalgr}   {\mathord}{greekletters}{103}
\DeclareMathSymbol {\deltalgr}   {\mathord}{greekletters}{100}
\DeclareMathSymbol {\epsilonlgr} {\mathord}{greekletters}{101}
\DeclareMathSymbol {\zetalgr}    {\mathord}{greekletters}{122}
\DeclareMathSymbol {\etalgr}     {\mathord}{greekletters}{104}
\DeclareMathSymbol {\thetalgr}   {\mathord}{greekletters}{106}
\DeclareMathSymbol {\iotalgr}    {\mathord}{greekletters}{105}
\DeclareMathSymbol {\kappalgr}   {\mathord}{greekletters}{107}
\DeclareMathSymbol {\lambdalgr}  {\mathord}{greekletters}{108}
\DeclareMathSymbol {\mulgr}      {\mathord}{greekletters}{109}
\DeclareMathSymbol {\nulgr}      {\mathord}{greekletters}{110}
\DeclareMathSymbol {\xilgr}      {\mathord}{greekletters}{120}
\DeclareMathSymbol {\omicronlgr} {\mathord}{greekletters}{111}
\DeclareMathSymbol {\pilgr}      {\mathord}{greekletters}{112}
\DeclareMathSymbol {\rholgr}     {\mathord}{greekletters}{114}
\DeclareMathSymbol {\varsigmalgr}{\mathord}{greekletters}{99}
\DeclareMathSymbol {\sigmalgr}   {\mathord}{greekletters}{115}
\DeclareMathSymbol {\taulgr}     {\mathord}{greekletters}{116}
\DeclareMathSymbol {\upsilonlgr} {\mathord}{greekletters}{117}
\DeclareMathSymbol {\philgr}     {\mathord}{greekletters}{102}
\DeclareMathSymbol {\chilgr}     {\mathord}{greekletters}{113}
\DeclareMathSymbol {\psilgr}     {\mathord}{greekletters}{121}
\DeclareMathSymbol {\omegalgr}   {\mathord}{greekletters}{119}
\DeclareMathSymbol {\digammalgr} {\mathord}{greekletters}{147}

\makeatletter

% \let\Alphastd A\let\Betastd B\let\Epsilonstd E\let\Zetastd Z%
% \let\Etastd N\let\Iotastd I\let\Kappastd K\let\Mustd M%
% \let\Nustd N\let\Omicronstd O\let\Rhostd P\let\Taustd T%
% \let\Chistd X\let\Digammastd F%
% but we wan't them upright (as the other capital Greek letters), hence:
\DeclareMathSymbol{\Alphastd}  {\mathord}{operators}{A}
\DeclareMathSymbol{\Betastd}   {\mathord}{operators}{B}
\DeclareMathSymbol{\Epsilonstd}{\mathord}{operators}{E}
\DeclareMathSymbol{\Digammastd}{\mathord}{operators}{F}
\DeclareMathSymbol{\Etastd}    {\mathord}{operators}{H} % not sure here
\DeclareMathSymbol{\Iotastd}   {\mathord}{operators}{I}
\DeclareMathSymbol{\Kappastd}  {\mathord}{operators}{K}
\DeclareMathSymbol{\Mustd}     {\mathord}{operators}{M}
\DeclareMathSymbol{\Nustd}     {\mathord}{operators}{N}
\DeclareMathSymbol{\Omicronstd}{\mathord}{operators}{O}
\DeclareMathSymbol{\Rhostd}    {\mathord}{operators}{P}
\DeclareMathSymbol{\Taustd}    {\mathord}{operators}{T}
\DeclareMathSymbol{\Chistd}    {\mathord}{operators}{X}
\DeclareMathSymbol{\Zetastd}   {\mathord}{operators}{Z}

\let\digammastd f% <- not satisfactory ?
\let\omicronstd o%

\@for\@tempa:=alpha,beta,gamma,delta,epsilon,zeta,eta,theta,%
iota,kappa,lambda,mu,nu,xi,pi,rho,varsigma,sigma,%
tau,upsilon,phi,chi,psi,omega,Gamma,Delta,Theta,Lambda,%
Xi,Pi,Sigma,Upsilon,Phi,Psi,Omega\do{\expandafter\let
\csname\@tempa std\expandafter\endcsname\csname\@tempa\endcsname}%

\newcommand*\lgrgreek{%
\@for\@tempa:=alpha,beta,gamma,delta,epsilon,zeta,eta,theta,%
iota,kappa,lambda,mu,nu,xi,omicron,pi,rho,varsigma,sigma,%
tau,upsilon,phi,chi,psi,omega,digamma,Digamma,Alpha,Beta,%
Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,Nu,%
Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,%
Omega\do{\expandafter\let\csname\@tempa\expandafter\endcsname
\csname\@tempa lgr\endcsname}}%

\newcommand*\stdgreek{%
\@for\@tempa:=alpha,beta,gamma,delta,epsilon,zeta,eta,theta,%
iota,kappa,lambda,mu,nu,xi,omicron,pi,rho,varsigma,sigma,%
tau,upsilon,phi,chi,psi,omega,digamma,Digamma,Alpha,Beta,%
Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,Nu,%
Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,%
Omega\do{\expandafter\let\csname\@tempa\expandafter\endcsname
\csname\@tempa std\endcsname}}%

\newcommand*\checkgreekletters{%
\@for\@tempa:=alpha,beta,gamma,delta,epsilon,zeta,eta,theta,%
iota,kappa,lambda,mu,nu,xi,omicron,pi,rho,varsigma,sigma,%
tau,upsilon,phi,chi,psi,omega,digamma,Digamma,Alpha,Beta,%
Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,Mu,Nu,%
Xi,Omicron,Pi,Rho,Sigma,Tau,Upsilon,Phi,Chi,Psi,%
Omega\do{\makebox[.75em]{$\csname\@tempa\endcsname$}, }}

\makeatother
\stdgreek % initializes \Alpha, etc...

\begin{document}

This is standard: $\alpha^n+\beta^n+x^n+y^n=z^n$.

\checkgreekletters

\mathversion {upright}\lgrgreek

This is upright: $\alpha^n+\beta^n+x^n+y^n=z^n$.

\checkgreekletters

\mathversion {normal}\stdgreek

This is standard:  $\alpha^n+\beta^n+x^n+y^n=z^n$.

\checkgreekletters

\end{document}


-
There are also nice upright Greek letters in the LGR encoded cmr font. But the numbers to use as last entry of \DeclareMathSymbol have all to be changed to use this LGR encoding. –  jfbu May 27 '13 at 17:14
thanks a lot for this easy to understand solution. It will do nicely for my intended use. –  ogerard May 28 '13 at 3:10
with the lgr variant, with CM fonts, it appears that the uppercase Greek glyphs are the same in the LGR and in the OT1 fonts. So, it would be simpler to not define both \Alphastd and \Alphalgr but just \Alpha (to be an A from OT1), and not touch \Gamma etc... only lowercase letters need to be treated as in my answer. However for some other fonts in LGR encoding than CM one needs to do as in my answer. –  jfbu May 28 '13 at 8:08
and were it not for the fact that \DeclareMathSymbol is made preamble-only by the LaTeX kernel, we could spare defining all these control sequences \alphalgr, \alphastd etc... (using directly \mathcode` is possible but then one needs to recover the family number, and perhaps worry about unicode engines, etc... ) –  jfbu May 28 '13 at 8:15
thanks for this extension of the idea so that all the Greek capitals can be selected. As for the constraints of \DeclareMathSymbol, I don't mind at all. I feel it is cleaner and more portable. –  ogerard May 28 '13 at 9:44