15

How can I get the kerning and ligatures of text mode in an upright math font with the unicode-math package loaded?

1. Without unicode-math

Code:

\documentclass{article}

\begin{document}
  affinity\par
  $\mathrm{affinity}$\par
\end{document}

Output:

Kerning and ligatures of text mode.

2. With unicode-math

Code:

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

\begin{document}
  affinity\par
  $\mathrm{affinity}$\par
\end{document}

Output:

Kerning and ligatures of math mode. I would like those of text mode here.

2 Answers2

9

The use of math alphabet commands in unicode-math is somewhat broken there are open issues at github which mentions \mathit but \mathrm is the same.

You can redefine \mathrm to use the Roman text font as follows:

enter image description here

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

\setmathfont{Latin Modern Math}
\setmathfont[range=\mathup]{Latin Modern Roman}
\begin{document}
  affinity\par
  $\mathrm{affinity}$\par
\end{document}

not working version including mathit

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

\setmathfont{Latin Modern Math}
\setmathfont[range=\mathup]{Latin Modern Roman}
%\setmathfont[range=\mathit]{lmroman12-italic}% affects normal math as well.
\DeclareMathAlphabet\xxmathit{EU2}{lmr}{m}{it}% EU1 for xetex EU2 for luatex
\def\xmathit#1{\xxmathit{\mathup{#1}}}% right font but ff ligature broken
\begin{document}
\showoutput
  1 affinity\par
  2 \textit{affinity}\par
  3 $affinity$\par
  4 $\mathrm{affinity}$\par
  5 $\xmathit{affinity}$\par
\end{document}
David Carlisle
  • 757,742
  • Thank you! Now if I want to do the same thing but with \mathit, what is the name of the Latin Modern italic text font? (Latin Modern Roman Italic or Latin Modern Roman-Italic don't work.) – Géry Ogam May 22 '15 at 18:00
  • @Maggyero I'm not sure, I added an example to the question with two possible ways one commented out, the other selects the right font but ligatures not working. As I say I opened a github issue last year on mathit not working with unicode-math, you could ping will there..... – David Carlisle May 22 '15 at 19:20
  • Is there a way to contact him directly on Stackexchange? – Géry Ogam May 22 '15 at 21:57
  • no, see http://meta.tex.stackexchange.com/questions/6164/is-there-a-way-to-message-a-user-on-this-site, although he regularly passes by http://tex.stackexchange.com/users/179/will-robertson (I could mail him:-) – David Carlisle May 22 '15 at 22:05
  • Alright, yes could you email him? By the way, I read the Github issue you opened but at the end of the thread Will says: "(Discussion continued on LaTeX-L.)" without any url, what it this LaTeX-L? – Géry Ogam May 23 '15 at 07:50
  • @Maggyero latex-l the main discussion list for latex development (not a help list) http://listserv.uni-heidelberg.de/cgi-bin/wa?A0=latex-l – David Carlisle May 23 '15 at 16:42
  • Thank you. I've just posted a related question here and I would like very much to have your opinion on this matter since you are one of the most competent on this forum (nobody has really answered yet). – Géry Ogam May 24 '15 at 00:08
  • I've just realized that in classic TeX \mathnormal, \mathit and \textit should result in 3 different fonts being used, for instance Cambria Math, Cambria and Palatino respectively. That concept of 2 fonts for math mode (one math font, and one text font that may differ from the text font for text mode) was a little hard to understand. Now about the unicode-math package, what do you think of my suggestion below? – Géry Ogam May 26 '15 at 01:00
1

To follow-up the discussion in the comments of David Carlisle's answer about the unicode-math package:

In LaTeX, there are two fonts in math mode:

  • one for letters (variables), usually called math font (better called math letter font);
  • one for words, usually called math text font (better called math word font),

and one text font in text mode, usually called text font.

The text font and the math text font may be different:

\documentclass{article}
\renewcommand{\familydefault}{ppl} % sets Palatino as text font
\begin{document}
    $\mathnormal{affinity}$\par    % math letter font
    $\mathit{affinity}$\par        % math word font
    $\textit{affinity}$\par        % text font
\end{document}

Here \mathnormal, \mathit and \textit result in three different fonts being used: Latin Modern Math, Latin Modern and Palatino respectively.

With the unicode-math package loaded, the output becomes different:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Palatino}             % sets Palatino as text font
\setmathfont{Latin Modern Math}    % sets Latin Modern Math as math letter font
\begin{document}
    $\mathnormal{affinity}$\par    % math letter font
    $\mathit{affinity}$\par        % math word font
    $\textit{affinity}$\par        % text font
\end{document}

Here the package redefines \mathnormal as nothing and \mathit as math letter font (instead of math word font in classic LaTeX). So by default the unicode-math package breaks the standard behaviour of LaTeX. That is why David Carlisle opened an issue at Github as said in his answer (there is another interesting Github issue about the same problem here).

As suggested by David Carlisle in the Github issue, I first thought about introducing new math font commands \mathwxx for math word font in the unicode-math package, to keep backward compatibility:

But finally I am wondering if those new math font commands \mathwxx are needed, and even if they make sense: indeed, when do we need to use an upright math letter font after all? Math letter fonts are only for variables, therefore they should always be in italic, never upright. We only need an upright math font for non variables, that is for math word fonts, so we should better replace the math letter font of the Unicode ranges \mathup and \mathbf by a math word font:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Palatino}
\setmathfont{Latin Modern Math}
\setmathfont[range=\mathup]{Latin Modern Roman} % sets a math word font instead of the default math letter font
\setmathfont[range=\mathbfup]{lmroman12-bold}   % sets a math word font instead of the default math letter font
\let\mathnormal\mathit                          % redefines \mathnormal as before the unicode-math package
\begin{document}
    \noindent
    \texttt{\backslash mathup}: $\mathup{affinity}$\\
    \texttt{\backslash mathbf}: $\mathbf{affinity}$\\
    \texttt{\backslash mathit} or \texttt{\backslash mathnormal}: $\mathit{affinity}$ (default)\\
    \texttt{\backslash mathbfit}: $\mathbfit{affinity}$\\
\end{document}

Thus with that setup we get the standard LaTeX behaviour.

  • I'm not sure the above is really an answer, more an extended comment. But some comments. It is best not to use minimal class for font examples as there are many artefacts due to the fact that it doesn't set up fonts. "two fonts in math mode" isn't right in classic tex (where there are usually four, and up to 16) or unicode-math (where there is usually just one). – David Carlisle May 26 '15 at 08:44
  • 1
    Your initial example is using computer modern not latin modern for math, the missing mathnormal commands are not because \mathnormal is defined to do nothing but as warned in the log Missing character: There is no (U+1D44E) in font cmmi10! the final example says that with that suggested setup you get "the default latex behaviour" but it shows exactly the problem that is raised in github that \mathit is giving the math italic font as accessed by \mathnormal (or no font command at all) instead of a text italic font. – David Carlisle May 26 '15 at 08:47
  • @DavidCarlisle: Yes, this was to continue our discussion with some visual examples. 1. 'It is best not to use minimal', alright, I'll edit the answer. 2. 'There are usually four, and up to sixteen', you're right, actually I meant two fonts in the generic sense: the math font used for variables (math letter italic) and the math font used for constants, operators and subscript specifiers (math word upright). 3. 'Your initial example is using computer modern, not latin modern for math', that's just what I wanted to ask you: how to use Latin modern math without the unicode-math package? – Géry Ogam May 26 '15 at 10:46
  • The site quidelines discourage discourage discousion posted via (non-)answers. For latin modern math then you need something equivalent to unicode-math as it has not been split up and encoded to match the classic math encodings as far as I know. I still don't agree that "two fonts" has any reasonable interpretation here, or that variables are always set italic or in the same font: for variables in classic encodings, single letter latin alphabet is set with math italic, multi-letter latin alphabet is set with \mathit, single letter uppercase Greek is usually set upright (same font as \mathrm). – David Carlisle May 26 '15 at 10:54
  • @DavidCarlisle: 4. 'The missing mathnormal commands are not because \mathnormal is defined to do nothing', alright, thanks. 5. 'It shows exactly the problem that is raised in github', yes, but I have a question: are there really people that use the standard \mathit for writing math words (multi-letter text)? If yes, to not break backward compatibility my suggestion to introduce a new math command \mathwxx for math words should be the opposite: introducing a new math command \mathlxx for math letters and keeping the \mathxx command for math words (default LaTeX behaviour). – Géry Ogam May 26 '15 at 11:07
  • 2
    Yes of course people use \mathit to make multi-letter words, what other use would it have? See for example this answer of egreg's giving \mathit as the solution to make a variable called AB as opposed to the product of two variables A and B http://tex.stackexchange.com/questions/16433/math-letters-italic-correction/16442#16442 but search the internet for \mathit you will find it is almost always used with more than one letter in its argument. – David Carlisle May 26 '15 at 11:12
  • @DavidCarlisle: Thanks for this great example of use of \mathit. I had a discussion with egreg in his answer at the link you provided, so now I truly understand the difference with \textit. And the same example applies to \mathrm and \textrm, even if it's a bit harder to notice. So I removed my bad conclusion about the changes to be made in the unicode-math package in this thread and now really want to convince Will Robertson and Khaled Hosny to implement the correction you suggested to their package. Do they plan to do it soon or they don't agree with you? – Géry Ogam May 27 '15 at 23:12
  • 1
    I agree with the comments, it's just a matter of resources (i.e., time). I'd hoped to address this many months ago by now… apologies for the inconvenience. – Will Robertson May 28 '15 at 00:57
  • @WillRobertson: Fantastic, thank you very much! There's exactly the same problem with ConTeXt since it uses Unicode math as well. I'll try again to talk about it to Hans Hagen. Could you just give me a short overview of the solution to the problem so that I can convince him (I'm just an average TeX user)? I've already pointed out to him the bug in the ConTeXt mailing list a few weeks ago but I was not very convincing: see here. – Géry Ogam May 28 '15 at 08:37