# What is the cleanest way to redefine math-mode font commands such as \mathrm and \mathit to use a different font?

What is the cleanest way to redefine math-mode font commands such as \mathrm and \mathit?

I would like everything else to be unaffected; for example, perhaps I enjoy working with mathptmx and I want to keep its \mathnormal, but I want to change \mathrm and mathit to use a different font.

\documentclass{article}
\usepackage{mathptmx}

\begin{document}

Some text.

Some formula: $$abc = de/f \quad[\mathrm{var}, \mathit{val}]$$

$$\mathnormal{abc = de/f}$$

var, \textit{val}

$$\mathrm{var}, \mathit{val}$$

% Bitstream Charter
{\fontfamily{bch}\fontseries{m}\fontshape{n}\selectfont  var,
\fontfamily{bch}\fontseries{m}\fontshape{it}\selectfont val}

% Linux Biolinum
{\fontfamily{LinuxBiolinumT-OsF}\fontseries{m}\fontshape{n}\selectfont  var,
\fontfamily{LinuxBiolinumT-OsF}\fontseries{m}\fontshape{it}\selectfont val}

% Linux Libertine
{\fontfamily{LinuxLibertineT-OsF}\fontseries{m}\fontshape{n}\selectfont  var,
\fontfamily{LinuxLibertineT-OsF}\fontseries{m}\fontshape{it}\selectfont val}

\end{document}

-
Using Charter for \mathrm when the main font is Times is a typographic nonsense. –  egreg Dec 14 '13 at 15:39
@egreg I'm not sure I feel that strongly about it :-) but sometimes a bit of variety serves illustrative purposes, to give examples for the question. –  Lover of Structure Dec 14 '13 at 15:50
@egreg (bch is way better than some of the other choices that'd look outrageous in math mode.) –  Lover of Structure Dec 14 '13 at 15:58

Just redefine the macros \mathrm and \mathit to use a different font: the interface is

\DeclareMathAlphabet{<encoding>}{<family>}{<series>}{<shape>}


You can also supplement the font for a different math version: say also

\SetMathAlphabet{bold}{<encoding>}{<family>}{<series>}{<shape>}


for defining the font to be used in a \boldmath (or \mathversion{bold}) context.

An example follows. But beware that using Charter for \mathrm and \mathit is a typographical nonsense. Here's the visual proof of my claim.

\documentclass{article}
\usepackage{mathptmx}
\DeclareMathAlphabet{\mathrm}{OT1}{bch}{m}{n}
\DeclareMathAlphabet{\mathit}{OT1}{bch}{m}{it}

\begin{document}

Some text.

Some formula: $$abc = de/f \quad[\mathrm{var}, \mathit{val}]$$

Some formula: $$\mathit{abc}=\mathit{de}/\mathit{f}$$

\end{document}


Instead of \DeclareMathAlphabet one can define a substitute that won't eat up math families:

 \usepackage{amsmath}
\newcommand{\mathcharter}[1][\encodingdefault]{%
\text{\usefont{#1}{bch}{m}{n}#2}%
}


and use it as

\mathcharter{abc}


or even

\mathcharter[T1]{l'Hôpital}

-
What function does the OT1 serve? (I know it's about the "encoding", but practically what does that mean here? Somewhere in the preamble I have the line \usepackage[T3,T1]{fontenc}, with T3 included for tipa. Are there alternatives to OT1?) –  Lover of Structure Dec 14 '13 at 15:47
@LoverofStructure Not really; with \mathrm you should only use ASCII characters. –  egreg Dec 14 '13 at 15:51
@LoverofStructure No, OT1 is predefined. –  egreg Dec 14 '13 at 15:58
@LoverofStructure Using \fontfamily doesn't work in math mode. –  egreg Dec 14 '13 at 16:59
@LoverofStructure The math families are allocated only when expressly requested in a math formula for the first time. –  egreg Dec 23 '13 at 17:59