17

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.

7
  • @Verena: Welcome to tex.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting.
    – lockstep
    Mar 30, 2011 at 12:28
  • It is possible to define math versions (like \boldmath). But if you want a more specific answer you will have to give much more detailed informations about your actual setup. Btw: I don't think that is a good idea to use too much font families in a document. Mar 30, 2011 at 12:44
  • You already wrote that you want another font in the example. The question is how exactly should the math look. Which fonts should the letters, the numbers and the various symbols use? Make a small complete document which shows equation in the look you want for your example environment. Mar 30, 2011 at 13:03
  • @Ulrike: Right, sorry. Now I get it. Basically, I'm not picky as to the exact font type I want to use. Ideally, I'll just go for one that is easily accessible, already present in my MikTeX distribution (if such a thing is possible?) and works well with some font type I can choose via the \fontfamily command. Does an answer to my question depend on which mathfont I want to use? I was hoping for a general solution that does not depend on the precise choice of a certain mathfont. I've prepared an example as pdf now as you asked me to but... how do I share it with you?
    – user4544
    Mar 30, 2011 at 13:22
  • A pdf example is not useful. Make a small complete latex document. And no: it is not easy and there is no general solution. You mentioned the family ppl so I suggest that you search for mathpazo.sty on your machine, open it in your editor and look what it does to setup the math. Mar 30, 2011 at 13:32

2 Answers 2

7

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.

6

With the instructions below, only in Example environment the main font (of text and math formulas) will be changed into computer modern sans one.

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

However, dots and commas are not correct: their output is colon and semicolon because of the position of these digits into the computer modern sans font table. You may correct this error with

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

although, in my opinion, it still sounds like an "error".

You must log in to answer this question.