如何用无衬线字体排版一些包含数学内容的文本

如何用无衬线字体排版一些包含数学内容的文本

我在外部宏中有一小段文字 \SomeText,我想将其排版为无衬线字体。但是,由于我没有处于数学模式,因此\sffamily\SomeText不会更改数学字体,并且\sffamily\mathsf{\SomeText}不起作用。

那么,无论是否包含数学内容,我该如何用无衬线字体排版材料呢?

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

答案1

好的,我在发布问题后很短的时间内找到了解决方案:该sansmath\sansmath为此目的提供了命令(和几个环境)。

我仍然很好奇是否存在某种不依赖于附加包的解决方案。

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

答案2

看一下让 mathfont 尊重周围的家庭。您将在那里找到改变数学字母表的解决方案,并让普通字体切换(如\sffamily)也改变数学字体。

摘自:

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

结果

答案3

除了@Tobi 的出色回答之外,如果您需要使用 helvetica 作为无衬线字体:

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

(取自sansmathfonts.stysansmathfonts)。然后将其与排版的文本混合\fontfamily{phv}\selectfont\mdseries(记住仍然用于\sffamily数学)

例子:

在此处输入图片描述

相关内容