How to switch easily between normal font and sans-serif font for maths?

The sansmath package provides a command (\sansmath) and environment (sansmath) for exactly this purpose:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{sansmath}

\def\mySfFamily{\fontfamily{cmbr}\selectfont\sffamily\sansmath}
\def\N{\mathbf{N}}
\def\R{\mathbf{R}}
\def\Q{\mathbf{Q}}
\def\Z{\mathbf{Z}}
\def\C{\mathbf{C}}


\begin{document}

\begingroup
\mySfFamily
This should be sans-serif : $\R, \Q, \N, \Z, \C$ are sets.
\endgroup

\bigskip

This should be serif : $\R, \Q, \N, \Z, \C$ are sets.

\end{document}

enter image description here

To apply this "automagically", I would hack it into the \sffamily and \rmmfamily commands. (I prefer \appto from the etoolbox package for this, but one could as well use the usual \let\OLDmacro\macro and \renewcommand\macro{\OLDmacro <extension>} idiom.):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{sansmath,etoolbox}

\renewcommand{\sfdefault}{cmbr}

\appto{\sffamily}{\sansmath}
\appto{\rmfamily}{\unsansmath}


\def\N{\mathbf{N}}
\def\R{\mathbf{R}}
\def\Q{\mathbf{Q}}
\def\Z{\mathbf{Z}}
\def\C{\mathbf{C}}


\begin{document}


\sffamily

This should be sans-serif : $\R, \Q, \N, \Z, \C$ are sets.

\rmfamily

This should be serif : $\R, \Q, \N, \Z, \C$ are sets.


\end{document}

enter image description here


Some time ago I asked nearly the same question, the answer was to define a new math alphabet and hack it into the definition of \sffamily. So I’d suggest to close this question as duplicate but it can’ be done until the bounty is closed.


This is the code a build upon the answer of egreg.

\documentclass[parskip=half]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
\SetSymbolFont{letters}{sans}{OML}{cmbrm}{m}{it}
\SetSymbolFont{symbols}{sans}{OMS}{cmbrs}{m}{n}
\SetMathAlphabet{\mathit}{sans}{OT1}{cmbr}{m}{sl}
\SetMathAlphabet{\mathbf}{sans}{OT1}{cmbr}{bx}{n}
\SetMathAlphabet{\mathtt}{sans}{OT1}{cmtl}{m}{n}
\SetSymbolFont{largesymbols}{sans}{OMX}{iwona}{m}{n}

\DeclareMathVersion{boldsans}
\SetSymbolFont{operators}{boldsans}{OT1}{cmbr}{b}{n}
\SetSymbolFont{letters}{boldsans}{OML}{cmbrm}{b}{it}
\SetSymbolFont{symbols}{boldsans}{OMS}{cmbrs}{b}{n}
\SetMathAlphabet{\mathit}{boldsans}{OT1}{cmbr}{b}{sl}
\SetMathAlphabet{\mathbf}{boldsans}{OT1}{cmbr}{bx}{n}
\SetMathAlphabet{\mathtt}{boldsans}{OT1}{cmtl}{b}{n}
\SetSymbolFont{largesymbols}{boldsans}{OMX}{iwona}{bx}{n}

\newif\IfInSansMode
\let\oldsf\sffamily
\renewcommand*{\sffamily}{\oldsf\mathversion{sans}\InSansModetrue}
\let\oldmd\mdseries
\renewcommand*{\mdseries}{\oldmd\IfInSansMode\mathversion{sans}\fi\relax}
\let\oldbf\bfseries
\renewcommand*{\bfseries}{\oldbf\IfInSansMode\mathversion{boldsans}\else%
   \mathversion{bold}\fi\relax}
\let\oldnorm\normalfont
\renewcommand*{\normalfont}{\oldnorm\InSansModefalse\mathversion{normal}}
\let\oldrm\rmfamily
\renewcommand*{\rmfamily}{\oldrm\InSansModefalse\mathversion{normal}}

\usepackage[textwidth=9cm]{geometry}

\begin{document}
\section{Inline-$m\alpha \tau h$}
Normal: A $Ax\alpha+\chi b$

\sffamily Sans: A $Ax\alpha+\chi b$

\bfseries Bold-Sans: A $ Ax\alpha+\chi b$

\normalfont Normal: A $ Ax\alpha+\chi b$

\bfseries  Bold: A $ Ax\alpha+\chi b$

\normalfont Normal: A $ Ax\alpha+\chi b$

\section{Display-$\mu\alpha\tau\eta$}
Text in roman family
\begin{equation}
     \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}

\sffamily
Text in sans family
\begin{equation}
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}

\sffamily\bfseries
Text in sans family, bold version
\begin{equation}
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}

\rmfamily\bfseries
Text in roman family, bold version
\begin{equation}
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}
\end{document}

output


For comparison, this is how one can switch from sans to serif math and back in ConTeXt.

\def\N{{\bf N}}
\def\R{{\bf R}}
\def\Q{{\bf Q}}
\def\Z{{\bf Z}}
\def\C{{\bf C}}

\startbuffer
Some random text: $\R, \Q, \N, \Z, \C$ are sets.
\stopbuffer

% To format the examples
\setuphead[subject][style=bold, after=]

\starttext

\subject{Default} \getbuffer

\subject{Sans math}

\switchtobodyfont[ss] 
\appendtoks \ss \to \everymathematics
\getbuffer

\subject{Serif math}
\switchtobodyfont[rm]
\appendtoks \rm \to \everymathematics
\getbuffer


\stoptext

enter image description here

You can also use group {...} to limit the effect of font changes.