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

  • 1
    Welcome to TeX.SE. – Mico Jul 08 '23 at 17:19
  • Are you sure the document in the link you provide advocates writing \mathrm{\greeksymbol}? – Mico Jul 08 '23 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 08 '23 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 08 '23 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 08 '23 at 17:33

2 Answers2

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.

Davislor
  • 44,045
  • 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 09 '23 at 09:32
  • This is with unicode-math. You need a different method to use this in PDFTeX – Davislor Jul 09 '23 at 16:25
  • @tetukowski Added a whole other answer for PDFTeX. – Davislor Jul 09 '23 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 09 '23 at 20:09
  • your last two comments are pointing to the 'simple solution' with \usepackage[lcgreekalpha]{stix2}, isn't it? Thanks, i should try! – tetukowski Jul 09 '23 at 22:35
  • I still have questions about these matters for @Davislor and @Miko. But I have tight deadline this week. I want to confirm that both solutions by @Davislor (\usepackage[lcgreekalpha) and @mika (use unicode-math) are both OK. In my case, regarding the usage in of Latex in Matplotlib, using PDFlatex is more straightforward. and Lualatex returns error when the plot is complicated. Thank you to both @Miko & @Davislor. I will ask more, but later, i hope that's OK. – tetukowski Jul 10 '23 at 09:41
  • @tetukowski Not sure what the Matplotlib problem is, but I would normally suggest using LuaTeX when you can and PDFTeX when you have to. – Davislor Jul 10 '23 at 15:05
  • Why Lualatex? @Davislor, could you pinpoint me the manual that you mentioned before. I would like to learn and see other fonts that I can use. – tetukowski Jul 10 '23 at 18:17
  • 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 '23 at 18:23
  • in your 3rd answer of using Modern Fonts, "Use the upgreek-style commands, which unicode-math supports"? Could you help to to give example how to set this in the preamble? (I tried something, but it didn't work). Is that means that I don't need to use \symup and it is applicable for any other unicode-math fonts? – tetukowski Oct 23 '23 at 10:42
  • I meant that $\upGamma$, $\upbeta$, and so forth work out of the box in unicode-math, with no extra setup needed. – Davislor Oct 23 '23 at 20:03
  • Thank you so much. Yes, it works. I thought that it only can be done with '\symup', if i understand correctly, so symup is equivalent (somewhat) to mathrm of pdftex. Very valuable lesson from your answer above, but as I will use Tex now (before I was only using it for matplotlib), i probably will have more question in the future. – tetukowski Oct 24 '23 at 11:30
  • @tetukowski In legacy 8-bit TeX, \mathrm is used in two distinct ways: upright math symbols in equations (which are \symup in unicode-math), but also words in math mode, such as if, otherwise, PROFIT, and so on (\mathrm or \textnormal in unicode-math). Even in 8-bit mode, this can cause a problem if you want to use both upright accented letters and upright Greek letters, since no 8-bit font encoding has both. – Davislor Oct 24 '23 at 11:34
  • @tetukowski If you are consistently using \mathrm for upright math symbols, you can, as I mentioned, call \usepackage[mathrm=sym]{unicode-math} for compatibility. In that case, any comments in math mode should switch back to text mode, such as \textup{ if and only if }. – Davislor Oct 24 '23 at 11:40
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}

Mico
  • 506,678
  • 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\mathrrmtosymupwill cost the same as changing the\omegato\upomega.` How difficult is the alternative method? – tetukowski Jul 08 '23 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 08 '23 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 08 '23 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 08 '23 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 08 '23 at 23:30
  • @tetukowski - Under pdfLaTeX, your notes are a good summary in "TeX math style", which is also the only math style provided by pdf(La)TeX. The unicode-math package, which cannot run under pdfLaTeX, provides three addtional "math styles" -- ISO, french, and upright. – Mico Jul 09 '23 at 05:11
  • I just realized, that the French style use uppercase Latin as upright, but I need it to be italic, except if it is a constant (the same case as in lowercase Latin). \mathit{} does the work. by the way \symup{} is not working but \mathrm is ok. – tetukowski Jul 09 '23 at 09:28
  • @tetukowski - In other words, the "french" math style may not be right for you. Oh well. – Mico Jul 09 '23 at 09:31
  • @tetukowski - If you don't load the unicode-math package,\symup is not defined. – Mico Jul 09 '23 at 09:33
  • Is Tex the default value if you don't set math-style? – tetukowski Jul 10 '23 at 20:34
  • @tetukowski - Not sure what you mean by "Tex". If it's pdfTeX, then there's exactly one, and only one, math style which, by implication, is also the default math style. If it's LuaTeX, then the unicode-math package provides a choice of four predefined math styles. (See my answer above for more information about them.) The default math style is, maybe unsurprisingly, "TeX". – Mico Jul 10 '23 at 20:39
  • I'm sorry I wasn't clear enough. Yes, I meant the default math style using unicode-Math. Thank you. – tetukowski Jul 10 '23 at 20:57