[Tex/LaTex] the cleanest way to redefine math-mode font commands such as \mathrm and \mathit to use a different font

fontsmath-mode

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}

Best Answer

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}

enter image description here

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}
Related Question