9

I would like to switch easily to sans-serif font, even for the mathematical symbols. Is it possible?

So far is what I have tried :

\documentclass{article}

\def\mySfFamily{\fontfamily{cmbr}\selectfont\sffamily}
\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

But as you see, it doesn't work for maths. I have chosen the font \fontfamily{cmbr} because of this post.

  • Since you are using only boldface letters in math mode you can use \def\N{\textbf{\textsf{N}}} – Sigur Oct 15 '13 at 22:26
  • @Sigur : I also use $x\in\R$ and I want to switch easily between serif and sans-serif mode. – Colas Oct 16 '13 at 9:24
  • 1
    The style of math fonts usually carries information (such as bold for vectors, italic for scalar variable, etc). As a reader, I would be confused if you wrote $x\in\R$ where the R was serif and then later read $x\in\R$ where the R was san-serif. It would make me think the author had two different (though possibly related) sets in mind. – A.Ellett Oct 16 '13 at 17:52
  • I see your point of view (even though I don't agree : if the context is non-serif, I will understand that $\R$ sans serif denotes the same object as a usual $\R$). Do you know if it is possible? – Colas Oct 17 '13 at 14:29
  • What about using \mathsf{} inside the math environment? – Chris Oct 24 '13 at 11:35
9
+150

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

  • @Colas: Is using sansmath an option for you or is anything missing from this solution? – Daniel Oct 25 '13 at 11:28
7

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

6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.