仅在文档的某些部分更改数学字体?

仅在文档的某些部分更改数学字体?

在包含大量公式的大型文档中,我时常使用自定义的“示例”环境来进行说明。为了更好地将其与其余文本区分开来,我决定在示例环境中使用另一种字体系列。

但是 - 这是我的问题 - 我使用的代码只更改了文本字体。如何在定义的环境中为公式使用另一种数学字体?

\newenvironment{Ex}{\fontfamily{ppl}\selectfont\par\vspace{1ex}\begin{quote}\textbf{Example: }}{\end{quote}\par}

提前感谢你的帮助!

编辑:有关我的问题的更多详细信息。让我举一个例子来说明一下:

[Here's some default-formatted text]
    \begin{Example}
     [some example text I want in another font family]
     \begin{equation}
      and now I need a formula within the example-environement set in a different style than the standard layout to underline it's still part of the example-surrounding
     \end{equation}
     [maybe some more text within the example]
    \end{Example}
    [And now back to the main body of the text set once again in the default layout]

我希望这可以澄清(而不是混淆)我的问题。

答案1

您可以使用unicode 数学包。您需要使用xelatexlualatex才能使其工作(而不仅仅是pdflatex)。作为一个相当简单的例子,以下内容在示例环境中从斜体切换到直立字体。

\documentclass{minimal}
\usepackage{unicode-math}

\setmathfont{xits-math.otf}

\newenvironment{example}{\par\textbf{Example:}\setmathfont[math-style=upright]{xits-math.otf}}{\par}

\begin{document}

\[
  E = m c^2
\]

\begin{example}
A famous equation is
%
\[
  E = m c^2
\]
\end{example}

\[
  E = m c^2
\]

\end{document}

(您需要有 STIX 字体才能使这个特定示例工作。)这将产生:

改变数学字体

有关语法的更多信息,我建议您阅读unicode-math文档。我还应该警告您,在文档中多次更改字体大大减慢了编译时间。它可能不是,我不确定到底是什么导致了速度变慢,但是在一些文档中,我经常更改数学字体,然后我发现我必须在编写文档时禁用字体更改,并且只在最后一次运行时启用它。

答案2

按照以下说明,仅在示例环境中,主字体(文本和数学公式)才会更改为 computer modern sans 字体。

    \DeclareMathVersion{sfmath}
    \DeclareSymbolFont{sfletters}{OT1}{cmss}{m}{sl}         
        \SetSymbolFont{letters}{sfmath}{OT1}{cmss}{m}{sl}   
    \DeclareSymbolFont{sfoperators}{OT1}{cmss}{m}{n}
        \SetSymbolFont{operators}{sfmath}{OT1}{cmss}{m}{n}  
    \SetMathAlphabet\mathit{sfmath}{OT1}{cmss}{m}{sl}
    \SetMathAlphabet\mathrm{sfmath}{OT1}{cmss}{m}{n}
    \SetMathAlphabet\mathbf{sfmath}{OT1}{cmss}{bx}{n}
    %\SetMathAlphabet\mathbb{sfmath}{OT1}{cmss}{bx}{n}

    \DeclareSymbolFont{greek}{OML}{cmr}{m}{n}   
    \DeclareMathSymbol{\alpha}{0}{greek}{"0B}
    \DeclareMathSymbol{\beta}{0}{greek}{"0C}
    \DeclareMathSymbol{\gamma}{0}{greek}{"0D}
    \DeclareMathSymbol{\delta}{0}{greek}{"0E}
    \DeclareMathSymbol{\epsilon}{0}{greek}{"0F}
    \DeclareMathSymbol{\zeta}{0}{greek}{"10}
    \DeclareMathSymbol{\eta}{0}{greek}{"11}
    \DeclareMathSymbol{\theta}{0}{greek}{"12}
    \DeclareMathSymbol{\iota}{0}{greek}{"13}
    \DeclareMathSymbol{\kappa}{0}{greek}{"14}
    \DeclareMathSymbol{\lambda}{0}{greek}{"15}
    \DeclareMathSymbol{\mu}{0}{greek}{"16}
    \DeclareMathSymbol{\nu}{0}{greek}{"17}
    \DeclareMathSymbol{\xi}{0}{greek}{"18}
    \DeclareMathSymbol{\pi}{0}{greek}{"19}
    \DeclareMathSymbol{\rho}{0}{greek}{"1A}
    \DeclareMathSymbol{\sigma}{0}{greek}{"1B}
    \DeclareMathSymbol{\tau}{0}{greek}{"1C}
    \DeclareMathSymbol{\upsilon}{0}{greek}{"1D}
    \DeclareMathSymbol{\phi}{0}{greek}{"1E}
    \DeclareMathSymbol{\chi}{0}{greek}{"1F}
    \DeclareMathSymbol{\psi}{0}{greek}{"20}
    \DeclareMathSymbol{\omega}{0}{greek}{"21}
    \DeclareMathSymbol{\varepsilon}{0}{greek}{"22}
    \DeclareMathSymbol{\vartheta}{0}{greek}{"23}
    \DeclareMathSymbol{\varpi}{0}{greek}{"24}
    \DeclareMathSymbol{\varrho}{0}{greek}{"25}
    \DeclareMathSymbol{\varsigma}{0}{greek}{"26}
    \DeclareMathSymbol{\varphi}{0}{greek}{"27}

    \DeclareSymbolFont{Greek}{OT1}{cmr}{m}{n}
    \DeclareMathSymbol{\Gamma}{0}{Greek}{"00}
    \DeclareMathSymbol{\Delta}{0}{Greek}{"01}
    \DeclareMathSymbol{\Theta}{0}{Greek}{"02}
    \DeclareMathSymbol{\Lambda}{0}{Greek}{"03}
    \DeclareMathSymbol{\Xi}{0}{Greek}{"04}
    \DeclareMathSymbol{\Pi}{0}{Greek}{"05}
    \DeclareMathSymbol{\Sigma}{0}{Greek}{"06}
    \DeclareMathSymbol{\Upsilon}{0}{Greek}{"07}
    \DeclareMathSymbol{\Phi}{0}{Greek}{"08}
    \DeclareMathSymbol{\Psi}{0}{Greek}{"09}
    \DeclareMathSymbol{\Omega}{0}{Greek}{"0A}

    \DeclareSymbolFont{mainsymbols}{OT1}{cmr}{m}{n}     
    \DeclareMathSymbol{+}{2}{mainsymbols}{"2B}
    \DeclareMathSymbol{=}{3}{mainsymbols}{"3D}

    \DeclareSymbolFont{othersymbols}{OML}{cmr}{m}{n}        
    \DeclareMathSymbol{<}{3}{othersymbols}{"3C}
    \DeclareMathSymbol{/}{0}{othersymbols}{"3D}
    \DeclareMathSymbol{>}{3}{othersymbols}{"3E}
    \DeclareMathSymbol{\triangleright}{3}{othersymbols}{"2E}
    \DeclareMathSymbol{\partial}{0}{othersymbols}{"40}
    \DeclareMathSymbol{\ell}{0}{othersymbols}{"60}

    \DeclareSymbolFont{parentesi}{OT1}{cmr}{m}{n}       
    \DeclareMathSymbol{(}{4}{parentesi}{"28}
    \DeclareMathSymbol{)}{5}{parentesi}{"29}
    \DeclareMathSymbol{[}{4}{parentesi}{"5B}
    \DeclareMathSymbol{]}{5}{parentesi}{"5D}

    \DeclareMathDelimiter{(}{\mathopen}{parentesi}{"28}{largesymbols}{"00}
    \DeclareMathDelimiter{)}{\mathclose}{parentesi}{"29}{largesymbols}{"01}
    \DeclareMathDelimiter{[}{\mathopen}{parentesi}{"5B}{largesymbols}{"02}
    \DeclareMathDelimiter{]}{\mathclose}{parentesi}{"5D}{largesymbols}{"03}

    \newenvironment{Example}{%
    \mathversion{sfmath}%
    \bigbreak\noindent\textbf{Example.}}
    {\par}

但是,点和逗号不正确:它们的输出是冒号和分号,因为这些数字在计算机现代 sans 字体表中的位置。您可以使用以下方法更正此错误

    \DeclareMathSymbol{,}{6}{mainsymbols}{"2C}
    \DeclareMathSymbol{.}{6}{mainsymbols}{"2E}

尽管在我看来,这仍然听起来像是一个“错误”。

相关内容