alerque / libertinus

The Libertinus font family
Other
928 stars 57 forks source link

math: uneven spacing of ⟨f,f⟩ and ‖f‖ #495

Closed mdeff closed 2 years ago

mdeff commented 2 years ago

Describe the bug When typesetting ⟨f,f⟩ and ‖f‖, the spacing between the letter f and the angles and bars is uneven.

Steps to reproduce Compile the following document with latexmk -pdflua.

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{LibertinusMath-Regular.otf}
\newcommand*{\norm}[1]{{\ensuremath{\left\|{#1}\right\|}}}
\newcommand*{\inner}[1]{{\ensuremath{\left\langle{#1}\right\rangle}}}
\begin{document}
    \[
        \inner{f, f}       \hspace{2em}
        \norm{f}_2^2       \hspace{2em}
        \inner{g, g}       \hspace{2em}
        \norm{g}_2^2       \hspace{2em}
    \]
\end{document}

Expected vs. actual behavior The spacing should be similar on the left and right, as it is when typesetting ⟨g,g⟩ and ‖g‖.

Screenshots 20211227_191848

mdeff commented 2 years ago

Note that many OpenType math fonts have this issue.

For example, compare the Latin Modern OpenType (\setmathfont{latinmodern-math.otf}) 20211227_155345_latinmodern

with the Latin Modern from TeX (i.e., without \usepackage{unicode-math}): 20211227_155248_without_unicode-math

khaledhosny commented 2 years ago

Are you using LuaTeX? This is a known LuaTeX issue as it ignores italic correction by default. If you search tex.stackexchange.com/ you might find a way to activate the italic correction. XeTeX should not have this problem.

mdeff commented 2 years ago

Thanks for the hint! I'm using LuaTex.

There's indeed an answer on the TeX Stack Exchange where you suggest the following:

\everymath{\luatexcatcodetable\CatcodeTableLaTeX}
\everydisplay{\luatexcatcodetable\CatcodeTableLaTeX}

But \luatexcatcodetable and \CatcodeTableLaTeX are giving me an "undefined control sequence" error. Would you know of any other way?

XeTeX (i.e., compiling with latexmk -pdfxe) indeed does not have this problem.

mdeff commented 2 years ago

STIX Two and Cambria don't have this problem, neither with LuaTeX nor XeTeX.

redtrumpet commented 2 years ago

The pics produced by the lua-visual-debug package indicate, that italic correction does indeed not work for "STIX Two" either. Here is what it looks like without unicode-math:

without unicode-math

Note the yellow box to the right of the second f, which indicates kerning. Now with \usepackage{unicode-math}, it looks like this: with unicode-math

Using \setmathfont{STIX Two Math} produces

stix two

which still has no kerning. However, the box of the f seems to be bigger, preventing a real issue. But if you compare the STIX Two Math font to Libertinus Math in fontforge you will see that both have an "italic correction" indicator, which is not respected.

I also opened a new question on tex.SE, where someone noted that it works, if one does not use \left and \right:

libertinus without left right

redtrumpet commented 2 years ago

There seems to be a solution by adding \mathitalicsmode=1 in the preamble. However, for some reason I don't understand yet, you have to remove the surrounding brackets { and } from the commands. So

\mathitalicsmode=1
\newcommand*{\norm}[1]{{\ensuremath{\left\|#1\right\|}}}
\newcommand*{\inner}[1]{{\ensuremath{\left\langle#1\right\rangle}}}

seems to work.

mdeff commented 2 years ago

That works! Thanks for investigating and sharing your findings.

mdeff commented 2 years ago

Though italic correction is not ideal for every font. For example, with STIX Two, italic correction introduces too much space. (Although that's a slight problem compared to the overlap with Libertinus.)

It's too bad that different fonts encode this information differently. 😕

\setmathfont{STIX Two Math} without \mathitalicsmode=1: 20220427_004444

\setmathfont{STIX Two Math} with \mathitalicsmode=1: 20220427_004229

khaledhosny commented 2 years ago

STIX fonts are under active development, please report issues you have https://github.com/stipub/stixfonts/issues (they might be working around the broken LuaTeX default by increasing the right sidebearing and forgot to remove the italic correction).

Firestar-Reimu commented 1 year ago

need package physics

\ket{f} |f\rangle \expval{f,f} \langle f,f \rangle

without \mathitalicsmode=1

\usepackage{unicode-math}
\unimathsetup{mathrm=sym,mathbf=sym,mathsf=sym,bold-style=ISO}
\usepackage[math=+ss08]{libertinus-otf}

Screenshot_20221115_192800

with \mathitalicsmode=1

\mathitalicsmode=1
\usepackage{unicode-math}
\unimathsetup{mathrm=sym,mathbf=sym,mathsf=sym,bold-style=ISO}
\usepackage[math=+ss08]{libertinus-otf}

Screenshot_20221115_192901

Firestar-Reimu commented 1 year ago

\mathitalicsmode=1 has limited effect, for example: $ \left\langle f,f\right\rangle $ has correct spacing, but $ \left\langle {f,f}\right\rangle $ still outputs wrong spacing .

The definition of \expval in physics looks like this:

\DeclareDocumentCommand\expectationvalue{ s s m g }
{ % Expectation value
\IfNoValueTF{#4}
{
\IfBooleanTF{#1}
{\vphantom{#3}\left\langle\smash{#3}\right\rangle} % Starred implicit form: no resizing
{\left\langle{#3}\right\rangle} % Normal implicit form: auto sizing
}
{
\IfBooleanTF{#1}
{
\IfBooleanTF{#2}
{\left\langle{#4}\middle\vert{#3}\middle\vert{#4}\right\rangle} % Double starred explicit form: total auto sizing
{\vphantom{#3#4}\left\langle\smash{#4}\middle\vert\smash{#3}\middle\vert\smash{#4}\right\rangle} % Starred explicit form: no resizing
}
{\vphantom{#3}\left\langle{#4}\middle\vert\smash{#3}\middle\vert{#4}\right\rangle} % Normal explicit form: only resize based on bra/ket arguments
}
}

You can see it added { } or \smash. Both currently invalidate the automatically added italic correction. It should be because, with { } or \smash, their contents will form a new ord atom, and no italic correction is applied to the end of this ord atom.

Italic correction can only be added manually: \expval{f,f\/}.

Firestar-Reimu commented 1 year ago

\mathitalicsmode=1 has limited effect

so maybe increase width of letter f is the ultimate solution?

Firestar-Reimu commented 1 year ago

Different between mathitalicsmode=1 or 2

\documentclass{article}
\usepackage{unicode-math}
\mathitalicsmode=1
\setmathfont{LibertinusMath-Regular.otf}
\newcommand*{\norm}[1]{{\ensuremath{\|{#1}\|}}}
\newcommand*{\inner}[1]{{\ensuremath{\langle{#1}\rangle}}}
\begin{document}
    \[
        \inner{f, f}       \hspace{2em}
        \norm{f}_2^2       \hspace{2em}
        \inner{g, g}       \hspace{2em}
        \norm{g}_2^2       \hspace{2em}
    \]
\end{document}

Screenshot_20230316_203758

\documentclass{article}
\usepackage{unicode-math}
\mathitalicsmode=2
\setmathfont{LibertinusMath-Regular.otf}
\newcommand*{\norm}[1]{{\ensuremath{\|{#1}\|}}}
\newcommand*{\inner}[1]{{\ensuremath{\langle{#1}\rangle}}}
\begin{document}
    \[
        \inner{f, f}       \hspace{2em}
        \norm{f}_2^2       \hspace{2em}
        \inner{g, g}       \hspace{2em}
        \norm{g}_2^2       \hspace{2em}
    \]
\end{document}

Screenshot_20230316_203855