5

I'm using Stix two math font in Latex in Matplotlib for my plot. At the moment i'm restricted to Stix two math, to match the equation-font in ms-Word.

The test that i show here is using tex engine in overleaf.com. later i apply the same preamble in matplotlib. (and so it is not matplotlib problem).

I need to use upright (regular) greek for mathematical equations. Reading this answer, i thought simply using \mathrm{\greeksymbol} should be ok. but it didn't work. It shows the same italic greek, with or without \mathrm (see row 2 & 3 on test image). i try to use package \usepackage{upgreek}, but it seems use different font (see the last row), i compared it with the standard stix two math regular (the first row).

here is my code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{stix2}
\usepackage{upgreek}

\begin{document}

$\tau,\omega,\pi$

$\mathrm{\tau},\mathrm{\omega},\mathrm{\pi}$

$\uptau,\upomega,\uppi$

\end{document}

Did i make a mistake? why the mathrm is not working? I'm sorry that i am very beginner in latex.

Here is the result: enter image description here

Thanks a lot!

Tex engine: overleaf.com

5
  • 1
    Welcome to TeX.SE.
    – Mico
    Jul 8, 2023 at 17:19
  • Are you sure the document in the link you provide advocates writing \mathrm{\greeksymbol}?
    – Mico
    Jul 8, 2023 at 17:21
  • 1
    Hi Mico, thanks.. it says ' The stix and stix2 packages support Greek letters in \mathrm', so I thought it is like that. Could you elaborate more or point me to any source to learn?
    – tetukowski
    Jul 8, 2023 at 17:25
  • If you need to use upright Greek letters, do you also need to use upright (lowercase) Latin letters? Please advise.
    – Mico
    Jul 8, 2023 at 17:26
  • 1
    So in my case: any variable in latin --> italic , any variable but in greek letter --> upright greek. It doesn't matter uppercase or lowercase. For something that isn't variable, it must be in upright. Everything inside $$ because it is in math mode, usually when i need upright, i just to the \mathrm{\non variable symbol}
    – tetukowski
    Jul 8, 2023 at 17:33

2 Answers 2

6

Update: The Simple Solution

Although stix2 has lowercase Greek letters in \mathrm, LaTeX’s default definitions of \alpha, \beta and so on ignore the current math alphabet. There is a package option to override this, [lcgreekalpha].

\documentclass{article}
\usepackage{amsmath}
\usepackage[lcgreekalpha]{stix2}

\begin{document}
\begin{equation*}
\begin{split}
       &\sin^2 \mathrm{\theta} \cos^2 \mathrm{\phi}\\
     + &\cos^2 \mathrm{\theta} \cos^2 \mathrm{\phi}
\end{split}
\end{equation*}
\end{document}

stix2-mathrm font sample

Note: Although stix2 does not provide upgreek-style commands, such as \upalpha, you might want to add them for compatibility with other packages. This will make it much simpler to change the font of your papers. You should remove upgreek, which isn’t compatible with stix2.

The following will set \upalpha to \mathrm{\alpha} if it has not been defined, but not change the existing definition if there is one, which should be safe to use with any document class:

\providecommand\upalpha{\mathrm{\alpha}}

With Modern Fonts

You can get upright Greek letters with unicode-math, in LuaLaTeX or XeLaTeX. Every Unicode math font has them. However, \mathrm is not the command to get them (at least, not out of the box).

There are at least four ways:

  • Change \mathrm{\omega} to \symup{\omega}
  • Load unicode-math with the [mathrm=sym] package option, to make \mathrm an alias for \symup. If you ever need words in math mode, use \textnormal or \textup instead.
  • Use the upgreek-style commands, which unicode-math supports.
  • Instead of solving the real problem, change your \mathrm font to one that has Greek letters. You’re only supposed to do that if you actually will be writing Greek words in math mode, like \mathbf{εὕρηκα!}. And in that case, you probably want to write Greek in text mode, too, so you likely already set it to something that will work. However, you can set this to a different font family with, e.g. \setmathrm{CMU Serif}.

Finally, you probably want to set

\tracinglostchars=3

in your preamble, to make TeX consider it an error when you request a symbol from a font that doesn’t have it. The default behavior, thanks to some technical debt from the 1980s, is to silently print a warning to the middle of a .log file.

\documentclass{article}
\tracinglostchars=3
\usepackage[stixtwo]{fontsetup}

\begin{document}
\begin{equation*}
\begin{split}
       &\sin^2 \symup{\theta} \cos^2 \symup{\phi} \\
     + &\cos^2 \uptheta \cos^2 \upphi
\end{split}
\end{equation*}
\end{document}

STIX Two Math sample

With Legacy Fonts

The only standard TeX font encoding that has all of the Latin and Greek letters is OML (which you can think of as “old math letters.”) However, very few font families have upright letters in this encoding. One package that does is mathdesign, which provides upright Charter (the mdbch family), Garamond (mdugm) or Utopia (mdput).

So, here is an extremely contrived example of how to select an upright font (Math Design Utopia) with Greek letters. It loads this as both the \mathrm alphabet and as the font for operators like \sin and \cos, redefines \theta and \phi so that they work properly with \mathrm, and also adds commands \uptheta and \upphi. It sets all of these up to work with \mathversion{bold}, so they will work with \boldsymbol or bm.

\documentclass{article}
\tracinglostchars=3
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\DeclareSymbolFont{upletters}{OML}{mdput}{m}{n}
\SetSymbolFont{upletters}{bold}{OML}{mdput}{b}{n}
\DeclareSymbolFontAlphabet\mathrm{upletters}

\DeclareMathSymbol{\theta}{\mathalpha}{letters}{"12}
%...
\DeclareMathSymbol{\phi}{\mathalpha}{letters}{"1E}

\DeclareRobustCommand\uptheta{\mathrm{\theta}}
\DeclareRobustCommand\upphi{\mathrm{\phi}}

\makeatletter
\def\operator@font{\mathgroup\symupletters}
\makeatother

\begin{document}
\begin{equation*}
\begin{split}
       &\sin^2 \mathrm{\theta} \cos^2 \mathrm{\phi}\\
     + &\cos^2 \uptheta \cos^2 \upphi
\end{split}
\end{equation*}
\end{document}

Math Design Utopia sample

I say “extremely contrived” because, in practical real-world use, you would use a package for this, such as mathdesign or fourier in this case, or upgreek for something more compatible with Computer Modern. If you just want to load math alphabets that support Greek, isomath is a flexible package for that. However, if no package does exactly what you want, or you are writing your own font package, you can use commands like those to set it up.

Variations of these commands will also work with the fonts from any package that extends OML to its own custom 8-bit encoding, but keeps all the letters in the same places as OML. These include eulervm, newtxmath, newpxmath and stix2-type1. You will need to check the package documentation to see if it has font tables, and then probably reverse-engineer the package itself to see how it declares the encoding and family name. This is how it’s possible to use Euler for medium-weight letters but Math Design Charter for bold.

There is another alternative: mathastext has an option to load Greek letters from a font in the 8-bit LGR encoding, such as the ones from the Greek Font Society.

14
  • The first way: Change \mathrm{\omega} to \symup{\omega}. Which package are we using for this method? Thanks before to your answer. Some i couldn't understand but i will try it.
    – tetukowski
    Jul 9, 2023 at 9:32
  • This is with unicode-math. You need a different method to use this in PDFTeX
    – Davislor
    Jul 9, 2023 at 16:25
  • @tetukowski Added a whole other answer for PDFTeX.
    – Davislor
    Jul 9, 2023 at 19:20
  • @tetukowski Actually, looking it up in the manual, the package you were using has an option to fix this. With that, you can remove upgreek (which isn’t compatible anyway).
    – Davislor
    Jul 9, 2023 at 20:09
  • 2
    @tetukowski LuaTeX is the only TeX engine still in active development and adding features. The others are in maintenance mode. I’m not sure which manual you’re asking about, but here is a list of OpenType math fonts, and the best available list of seven-bit math fonts is in the isomath manual.
    – Davislor
    Jul 10, 2023 at 18:23
5

Let's posit up front that it's no fun to have to replace all instances of \alpha, \beta, etc with \upalpha, \upbeta, etc. A method for rendering lowercase greek letters in an upright font shape automatically would be quite attractive, right?

The unicode-math package lets users choose among four pre-defined math styles:

enter image description here

Since you need to display all Greek (both lowercase and uppercase) letters in an upright font shape, I take it that you should load the Stix Two Math font package with either math-style=french -- if lowercase Latin Letters are supposed to be shown in the slanted ("italic") font shape -- or math-style=upright -- if lowercase Latin letters are supposed to be shown in the upright font shape.

Since you've written in a comment that "any variable in latin [letters] --> italic", I suppose you want the "french" math style. To render constants in an upright font shape, I suggest you write \symup{i}, \symup{e} as needed.

I suggest you use LuaLaTeX to compile your document and replace \usepackage{stix2} with the following two instructions:

\setmainfont{Stix Two Text}
\setmathfont{Stix Two Math}[math-style=french]

The code for the table shown above is as follows.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Stix Two Text} % text font

%handy shortcut macro:
\newcommand{\blurb}{$a,b,c \quad A,B,C \quad \gamma,\delta,\omega \quad \Gamma,\Delta,\Omega$}

\begin{document}

\begin{tabular}{ll}
Math Style & Appearance \\[1ex]
TeX     & \setmathfont{Stix Two Math}[math-style=TeX]\blurb \\
ISO     & \setmathfont{Stix Two Math}[math-style=ISO]\blurb \\
french  & \setmathfont{Stix Two Math}[math-style=french]\blurb \\
upright & \setmathfont{Stix Two Math}[math-style=upright]\blurb \\
\end{tabular}

\end{document}
12
  • Wow.. that's a lot. Thank you! At first, I was afraid I couldn't pass that complicated content, but that is just for you to make the table for comparison. If I choose to use French, this line \setmathfont{Stix Two Math}[math-style=french], should below the \usepackage{}? The french style seems reasonable to me. I have to try it, but still a lot of work, I'm on Windows and only have Miktex. Another question: Since all my plots (code) are ready, changing \mathrrm` to symup will cost the same as changing the \omega to \upomega. How difficult is the alternative method?
    – tetukowski
    Jul 8, 2023 at 21:04
  • @tetukowski - Yes, the \setmainfont and \setmathfont directives should come after \usepackage{unicode-math}. If you already have the necessary \mathrm directives in place, you needn't change them to \symup.
    – Mico
    Jul 8, 2023 at 21:54
  • Hi Mico, although it is working wiht the current installation of miktex using textWorks. It is not working, in Matplotlib, as I have to use pgf backend to use lualatex. I know this is matplotlib limitation, but do you have any other idea/solution without Lualatex? Thank you.
    – tetukowski
    Jul 8, 2023 at 22:42
  • @tetukowski - I'm afraid I'm not familiar with matplotlib. In your posting, you mentioned that you use LaTeX, and LuaLaTeX is LaTeX.
    – Mico
    Jul 8, 2023 at 23:01
  • yes in Matplotlib we can use Latex for rendering. Only to set either to use pdflatex, xelatex, lualatex is quite complicated. The default is to use pdflatex. In my case, i still have problem when try to use lualatex (but i still try it). So.. back to the topic, for latin character in math-mode, the default way to use upright letter is by using \mathrm. For greek letter, there is no \mathrm or default method to change to upright font, it may depends on the font that we use, do I understand this correctly or not? Thank you.
    – tetukowski
    Jul 8, 2023 at 23:30

You must log in to answer this question.

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