How to declare a mathematical operator whose glyph is the Greek letter sigma, when the unicode-math package is loaded?

A short description of the problem

I'd like to create a mathematical operator whose glyph is the Greek lowercase letter sigma, while the unicode-math package is loaded.

A demonstration of the problem by way of a minimal working example

I saved the following LaTeX code in the file ~/Test.tex.

\documentclass{article}
\usepackage{amsmath}
%\usepackage{unicode-math}
%\setmathfont{STIX Two Math}
\DeclareMathOperator{\op}{\sigma}
\begin{document}
\begin{align*}
&\op x\\
&\sigma x
\end{align*}
\end{document}


The code creates a mathematical operator called \op, whose glyph is the Greek lowercase letter sigma. The body of the document uses both the operator as well as the bare letter sigma.

I then executed the following commands in the Terminal.

> cd ~
> lualatex Test


This resulted in the creation of the file ~/Test.pdf. When opened in a PDF viewer, this file displayed as follows. (I screenshot only the relevant part of the display).

As can be seen, the operator and the bare sigma behave differently: the space between the operator and the following letter is greater than the space between the bare sigma and the following letter. This is the rationale behind my desire to define sigma as a mathematical operator, rather than simply use the bare letter.

If now the two commented lines are uncommented, so as to load the unicode-math package and set the math font to STIX Two, and the code is recompiled, the resulting PDF file displays as follows.

As can be seen, nothing was printed out where the operator should be.

Questions

1. Why didn't the operator print out when the unicode-math package was loaded?
2. How can I define a mathematical operator whose glyph is the Greek lowercase letter sigma, when the unicode-math package is loaded, and STIX Two is set as the math font?

Remark

(The following remark is posted a day after the original question was posted, and after several answers have been proposed.)

A couple users mentioned in their answers below that \DeclareMathOperator looks for the glyph for the character code it is passed as its second argument in the document's text font, and that therefore setting the document's text font to, say, STIX Two Text, using the command \setmainfont{STIX Two Text}, should resolve the problem I described in my original question.

However, the following code undermines these claims.

\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}
\DeclareMathOperator{\op}{\sigma}
\begin{document}
\begin{align*}
&\op x\\
&\sigma x
\end{align*}
\end{document}


• Missing character: There is no 𝜎 (U+1D70E) in font [lmroman10-regular]:+tlig ;! Jan 23 at 17:59
• @JosephWright So how can I work around it? Jan 23 at 18:03
• On the edit: you get the warning again as STIX Two Text doesn't have a glyph in slot U+1D70E (MATHEMATICAL ITALIC SMALL SIGMA) as it's a text font. Jan 25 at 10:28

5 Answers

with classical font setup \mathrm has no effect on sigma so \mathrm{\sigma} is \sigma with no font change, so you could use

\newcommand\op{\mathop{{}\sigma}}


which will work with or without unicode-math

Operators are taken from the text font, so you need one that is suitable and has Greek letters. For example

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}
\setmainfont{STIX}
\usepackage{unicode-math}
\setmathfont{STIX Two Math}
\DeclareMathOperator{\op}{\sigma}
\begin{document}
\begin{align*}
&\op x\\
&\sigma x
\end{align*}
\end{document}


(Without the glyph available, the log shows a warning: Missing character: There is no 𝜎 (U+1D70E) in font [lmroman10-regular]:+tlig ;! in the case of the code in the question.)

• Thanks. So why does the operator work when the unicode-math package is not loaded, as in the first example code listed in my original question? Jan 23 at 18:35
• Also: is there a way to solve the problem without changing the main text font I currently use? Jan 23 at 18:37

In 8-bit TeX fonts have very limited size, so Knuth came up with the concept of families, which in LaTeX are known as math groups.

The command \sigma is actually a mathcode, four hexadecimal digits that specify what math group the symbol should be taken from. Only mathcodes whose most significant digit is 7 allow the symbol to be taken from the current value of \mathgroup.

This is how \mathrm works: it sets \mathgroup to 0 instead of the default value −1, so mathcodes with 7 as leading digit will point to the desired slot in the font corresponding to mathgroup 0.

This is not a model for unicode-math, where basically there is a single math font, not several. And most characters have mathcode starting with 7, including \sigma, because unicode-math wants to change blocks rather than fonts.

Note also that \mathrm used by a command defined with \DeclareMathOperator switches to the main text font, which doesn't necessarily have a sigma in it (precisely, 𝜎 U+1D70E MATHEMATICAL ITALIC SMALL SIGMA). But it's easy to solve the issue.

\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math}

\setmathfont{STIX Two Math}

\DeclareMathOperator{\op}{\mathnormal{\sigma}}

\begin{document}

$\op x$

$\sigma x$

\end{document}


We are essentially saying to LaTeX that \sigma should be taken from the math symbols font.

For reference, STIX Two Text italic has an italic small text sigma in it.

MWE

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{STIX Two Math}
\setmathfontface{\textsigit}{stixtwotext-italic}[Colour=blue]
\DeclareMathOperator{\op}{\textsigit{\symbol{963}}}
\begin{document}
\begin{align*}
&\op x\\
&\sigma x
\end{align*}
\end{document}


In the second line, \sigma will use the glyph U+1D70E (𝜎, Mathematical Italic Small Sigma) from the chosen math font (STIX Two Math).

However, as explained in the unicode-math manual (section 4.4, Legacy maths ‘alphabet’ commands), unicode-math makes \mathrm use the text font instead of the math font. If you don’t specify a text font (via \setmainfont), then Latin Modern Roman (LMR) will be used. But LMR doesn’t have the required glyph: during compilation, the following message appears.

Missing character: There is no 𝜎 (U+1D70E) in font [lmroman10-regular]:+tlig

I’d like to point out that changing \sigma to σ doesn’t help, since the character U+03C3 (σ, Greek Small Letter Sigma) is also missing from LMR.

Missing character: There is no σ (U+03C3) in font [lmroman10-regular]:+tlig

(A short digression: The same problem would also occur with \Sigma since LMR doesn’t contain the glyph U+1D6F4 (𝛴, Mathematical Italic Capital Sigma). But funnily enough, LMR does contain the glyph U+03A3 (Σ, Greek Capital Letter Sigma), so Σ instead of \sigma will actually print a symbol!)

We can fix this problem in several ways.

• As already mentioned in Joseph Wright’s post, we could change the text font to a font that actually contains the required glyphs, the obvious choice being STIX Two Text. The resulting σ will be upright.

• unicode-math defines several new font commands that work similar to \mathrm, \mathit, etc., but always use the maths font instead of the text font. Two of these new commands are \symup for upright text and \symit for italic text. Wrapping one of these commands around \sigma will force unicode-mathto take the resulting symbol from the maths font (STIX Two Math).

\documentclass{article}
%\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{STIX Two Math}
\DeclareMathOperator{\op}{\symup{\sigma}}
\begin{document}
\begin{gather*}
\op x \\
\mathop{\sigma} x
\end{gather*}
\end{document}


If we use \symup then the resulting σ will be upright; if we use \symit then it will be italic.

• We can tell unicode-math that we always want \mathrm to be taken from the maths font anstead of from the text font. As explained in the unicode-math manual (4.4.2, in particular Table 4), this can be done by passing the option mathrm=sym to unicode-math.

\documentclass{article}
%\usepackage{amsmath}
\usepackage[mathrm=sym]{unicode-math}
\setmathfont{STIX Two Math}
\DeclareMathOperator{\op}{\sigma}
\begin{document}
\begin{gather*}
\op x \\
\mathop{\sigma} x
\end{gather*}
\end{document}


The resulting σ will be upright.

These solutions will look as follows (depending on whether σ will appear upright or italic).