I wish to import from files a font family, which comes in extra light, light, regular, medium, bold and black. Instead of defining several different font names and choosing them as I need, it would be great if I could somehow define a single family and then attribute when I used different weights. Is this possible? I am using xelatex with fontspec. Is there an alternative to fontspec that offers an easy solution?
1 Answer
The manual of fontspec
mentions the key FontFace
. So let's assume your font are
BestFont-ExtraLight.otf
BestFont-ExtraLightItalic.otf
BestFont-Light.otf
BestFont-LightItalic.otf
BestFont-Regular.otf
BestFont-Italic.otf
BestFont-Medium.otf
BestFont-MediumItalic.otf
BestFont-Bold.otf
BestFont-BoldItalic.otf
BestFont-Black.otf
BestFont-BlackItalic.otf
Then you should be able to provide the font definition as
\setmainfont{BestFont}[
Extension=.otf,
UprightFont=*-Regular,
ItalicFont=*-Italic,
BoldFont=*-Bold,
BoldItalicFont=*-BoldItalic,
FontFace={xl}{n}{*-ExtraLight},
FontFace={xl}{it}{*-ExtraLightItalic},
FontFace={l}{n}{*-Light},
FontFace={l}{it}{*-LightItalic},
FontFace={mb}{n}{*-Medium},
FontFace={mb}{it}{*-MediumItalic},
FontFace={k}{n}{*-Black},
FontFace={k}{it}{*-BlackItalic},
]
A real world example:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Raleway}[
Extension=.otf,
UprightFont=*-Regular,
ItalicFont=*-Regular-Italic,
BoldFont=*-Bold,
BoldItalicFont=*-Bold-Italic,
FontFace={xl}{n}{*-ExtraLight},
FontFace={xl}{it}{*-ExtraLight-Italic},
FontFace={l}{n}{*-Light},
FontFace={l}{it}{*-Light-Italic},
FontFace={mb}{n}{*-Medium},
FontFace={mb}{it}{*-Medium-Italic},
FontFace={k}{n}{*-Black},
FontFace={k}{it}{*-Black-Italic},
]
\begin{document}
\newcommand{\test}[2]{%
#1: {\fontseries{#2}\selectfont This is upright. \itshape This is italic}\par
}
\test{ExtraLight}{xl}
\test{Light}{l}
\test{Regular}{m}
\test{Medium}{mb}
\test{Bold}{bx}
\test{Black}{k}
\end{document}
Now you can also define font switching commands by yourself:
\DeclareRobustCommand\xlseries{\fontseries{xl}\selectfont}
\DeclareTextFontCommand{\textxl}{\xlseries}
\DeclareRobustCommand\lseries{\fontseries{l}\selectfont}
\DeclareTextFontCommand{\textl}{\lseries}
\DeclareRobustCommand\mbseries{\fontseries{mb}\selectfont}
\DeclareTextFontCommand{\textmb}{\mbseries}
\DeclareRobustCommand\kseries{\fontseries{k}\selectfont}
\DeclareTextFontCommand{\textk}{\kseries}
-
Great answer! Thank you! Will the commands
\textmd
and\textlf
also automatically work after this? Sep 2, 2015 at 19:08 -
-
So, in other words, there is no way to import a font from local files that would make the latex font weight commands to work out of the box beyond medium and bold? Sep 3, 2015 at 9:53
-
1@SubhamoySengupta There are so many variations, especially in font weight naming, that attempting to cover them is hopeless.– egregSep 3, 2015 at 9:54
-
1@SubhamoySengupta I added a model for font switching command definitions. For instance,
\textxl{foo}
will print in ExtraLight.– egregSep 3, 2015 at 10:00