1

I would like to use a serif font for both the body of my text and maths environments as well as a separate monospaced font for things like URLs.

When I compile this using fontspec:

\documentclass[12pt,a4paper]{article}
\usepackage[hidelinks]{hyperref}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setmonofont{Courier New}
\begin{document}
\noindent See the following article on Wikipedia about the year AD 2017:

\url{https://en.wikipedia.org/wiki/2017}

$2000 + 17 = 2017$
\end{document}

I get:

Which does not use the right maths font. Unsurprisingly, I get the same thing by just replacing fontspec with mathspec). But when I add \setmathsfont{Times New Roman} using mathspec, I still get the same result.

And when I compile:

\documentclass[12pt,a4paper]{article}
\usepackage[hidelinks]{hyperref}
\usepackage{mathspec}
\setallmainfonts{Times New Roman}
\setallmonofonts{Courier New}
\begin{document}
\noindent See the following article on Wikipedia about the year AD 2017:

\url{https://en.wikipedia.org/wiki/2017}

$2000 + 17 = 2017$
\end{document}

I get:

Which changes the maths environment to the desired font but changes the numbers in the given URL to serif rather than monospaced, which is undesirable.

Does anyone have idea where I'm going wrong and how I can get something that looks like this:

  • 1
    Have you tried unicode-math and its \setmathfont (in combination with fontspec)? – TeXnician Apr 17 '17 at 14:05
  • I hadn't but that actually works quite well! – Miztli Apr 17 '17 at 16:26
4

url uses math to typeset the url and as mathspec changes mathcodes you get the wrong font. You could reset them:

\documentclass[12pt,a4paper]{article}
\usepackage[hidelinks]{hyperref}
\usepackage{mathspec}
\setallmainfonts{Times New Roman}
\setallmonofonts{Courier New}

\makeatletter
\def\Url@FormatString{%
 \UrlFont
\Url@MathSetup
\mathcode"30=28720 %0
\mathcode"31=28721 %1
\mathcode"32=28722 %2
%...
\mathcode"37=28727 %7
 $\fam\z@ \textfont\z@\font
 \expandafter\UrlLeft\Url@String\UrlRight
 \m@th$%
}%
\begin{document}
\noindent See the following article on Wikipedia about the year AD 2017:

\url{https://en.wikipedia.org/wiki/2017}

$2000 + 17 = 2017$
\end{document}

enter image description here

But I would use unicode-math and a times like math font, e.g. like this

\documentclass[12pt,a4paper]{article}
\usepackage[hidelinks]{hyperref}
\usepackage{unicode-math}
\setmainfont{Times New Roman}
\setmonofont{Courier New}
\setmathfont{TeX Gyre Termes Math}
\begin{document}
\noindent See the following article on Wikipedia about the year AD 2017:

\url{https://en.wikipedia.org/wiki/2017}

$2000 + 17 = 2017$
\end{document}

enter image description here

  • What are the reasons for using TeX Gyre Termes Math rather than Times New Roman itself? – Miztli Apr 17 '17 at 16:14
  • For math you need a math font (they have additional values needed for math typesetting, see e.g. tex.stackexchange.com/q/19570/2388) – Ulrike Fischer Apr 17 '17 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.