You can make use of the XeTeX character class mechanism, which allows to insert tokens into the input stream based on user-defined character classes. In this case, we define a new class for uppercase letters and prepare code to be inserted before and after these letters:
\documentclass{article}
\usepackage{fontspec}
% Our font for uppercase letters
\newfontfamily\upfont[Ligatures=TeX,Color=FF0000]{Latin Modern Roman}
\XeTeXinterchartokenstate=1
\newXeTeXintercharclass \uppercaseclass
% Assign the new class to all Latin capital letters
\makeatletter
\@tempcnta=`\A
\loop\unless\ifnum\@tempcnta>`\Z
\XeTeXcharclass \@tempcnta \uppercaseclass
\advance \@tempcnta by 1
\repeat
\makeatother
% Setup font change
\XeTeXinterchartoks 0 \uppercaseclass = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 0 = {\endgroup}
\XeTeXinterchartoks 255 \uppercaseclass = {\begingroup\upfont}
\XeTeXinterchartoks \uppercaseclass 255 = {\endgroup}
\begin{document}
Once Upon a Time, there was a Beautiful Princess\ldots
\end{document}
The mechanism is described in the XeTeX reference.
Use mathspec
package. Note however that most fonts include only Latin, Greek alphabets that can be used with mathspec
. If you want the math fonts, alphabets and symbols alike, to be consistent, you need unicode-math
package together with an OpenType fonts with MATH table. Such fonts are relatively rare, and the only font with complete Unicode coverage is XITS/STIX.
Basic usage of mathspec
:
\usepackage{mathspec}
\setmainfont{Broadway}
\setmathsfont(Digits,Latin,Greek)[Numbers={Lining,Proportional}]{Broadway}
Several comments:
- Do not load
fontspec
package. It will clash with mathspec
.
- Symbols not available in the text fonts will fall back to Computer Modern. If you like the operators provided by
mnsymbol
package, which is designed to work with Minion Pro, use the option \usepackage[MnSymbol]{mathspec}
.
Basic usage of unicode-math
:
This package is for fonts that contain an OpenType MATH table. Currently only these fonts are available:
The last one is recommended for its complete coverage and times-like appearance (most professional books use times-like fonts for math).
\usepackage{unicode-math}
\setmainfont{XITS}
\setmathfont{XITS Math}
\setmathfont[range={\mathcal,\mathbfcal},StylisticSet=1]{XITS Math}
The last line is only usable with XITS Math
or STIX
. It makes sure that \mathcal and \mathscr looks different and be consistent with looks in traditional TeX fonts.
Miscellaneous comments on your code:
- Don't use
inputenc
with XeTeX or LuaTeX. The utf8 option of inputenc
is a compilation of dirty hacks that implements a tiny fraction of Unicode while XeTeX natively suuports UTF-8 and UTF-16.
- There is no need to specify
xetex
with graphicx
package. In modern distributions it auto detects.
- Your alternative solutions all utilize legacy code, like T1 fontenc, which should be avoided with
xetex
.
Best Answer
Using
fontspec
, you can define any font you want in XeTeX. You can then use them in the places of your choice by using the macro names you chose.If you want to change the fonts used in sections, you can combine
fontspec
withtitlesec
. Example:will use Linux Libertine O as main font and EB Garamond as section font.