[Tex/LaTex] Change math font only in some parts of a document

fontsmath-mode

Within a larger document with lots of formulas, I use a self-defined "Example" environement for illustrational purposes from time to time. In order to better set it apart from the rest of the text, I have decided to use another font family within those example surroundings.

But – and here's my problem – the code I use only changes the text font. How can I use another math font for the formulas within the defined enviromenent?

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

Thanks in advance for any help!

Edit: More details regarding my question. Let me illustrate it with an example:

[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]

I hope this clarifies (rather than confuses) my question.

Best Answer

You can do this using the unicode math package. You need to use xelatex or lualatex for this to work (rather than just pdflatex). As a rather simple example, the following switches from italic to upright font within the example environment.

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

(You need to have the STIX fonts for this particular example to work.) This produces:

changing maths font

For more on the syntax, I recommend that you read the unicode-math documentation. I should also warn you that changing the font several times in a document can slow down the compilation time considerably. It might not, I'm not sure exactly what it is that causes the slow-down, but on some documents where I change maths font a lot then I find that I have to disable the font changing when writing the document and only enable it for the final run.