24

How can one make the math mode change its font according to the surrounding text?

I have to use (pdf)latex. xelatex, luatex etc. are not possible alternatives in this case.

\documentclass{scrartcl}

\usepackage{lmodern}
%\usepackage{sfmath}
%\usepackage{cmbright}

\newcommand{\test}{%
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
}

\begin{document}
Text in roman family and math too $\test$
\begin{equation}
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}

\sffamily
Text in sans family and math should be in sans too $\test$
\begin{equation}
    \sqrt{(a_1+a_2+\gamma)^2}=\sum_{i=1}^2 a_i + \gamma
\end{equation}

\normalfont
Back to roman $\test$
\end{document}

I found sfmath but it only changes the latin letters and numbers and it works only as a global setting. cmbright contains a more or less matching (and working) math font but it changes the text font also and it works only globally too.

2
  • 2
    You should be aware that in math the font family can cary a meaning: a "A" in roman can be a different mathematical object than a "A" in typewriter. So you should be really careful when switching the math setup. In general adapting math "to the surroundig" text is not a good idea. Oct 31, 2011 at 10:45
  • @Ulrike: Hi, thanks for this advice. I’m familiar with this convention and support it in most cases. In this special case the formulas (mostly only one variable) are part of figures and graphics. The font inside all graphics has to be sans and it looks not good if the math text is in roman. I’ sure that there’ll be no confusion since the book deals only with simple math so that there is no differentiation between roman and serif necessary. I’ll ad an example to my question later this day.
    – Tobi
    Oct 31, 2011 at 14:35

3 Answers 3

18

If you use the Latin Modern or Computer Modern fonts, you can switch to CMBright:

\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}

(and Iwona for the large symbols); then a declaration such as

\sffamily\mathversion{sans}

will do; it respects grouping, but also \normalfont\mathversion{normal} will get back to normal.

enter image description here

10
  • Thank you very much! It seems to work perfectly :-) Could you explain how \DeclareMathVersion knows which of the settings belong to the serif version?
    – Tobi
    Oct 31, 2011 at 17:19
  • \DeclareMathVersion just sets up things so that it's possible to add math fonts to it with \Set... commands. It's a feature used normally for \mathversion{bold}.
    – egreg
    Oct 31, 2011 at 17:22
  • So every following \Set.. macros belong to the latest \DeclareMathVersion command?
    – Tobi
    Oct 31, 2011 at 17:32
  • @Tobi The math version is specified among the arguments of the \Set... commands.
    – egreg
    Oct 31, 2011 at 17:35
  • A moment ago I found it out my self … Thanks again!
    – Tobi
    Oct 31, 2011 at 17:48
16

This is what I compiled form my own ideas and egreg’s answer to make the mathfont automatically change to sans and/or bold font.

\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}

result

1

With unicode-math, you can declare a new math version as

\defaultfontfeatures{Scale = MatchLowercase}
\setmathfont[version=sans]{Fira Math}

You can then switch to it with \mathversion{sans} and re-use the rest of the code from the other answers, or define commands corresponding to \boldmath and \boldsymbol.

If you also want a bold sans-serif version, there is no OpenType math font that works out of the box. As of June 2020, the only sans-serif math font is Fira Math, but the only bold math fonts are XITS, Libertinus and Minion. Furthermore, range= and version= do not work together, so you cannot load Libertinus Math Bold with the letters from Libertinus Serif Bold (Italic).

For the time being, you can apply FakeBold to Fira Math:

 \setmathfont[version=boldsans, FakeBold=1.2]{Fira Math}

This is useful if you want to use math mode inside a bold sans-serif heading. You can change the formatting to \sffamily\bfseries\mathversion{boldsans} to have math that matches the text.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .