2

How can you change the font of MATH and Text of a section of your document?

Using the default packages (LaTeX) or system fonts (XeLaTex)?

David R
  • 149
  • Yes it is true that with setmainfont you can change the font of the entire document, but how can I change the font of math and text for just a part of the document? – David R Apr 26 '17 at 09:52
  • Sorry, folks, but this posting is not a duplicate of some earlier posting. Note that this posting is about changing both text and math fonts, as well as about how to make the change apply only locally, not globally for the remainder of the document. Any earlier postings (and associated answers) you may have thought of as antecdents to this posting were strictly about changing just the text fonts. – Mico Apr 26 '17 at 10:49

1 Answers1

2

If you can use either XeLaTeX or LuaLaTeX, you may employ \begingroup and \endgroup statements to limit the scope of a \setmainfont instruction. To keep the change of the math font "local" to a (TeX) group, don't use \setmathfont directly. Instead, set up all math versions in the preamble and employ \mathversion directives in the body of the document.

An MWE (minimum working example), to be compiled with either XeLaTeX or LuaLaTeX.

enter image description here

\documentclass{article}
\usepackage{unicode-math} % load 'fontspec' automatically
\setmathfont{Latin Modern Math}[version=LM]
\setmathfont{XITS Math}[version=XITS]

\newcommand\qbf{The quick brown fox jumps over the lazy dog.\quad $E=mc^2$.\par}
\begin{document}
\setmainfont{Latin Modern Roman}
\mathversion{LM}

\qbf

\begingroup
\setmainfont{XITS} % Times Roman clone
\mathversion{XITS}
\qbf
\endgroup

\qbf  % back to Latin Modern font family for both text and math
\end{document}
Mico
  • 506,678
  • Thanks a lot. I compiled using both Xelatex and lulatex and got errors. Can you please tell me if ''Latin Modern Math'' and "XITS Math" are the names of two system fonts? Do I need them to be installed? – David R Apr 26 '17 at 11:41
  • Is there anyway to use the system fonts? Like using \usepackage[MnSymbol]{mathspec}? – David R Apr 26 '17 at 11:45
  • @DavidR - The MWE works on a MacTeX2016 system. Which OS and which TeX distribution (of which vintage) do you have? – Mico Apr 26 '17 at 11:45
  • I use TexLive and TexShop on a Mac. So I should not receive any errors? – David R Apr 26 '17 at 20:11
  • @DavidR - ''Latin Modern Math'' and "XITS Math" are both fonts that should be part of any MacTeX2016 distribution. Does the example doc compile successfully under LuaLaTeX on your computer? – Mico Apr 26 '17 at 20:18
  • No I copied and pasted in a new document and receive the following error when I tried to compiled it with LuaLaTeX

    "LaTeX Error: Missing \begin{document}. ..... 1.3 \setmathfont{Latin Modern Math}[version=LM] ?"

    And if I press enter, another error "1.15 \mathversion{LM} ?"

    And at the end all math fonts will be times new roman

    – David R Apr 26 '17 at 22:06
  • @DavidR - Which vintage of TeXLive (or MaacTeX?) is on your system? – Mico Apr 26 '17 at 23:06
  • TeXShop 3.77 and I think TexLive 2015 – David R Apr 27 '17 at 00:43
  • @DavidR -- Ouch. Any chance you could run a massive update to MacTeX2016 or the prerelease version of MacTeX2017? – Mico Apr 27 '17 at 02:16
  • Thanks a lot. Apparently something is wrong with TeXLive on my mac because although I updated it to 2016 version, it still does not work. I could compile the file and see the result on Ubuntu. – David R Apr 27 '17 at 19:22
  • Is there anyway to use "euler" or "eulervm" for a part of the document? – David R Apr 27 '17 at 19:26
  • @DavidR - There's an OpenType math font called "Neo Euler" -- filename /Users/mico/Library/Fonts/euler.otf. I understand it's supposed to be the OpenType equivalent of the set of glyphs provided by the eulervm package. That said, I've never used the "Neo Euler" math font for any work yet. – Mico Apr 27 '17 at 19:35
  • In your answer, is it enough to just use {...} instead of \begingroup...\endgroup in your MWE? – Diaa Oct 13 '19 at 20:05
  • @Diaa - Yes, it is. – Mico Oct 13 '19 at 20:19