[Tex/LaTex] Defining subfamilies for a font

fontspecxetex

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?

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

enter image description here

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}
Related Question