14

I have a blurb of text in an external macro \SomeText, which I want to typeset in sans serif. However, \sffamily\SomeText does not change the math font and \sffamily\mathsf{\SomeText} does not work, as I am not in math mode.

So how can I typeset material in sans serif, whether it does or does not include math stuff?

\documentclass{article}
\usepackage{lmodern}

\newcommand{\SomeText}{This is some ($words \geq 5$) Text with math stuff in it: $a^2+b^2=c^2$}

\begin{document}
  \begin{description}
    \item[Roman:] \SomeText
    \item[Sans:] {\sffamily \SomeText \\ (however, the math is still Roman)}

%    LaTeX Error: \mathsf allowed only in math mode
%    \item[AllSans:] {\sffamily\mathsf \SomeText}
  \end{description}
\end{document}
  • Is all math stuff to be set in sans-serif, or just one small blurb? – Mico Jan 18 '12 at 15:49
  • @Mico: Only small blurbs. I do not want to change the math font globally, I just want sans serif math when \sffamily is active. – Daniel Jan 18 '12 at 15:51
11

Okay, I found a solution in embarrassingly short time after posting the question: The sansmath package provides the \sansmath command (and several environments) for this purpose.

I am yet curious to know if there is some solution that does not depend on an additional package.

\documentclass{article}
\usepackage{lmodern}
\usepackage{sansmath}
\newcommand{\SomeText}{This is some text with math stuff in it. 
    $x>5$, $a^2+b^2=c^2$}

\begin{document}
  \begin{description}
    \item[Roman:] \SomeText
    \item[Sans:] {\sffamily \SomeText \\ However, the math is still Roman.}
    \item[AllSans:] {\sffamily\sansmath \SomeText}
  \end{description}
\end{document}
  • I was going to suggest you consider using the sansmath package! :-) – Mico Jan 18 '12 at 16:11
  • This solution does not work well because, for me, it replaces characters: . (dot) becomes ( (parenthesis) and comma a "plus minus" symbol. – lauhub Feb 15 '15 at 14:44
10

Have a look at Make mathfont respect the surrounding family. There you’ll find a solution to change the math alphabet and also to make the normal font switches (like \sffamily) change the math font too.

Taken from there:

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

  • Thanks Tobi. For some reasons I did not find this question, which comes close to an exact duplicate. – Daniel Jan 18 '12 at 16:35
4

An addition to @Tobi's excellent answer, if you need to use helvetica for the Sans serif fonts:

\DeclareMathVersion{sans}
\SetSymbolFont{operators}{sans}{OT1}{cmsmf}{m}{n}
\SetSymbolFont{letters}{sans}{OML}{cmssm}{m}{it}
\SetSymbolFont{symbols}{sans}{OMS}{cmsssy}{m}{n}
\SetMathAlphabet{\mathit}{sans}{OT1}{cmssm}{m}{sl}
\SetMathAlphabet{\mathbf}{sans}{OT1}{cmsssy}{bx}{n}
\SetMathAlphabet{\mathtt}{sans}{OT1}{cmssex}{m}{n}
\SetSymbolFont{largesymbols}{sans}{OMX}{cmssex}{m}{n}

(taken from sansmathfonts.sty from the sansmathfonts package). This then blends in with text that is typesetted with \fontfamily{phv}\selectfont\mdseries (remember to still use \sffamily for the maths)

Example:

enter image description here

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.