2

how do I correctly set a main and mono font for my document when the font is imported with \usepackage ? I am using lualatex.

I tried setting it like this:

\usepackage{stix2}
\usepackage{sourcecodepro}
\usepackage{fontspec}
\setmainfont{stix2}
\setmonofont{sourcecodepro}

but this gives me an error Package fontspec Error: The font "stix2" cannot be found. \setmonofont

How do I do this properly?

1
  • 2
    stix2 already sets the default font. So you don't need \setmainfont. Package sourcecodepro also already sets option ttdefault by default, so setting the mono font is already the default. However, if you want, you can load the package explicitly with option ttdefault. To switch between the fonts inside the document, you can use \normalfont and \ttfamily and the other font commands defined by the packages, e.g., \sourcecodepro or \sourcecodeprolight. See the manuals of both packages for more information.
    – cabohah
    Jan 18, 2023 at 9:45

1 Answer 1

3

You should run

\usepackage{stix2}
\usepackage{sourcecodepro}

if you're ok with the packages' default settings, or

\usepackage{fontspec}
\setmainfont{Stix Two Text}
\setmonofont{Source Code Pro}

if you plan on customizing some of the font settings using the machinery of the fontspec package. E.g., you might run

\setmonofont{Source Code Pro}[Scale=MatchLowercase]

to equate the x-heights of the fonts.


enter image description here

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Stix Two Text}
\setmonofont{Source Code Pro}[Scale=MatchLowercase]
\begin{document}
Hello World.

\ttfamily Hello World.
\end{document}
5
  • 1
    Package sourcecodepro has an options scale and scaled, so you don't need to use \setmonofont for scaling.
    – cabohah
    Jan 18, 2023 at 11:32
  • 1
    @cabohah - I think you missed the point. The point of taking the \setmonofont route is that one has direct access to the full machinery of the fontspec package. E.g., \setmonofont{Source Code Pro}[BoldFont=Source Code Pro Semibold, BoldItalicFont=Source Code Pro Semibold Italic] to choose a non-default weight for the bold and bold-italic typefaces. No need to examine the user guide of the sourcecodepro package to find out if that package provides options for setting non-default bold font weights.
    – Mico
    Jan 18, 2023 at 11:45
  • 1
    Reading the package manual is always a good idea and if your point is to set features or font combinations the package does not provide, you should IMHO show an example for doing so and not an example, that reduces the comfort like automatically selecting the suitable UprightFont, ItalicFont, BoldFont and BoldItalicFont and providing other features like lining numbers or old style numbers etc.
    – cabohah
    Jan 18, 2023 at 12:38
  • @cabohah - Where, or when, did I claim that reading a manual is a bad idea? Please don't put words in my mouth. You seem to be hell-bent on disproving claims that I haven't made. What's your point?
    – Mico
    Jan 18, 2023 at 13:23
  • I think there is a misunderstanding here. I did not imply that you fundamentally reject reading manuals. I only pointed out in my first comment that the sanscodepro package provides an option, so the reason you demonstrated for not using the package is not valid. Furthermore, in my last comment I pointed out that the package does more than a simple \setkomafont{Sans Code Pro}. If that is a reason for you to get upset, I will try to avoid criticizing your answers in the future. Sorry!
    – cabohah
    Jan 18, 2023 at 14:47

You must log in to answer this question.

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