[Tex/LaTex] How to set default monospace font for \texttt{} in LuaLaTeX

fontstypewriter

I am able to set default text font using \newfontfamily\mainfont{}.

Here is an example.

\documentclass{article}
\usepackage{fontspec}
\newfontfamily\mainfont{DejaVu Sans}
\begin{document}
Foo
\texttt{Bar}
\end{document}

How can I similarly set the default monospace font that is used for text rendered with \texttt{}?

Best Answer

Read the main part of fontspec documentation. At page 7, you find

enter image description here

So you should do

\documentclass{article}
\usepackage{fontspec}
\setmainfont{DejaVu Sans}
\setmonofont{Libertinus Mono}[
  Scale=MatchLowercase
] % or whatever font you prefer

\begin{document}

Foo
\texttt{Bar}

\end{document}

enter image description here