1

I have written a document in serif as the base font for math as well (libertine). In this document I have defined a coloured environment which is sans (josefin sans). Now within that sans environment the default math is serif.

I would want it to be sans. I tried to force it via $\textsf{F} = \textsf{ma}$ but the numbers go for a toss and is not elegant.

Using $\mathsf{}$ results in defaulting to cm sans font, even if this can default to josefin it should be ok. Is there any easier way to achieve this in the definition of the environment itself?

I also looked at this similar question, but josefin does not seem to have sansmath option as it is not a mathfont.

I don't want sans as the default font for rest of the document so \renewcommand{\rmdefault}{\sfdefault} will not work for me. I prefer to use pdflatex.

MWE

\documentclass{article}
\usepackage{libertine}
\usepackage{libertinust1math}
\usepackage[T1]{fontenc}
\usepackage[medium]{josefin}
\usepackage{xcolor}

\newcommand{\hlred}[1]{\textcolor{red}{\sffamily #1}}% prints in red


\begin{document}
This is regular text with serif math font $F = ma$

\hlred{This is the coloured sans environment. The regular math here appears in serif $F = ma$. Only with textsf, it appears in sans $\textsf{F} = \textsf{ma}$. With mathsf, it defaults to computer modern sans $\mathsf{F = ma}$.}

\end{document}

enter image description here

2 Answers 2

3

Hope this can meet your requirement, please excuse if not so:

\documentclass{article}
\usepackage{libertine}
\usepackage{libertinust1math}
\usepackage[T1]{fontenc}
\usepackage[]{helvet}%substitute Helvetica as I don't have the specified font
\usepackage{sansmath}
\usepackage{xcolor}

\newcommand{\hlred}[1]{\textcolor{red}{\begin{sansmath}\sffamily\selectfont#1\end{sansmath}}}% prints in red


\begin{document}
This is regular text with serif math font $F = ma$

\hlred{This is the coloured sans environment. The regular math here appears in serif $F = ma$. Only with textsf, it appears in sans $F = ma$. With mathsf, it defaults to computer modern sans $F = ma$.}

\end{document}
1

Here is a version that works in LuaLaTeX or XeLaTeX, with OpenType fonts: It uses KP Math Sans as the sans-serif math version.

\documentclass{article}
\usepackage{xcolor}
\usepackage{unicode-math}
\usepackage{libertinus}
\defaultfontfeatures{Scale=MatchLowercase}
\usepackage[medium]{josefin}

\setmathfont{KPMath-Sans}[version=sans]

\newcommand\sansmath{\mathversion{sans}}
\newcommand{\hlred}[1]{\textcolor{red}{\sffamily\sansmath #1}}% prints in red sans-serif


\begin{document}
This is regular text with serif math font $F = ma$

\hlred{This is the coloured sans environment. The regular math here appears in
serif $F = ma$. Only with textsf, it appears in sans $\textsf{F} = \textsf{ma}$
With mathsf, it defaults to computer modern sans $\mathsf{F = ma}$.}

\end{document}

Libertinus/Josefin/KP Sans sample

And here is the equivalent, for PDFLaTeX:

\documentclass{article}
\usepackage{xcolor}
\usepackage[notext]{kpfonts}
\usepackage{libertinus}
\usepackage[medium,type1]{josefin}

\newcommand\sansmath{\mathversion{sf}}
\newcommand{\hlred}[1]{\textcolor{red}{\sffamily\sansmath #1}}% prints in red sans-serif


\begin{document}
This is regular text with serif math font $F = ma$

\hlred{This is the coloured sans environment. The regular math here appears in
serif $F = ma$. Only with textsf, it appears in sans $\textsf{F} = \textsf{ma}$
With mathsf, it defaults to computer modern sans $\mathsf{F = ma}$.}

\end{document}

Josefin/Libertinus/kpfonts sample

You must log in to answer this question.

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