latex3 / fontspec

Font selection in LaTeX for XeTeX and LuaTeX
http://latex3.github.io/fontspec/
LaTeX Project Public License v1.3c
274 stars 33 forks source link

Kernings lost in \mathrm and friends #387

Open RuixiZhang42 opened 4 years ago

RuixiZhang42 commented 4 years ago

Description

Kernings are lost in \mathrm and friends. According to David Carlisle in his answer:

\mathrm \mathit and friends are designed to allow multi-letter identifiers so use the same kerning and ligatures as the text fonts…

Check/indicate

Minimal example demonstrating the issue

\documentclass{article}
%\usepackage{newtxtext}
%\usepackage{newtxmath}
\usepackage{fontspec}
\setmainfont{texgyretermes-regular.otf}
\setsansfont{texgyreheros-regular.otf}
\begin{document}
\noindent
Test $\mathrm{VAR}_p(X)$\\
Test VAR$_p(X)$\\
\sffamily
Test $\mathsf{VAR}_p(X)$\\
Test VAR$_p(X)$
\end{document}

Further details

For some reasons I cannot run LuaLaTeX, but I suspect the same issue exists.

Here is the result with fontspec under XeLaTeX or LuaLaTeX:

nokern

And here is the result with TFM-based newtxtext and newtxmath under XeLaTeX or LuaLaTeX:

kern

wspr commented 4 years ago

Hmmm. These examples are always tricky as the LaTeX wrappers around the TeX primitives in one sense hide a lot of the detail. I’ll try to develop a plain TeX example to show the same thing. This could be something that fontspec can’t do anything about...

RuixiZhang42 commented 4 years ago

@wspr I just confirmed the same issue of missing kerning under LuaLaTeX.

This is very likely a bug introduced by recent versions of fontspec because I remembered having a lot of \VAR in my documents (defined via \DeclareMathOperator) but only today I started to notice the missing kerning (I could be wrong though!)

RuixiZhang42 commented 4 years ago

@wspr Ligatures are lost too (sorry for the multiple postings):

\documentclass{article}
\usepackage{fontspec}
\begin{document}
\noindent
Test $\mathrm{VARfi}_p(X)$\\
Test VARfi$_p(X)$
\end{document}

Commenting out \usepackage{fontspec} restores the desired behavior.

RuixiZhang42 commented 4 years ago

Looks like I didn’t search hard enough: See Ligatures are not used in OpenType maths families? Seems to be an engine problem :(

@wspr Please feel free to close this issue if you deemed this to be out of the scope of fontspec.

RuixiZhang42 commented 4 years ago

Perhaps the best thing fontspec can do is mentioning this somewhere in the documentation? There is nothing to do under XeTeX, so I am resorting to

\DeclareMathOperator\VARfi{V\kern-0.125emAR\char"FB01 }

Ugly, but at least usable.

stone-zeng commented 4 years ago

Consider the solution given by @u-fischer in https://tex.stackexchange.com/a/277395?

RuixiZhang42 commented 4 years ago

@stone-zeng Ulrike Fischer’s solution had one oversight: One must also pass any other OpenType features in addition to Renderer=Basic. XeLaTeX does not need this extra step and works out of the box (well, except for kerning, ligatures, etc.).

\documentclass{article}

\usepackage{amsopn}% For \DeclareMathOperator
\usepackage{fontspec}
\setmainfont{SourceSansPro-Regular.otf}[
  StylisticSet=2
  % Single-story lowercase a and straight-terminal lowercase l
]
\DeclareMathOperator\Stable{Stable}
\DeclareMathOperator\Var{Var}

\makeatletter
\ifx\XeTeXversion\@undefined
  % Modified from Ulrike Fischer
  \newfontfamily\SourceSansProOperator{SourceSansPro-Regular.otf}[
    Renderer=Basic,
    % Any additional OpenType features must be manually specified:
    StylisticSet=2
  ]
  \AtBeginDocument{%
    \SourceSansProOperator
    \DeclareSymbolFont{operators}\encodingdefault\f@family\mddefault\updefault
    \normalfont
  }
\fi
\makeatother

\begin{document}
A stable distribution is denoted\\
by $\Stable(\alpha,\beta,\gamma,\delta)$. The variance\\
of a stable random variable~$X$ is\\
$\Var(X)=\infty$ for $1\le\alpha<2$.
\end{document}

xelatex

lualatex-without-additional

lualatex-with-additional