我还用衬线字体编写了一份文档,作为数学的基本字体(libertine)。在这个文档中,我定义了一个无衬线字体的彩色环境(josefin sans)。现在,在该无衬线字体环境中,默认数学字体是衬线字体。
我希望它是 sans。我尝试过强制通过,$\textsf{F} = \textsf{ma}$
但数字乱七八糟,不太优雅。
使用$\mathsf{}$
会导致默认使用 cm sans 字体,即使可以默认使用 josefin 也应该没问题。有没有更简单的方法可以在环境本身的定义中实现这一点?
我也看了这个类似的问题,但是 josefin 似乎没有 sansmath 选项,因为它不是数学字体。
我不希望 sans 作为文档其余部分的默认字体,因此\renewcommand{\rmdefault}{\sfdefault}
对我来说不起作用。我更喜欢使用 pdflatex。
平均能量损失
\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}
答案1
希望这可以满足您的要求,如果不行,请原谅:
\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}
答案2
这是一个适用于 LuaLaTeX 或 XeLaTeX 并带有 OpenType 字体的版本:它使用 KP Math Sans 作为无衬线数学版本。
\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}
对于 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}