1

I am trying to use a custom font (.otf files) on a template document that I am building. I looked for different ways of doing so and am currently using the XeLatex compiler to create a new font family and I am trying to apply it to my entire document (that's my goal, not to just use it for a certain part, but the whole thing).

The code and output that I am getting are:

...

\usepackage{fontspec}
\newfontfamily\coolfont[Path=config/CoolFont/,
  Extension=.otf,
  UprightFont=*-Regular,
  ItalicFont=*-RegularItalic,
  BoldFont=*-Bold,
  BoldItalicFont=*-BoldItalic
]{CoolFont}
...

\begin{document}
\coolfont
    
...
\end{document}

Output

My problem is that the section header and the figure caption are not in the font I selected (the body text looks good, correctly applying bold and italics as well). This also happens for page numbers, table captions, etc. I have also tried calling the \coolfont in the preamble, which just gave me an error and I tried using \setmainfont, but that didn't even change the body text font.

What is the best way to use custom font files to set a font for the whole document? (I am using Overleaf for this project)

Thank you so much for the help!

EDIT:

The following is my failed attempt at making it work with \setmainfont, while trying to leave most of the configuration I was already using in my document.

%  -----------------------------  PREAMBLE  -----------------------------
\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{epsfig, url}
\usepackage{epstopdf}
\usepackage{graphicx}
\usepackage{datetime}
\usepackage{multirow}
\usepackage{indentfirst}
\usepackage{amssymb}
\usepackage{geometry}
\geometry{a4paper}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{newtxtext}
\usepackage{float}
\usepackage{titling}
\usepackage{xcolor}

\usepackage{fontspec}
\usepackage{titlesec}

\setmainfont[Path=config/CoolFont/,
  Extension=.otf,
  UprightFont=*-Regular,
  ItalicFont=*-RegularItalic,
  BoldFont=*-Bold,
  BoldItalicFont=*-BoldItalic
]{CoolFont}

%  ------------------------- DOCUMENT START -------------------------
\begin{document}
\section{The quick brown fox jumps over the lazy dog}
\label{intro}

The quick brown fox jumps over the lazy dog. 0123456789
\textbf{The quick brown fox jumps over the lazy dog. 0123456789}
\textit{The quick brown fox jumps over the lazy dog. 0123456789}
\textit{\textbf{The quick brown fox jumps over the lazy dog. 0123456789}}

\begin{figure}[hbt!]
    \centering
    \includegraphics[scale=0.1, angle=0]{imagens/image.png}
    \caption{The quick brown fox jumps over the lazy dog}
    \label{fig:image}
\end{figure}

\end{document}
6
  • 2
    Since you don't change the main font family but only create a new family, everything which explicitly selects a font family will overwrite it. There is a solution for this: \setmainfont and friends. So you should focus on why \setmainfont didn't even change the text body font. To help with that, we will need a minimal, but complete (including a document class etc.) example demonstrating the issue. Aug 2, 2021 at 20:39
  • @MarcelKrüger on it! Thank you for the guidance.
    – Victor
    Aug 3, 2021 at 11:52
  • 1
    This example isn't minimal, it includes all kinds of packages which aren't related to the problem. See e.g. tex.meta.stackexchange.com/questions/228/… to learn how to create minimal examples. Most likely you will find the problem on you own if you try that. Aug 3, 2021 at 19:02
  • Beside that, you should never use inputenc in XeLaTeX (you should have gotten a warning that it was ignored). Aug 3, 2021 at 19:04
  • @MarcelKrüger that post on minimal working example is so useful that it should be recommended for all users to read! You are right, by trying that I found the error on my own, which was the newtxtext package. I will have to change a few things to account for that, but the problem is solved! Thank you again!
    – Victor
    Aug 4, 2021 at 13:36

1 Answer 1

1

With guidance from a user in the comments I ended up solving the issue myself.

The problem lies in using the newtxtext package while at the same time attempting to use the command \setmainfont and loading a custom font with the fontspec package. I also tested if the problem persisted if I used \setmainfont with a non-custom font and it does. I can't be sure as to how newtxtext overwrites that setting, but the font is reverted back to default.

In short, from @MarcelKrüger:

If you don't change the main font family and only create a new family, everything which explicitly selects a font family will overwrite it.

which is why initially the font wasn't applied to the headers and captions. And secondly, \setmainfont will not work when importing the newtxtext package.

You must log in to answer this question.

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