3

I have a few specific font families that I'd like to reuse across different documents. Rather than copy-pasting the font definition in every document, I assumed I would do something like this in a shared e.g. montserrat.sty file:

\newfontfamily{\Montserrat}[
  Path=fonts/montserrat/,
  Extension = .ttf,
  Ligatures = TeX,
  UprightFont = *-Regular,
  BoldFont = *-Bold,
  ItalicFont = *-Italic,
  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},
]{Montserrat}

With this, I can use the font just fine in a Beamer theme:

\RequirePackage{montserrat}
\setbeamerfont{title}{family=\Montserrat, size={\fontsize{48}{48}}, series=\bfseries}

But I can't seem to figure out a way to use it as the main font of a regular text document:

\documentclass{article}

\usepackage{fontspec}
\usepackage{montserrat}

\setmainfont{\Montserrat}

fails with [fontspec] The font "\Montserrat " cannot be found. I could duplicate the same arguments I passed to newfontfamily in my call to setmainfont, but that's exactly what I'm trying to avoid - I have a few different documents reusing a few different fonts.

Is there any way to set a font defined through newfontfamily as the main font of a document?

3
  • Welcome to TeX.SE.
    – Mico
    Oct 16, 2022 at 23:39
  • Incidentally, have you tried \setmainfont{Montserrat} instead of \setmainfont{\Montserrat}?
    – Mico
    Oct 16, 2022 at 23:48
  • \setmainfont{Montserrat} requires the font to be installed on the system, and ignores any previous \newfontfamily call - hence even if the font was installed on the system, I wouldn't necessarily have all the different weights and shapes defined. Oct 17, 2022 at 3:05

2 Answers 2

3

Here's the skeleton for your montserrat.sty file

\ProvidesPackage{montserrat}

\RequirePackage{fontspec}
\newfontfamily{\Montserrat}[
  NFSSFamily=Montserrat,% <--- the key
  %Path=fonts/montserrat/,% <--- I can only use the TeX Live ones
  Extension = .otf,% <--- I have OpenType fonts
  Ligatures = TeX,
  UprightFont = *-Regular,
  BoldFont = *-Bold,
  ItalicFont = *-Italic,
  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},
]{Montserrat}

\DeclareOption{default}{%
  \renewcommand{\familydefault}{Montserrat}%
}
\ProcessOptions\relax

Now a couple of test files.

\documentclass{article}
\usepackage[default]{montserrat}

\begin{document}

This is in Montserrat.

{\fontseries{mb}\selectfont Montserrat}

{\fontseries{k}\selectfont Montserrat}

\end{document}

enter image description here

\documentclass{article}
\usepackage{montserrat}

\begin{document}

This is standard.

{\Montserrat
This is in Montserrat.

{\fontseries{mb}\selectfont Montserrat}

{\fontseries{k}\selectfont Montserrat}
}

\end{document}

enter image description here

You may want to add also

\DeclareOption{sfdefault}{%
  \renewcommand{\sfdefault}{Montserrat}%
}

so that \usepackage[sfdefault]{montserrat} will define Montserrat as the default sans serif font (like for \setsansfont).

With beamer you do \usepackage{montserrat} and the declaration you show in your post.

1
  • Thanks! I didn't think of renewing familydefault, that's exactly what I was after! Oct 17, 2022 at 21:09
1

If the MontSerrat font has been downloaded and activated suitably on your system, and if you need just the four basic or default font weight/shape combinations -- upright, italic, bold, and bold-italic -- I can see no reason for not running

\usepackage{fontspec}
\setmainfont{MontSerrat}

in the preamble.

Of course, if you want to use some of the non-default weight/shape combinations, you could replace \setmainfont{MontSerrat} with, say,

\setmainfont{MontSerrat}%
  [Path=/usr/local/texlive/2022/texmf-dist/fonts/opentype/public/montserrat/,
   Extension=.otf,
   UprightFont=Montserrat-ExtraLight,
   ItalicFont=Montserrat-ExtraLightItalic,
   BoldFont=Montserrat-Black,
   BoldItalicFont=Montserrat-BlackItalic]
2
  • I'm using quite a lot of custom fonts that aren't available in a lot of systems, but do have stored in a repo next to my documents. I also use other weights and shapes, e.g. Black. I could replace \setmainfont{Montserrat} with all the settings indeed, but that's exactly what I'd like to share instead - rather than defining those other weights and shapes over and over again in multiple documents. Oct 17, 2022 at 3:04
  • @MarcPlano-Lesay - Is there something stopping you from creating a sty file that runs \setmainfont{Montserrat}[<lots and lots of options>] instead of \newfontfamily{\Montserrat}[<lots and lots of options>]? The question you posed in the title of your posting is -- unless I'm badly mistaken... -- if one can designate the MontSerrat font to be the main document font, via a suitable \setmainfont directive. The answer is an emphatic yes.
    – Mico
    Oct 17, 2022 at 3:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .