5
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{fontspec}
\setmainfont{Source Sans Pro}[
    UprightFont     =Source Sans Pro-Light,
    BoldFont        =Source Sans Pro-Semibold,
    ItalicFont      =Source Sans Pro-Light Italic,
    BoldItalicFont  =Source Sans Pro-Semibold Italic]
\begin{document}
Maths Formula: $F=ma$

I cannot get upright serif when I use "mathrm" command: $\mathrm{F=ma}$
\end{document}

I have changed my fonts by using fontspec package. When I type in math mode, I want the font to be upright serif. However, the font in math mode is serif but becomes the font I set when I add \mathrm. How can I get an upright serif font in math mode?

  • 1
    You have to load a matching math font, but remember: There is a reason for those letters to be italic. There is also a reason when a letter is upright, or bold, or blackletter, or script ... There is a meaning behind it. – Johannes_B Mar 25 '18 at 9:55
  • Aside: If you load the fontspec package, you should almost certainly not be using the inputenc package as well. – Mico Mar 25 '18 at 10:06
  • 1
    unrelated but do not use \usepackage[utf8]{inputenc} (it does nothing other than issue a warning saying it should not be used with xetex/luatex) – David Carlisle Mar 25 '18 at 10:13
6

(Remark: I updated this answer significantly after the OP clarified his/her objectives.)

I suggest you (a) load the unicode-math package and (b) specify the option math-style=upright when running \setmathfont. You didn't specify which serif math font family you wish to use; I'm assuming it's Latin Modern Math.

Aside: Since you're loading the fontspec package, you shouldn't be loading the inputenc package with the option utf8.

enter image description here

Do not use \mathrm, as that'll give you letters that use the text font -- which you've defined to be a sans-serif font.

\documentclass[a4paper]{article}
%\usepackage[utf8]{inputenc} % don't load this package!
\usepackage{amsmath,amssymb}
\usepackage{unicode-math} % <-- new
\setmainfont{Source Sans Pro}[
    UprightFont    = *-Light,
    ItalicFont     = *-Light Italic,
    BoldFont       = *-Semibold,
    BoldItalicFont = *-Semibold Italic]

\setmathfont{Latin Modern Math}[math-style=upright] % <-- new
\begin{document}
Maths Formula: $F=ma$
\end{document}
  • No, I want the font to be serif upright, not to be the text font. The screenshot is used to say that I cannot get serif upright when I use \mathrm. – Siwei Mar 25 '18 at 10:11
  • I am sorry about misleading you. Sorry. – Siwei Mar 25 '18 at 10:14
  • 1
    I changed from XeLaTex to LuaLaTex. It works. Thank you. – Siwei Mar 25 '18 at 10:42
  • 1
    @Superfrankie are you on Mac OS X? then there is a trick to get XeTeX find the MacTeX/TeXLive provided fonts. Out of the box you need to use filenames as pointed out by Mico's comment. – user4686 Mar 25 '18 at 11:27
  • 1
    @Mico An “extension,” perhaps? In any case, it’s a package one can load instead. Authors can check out the documentation and see if they want to consider using it. – Davislor Mar 25 '18 at 22:18
3

The end result makes me weep.

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{fontspec}
\usepackage{mathastext}

\setmainfont{Source Sans Pro}[
  UprightFont    = *-Light,
  BoldFont       = *-Semibold,
  ItalicFont     = *-Light Italic,
  BoldItalicFont = *-Semibold Italic
]

\begin{document}

Maths Formula: $\mathbf{F}=m\mathbf{a}$
\[
\sum_{k=0}^n k^2 = n\Bigl(n+\frac{1}{2}\Bigr)(n+1)
\quad\text{for every $n$}
\]

\end{document}

enter image description here

  • This code generates the following warning message: "Package mathastext Warning: Unknown option 'upright'." I think the key pointer for the OP (and future readers of this posting) should be that one must load the mathastext message before running \setmainfont in order to get uprignt/serif math glyphs. – Mico Mar 25 '18 at 20:54
  • is your weeping due to the fact the math formula is mssing a /3 ? ;-) – user4686 Mar 26 '18 at 8:55
  • @jfbu No, wrong fonts call for wrong math. ;-) – egreg Mar 26 '18 at 8:57
  • Good point, but as there is a doubt whether all of that was intentional, I propose a substantial transfer of reps to my account :). This will perturbate the 654321 speculations and ruin all those who bet money on this, but that's allright to me. – user4686 Mar 26 '18 at 9:11
1

This was originally a comment to @egreg's answer. But now I have added a few extras. I am using Source Serif Pro which apparently has no italic variant.

In particular, fontspec should be loaded with no-math option when using mathastext.

Besides, I did not quite understand if the OP wants all letters in math to be upright, or only for \mathrm etc.. to produce serif letters.

I sort of expect OP's wanted letters italic, except when using \mathrm but well here we go. Anyway Source Serif Pro in TeXLive appears to have no italic variant, so this anyhow clinches the discussion here about whether letters should be italic or upright in math:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[no-math]{fontspec}

\setmainfont{Source Serif Pro}[
  UprightFont    = *-Light,
  BoldFont       = *-Semibold,
]

\usepackage[defaultmathsizes, basic]{mathastext}

\setmainfont{Source Sans Pro}[
  UprightFont    = *-Light,
  BoldFont       = *-Semibold,
  ItalicFont     = *-Light Italic,
  BoldItalicFont = *-Semibold Italic
]

\begin{document}

Physics Formula: $\mathbf{F}=m\mathbf{a}$.

Wrong Maths Formula:
\[
\sum_{k=0}^n k^2 = n\Bigl(n+\frac{1}{2}\Bigr)(n+1)
\quad\text{for every $n$}
\]

\end{document}

enter image description here

(the above is wrong math formula, see next).


By the way, I enjoy aesthetically serif in text and sans in math. This can be achieved here this way:

\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[no-math]{fontspec}

\setmainfont{Source Serif Pro}[
  UprightFont    = *-Light,
  BoldFont       = *-Semibold,
]

\setsansfont{Source Sans Pro}[
  UprightFont    = *-Light,
  BoldFont       = *-Semibold,
  ItalicFont     = *-Light Italic,
  BoldItalicFont = *-Semibold Italic
]

\renewcommand\familydefault{\sfdefault}
\usepackage[defaultmathsizes, basic]{mathastext}
\renewcommand\familydefault{\rmdefault}


\begin{document}

Maths Formula: $\mathbf{F}=m\mathbf{a}$
\[
\sum_{k=0}^n k^2 = \frac{n\Bigl(n+\frac{1}{2}\Bigr)(n+1)}3
\quad\text{for every $n$}
\]

\end{document}
% Local Variables:
% TeX-engine: xetex
% End:

enter image description here

  • @Mico I find as a rule that the exact opposite setting: serif letters in text, sans letters in math, gives pleasing aesthetic results. Here I am to my dismay almost thinking like egreg that this is a bit painful on the eyes... – user4686 Mar 25 '18 at 22:07
  • +1 for actively imagining that by the locution "upright serif", the OP might have been thinking of employing serif font (such as Source Serif Pro) which harmonizes with Source Sans Pro. In my answer, I had assumed (quite possibly invalidly...) that the term "upright serif" meant the system default, viz., Latin Modern (Roman)... – Mico Mar 25 '18 at 22:19
  • @Mico, I have read again OP's and can't conclude about what is asked really. Thus I decided to educate the public and use serif in text and sans in math mode ... – user4686 Mar 26 '18 at 8:52

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.