olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
33 stars 14 forks source link

strange kerning after some letters in math #152

Open gromadan opened 9 months ago

gromadan commented 9 months ago

Try

\fontfam[LMfonts]
$T$'
\bye

The apostrophe seems too close to the letter. Not sure if this is a feature or a bug, but I think it does not look too good (imagine you have a text in quotation marks ending with $T$). The same works with $P$. Also if you write something like $T$ h, the letter h seems to be too close as if there was no space. If you comment out the \fontfam line, there is no problem.

olsak commented 4 months ago

Sorry, I don't know, where the problem is. In LuaLaTeX or in the math font. If you try lualatex with \usepackage{unicode-math} and $T$' then you get the same bad result.

Udi-Fogiel commented 4 months ago

The following seems to work:

\mathitalicsmode = 2
\fontfam[LMfonts]
$T$'
\bye

but it might present new problems:

\fontfam[LMfonts]
\typosize[40/48]\parskip=40pt
\margins/1 (150,110) (7,7,7,7)mm
\footline={}
\fornum 0..2 \do{\mathitalicsmode=#1\relax #1:
            $T$'\quad $f(T)$\quad
            $1\over T$\quad $\int_a^b$\par}
\bye

image

with some manual italic correction you can get a decent output

\fontfam[LMfonts]
\typosize[40/48]\parskip=40pt
\margins/1 (150,110) (7,7,7,7)mm
\footline={}
\fornum 0..2 \do{\mathitalicsmode=#1\relax #1:
            $T\/$'\quad $f(T\/)$\quad
            $1\over T\/$\quad $\int_a^b$\par}
\bye

image

maybe the italic correction can be inserted automatically using mlist_to_hlist callback. I like the second option most (i.e with \mathitalicsmode=1), although the spacing between f and ( is better in the last one.

see https://tex.stackexchange.com/questions/697498/luatex-unicode-math-and-italic-correction?noredirect=1&lq=1 for more information.

Udi-Fogiel commented 4 months ago

Maybe you will be interested in this talk: https://www.youtube.com/watch?v=WuRa4cnXK-I

olsak commented 4 months ago

Your first problem was that luaTeX doesn't add italic correction at the end of the math list when Unicode math is used. With classical math, the italic correction is added. And I don't know how there is this different behavior. Try:

A:$T$'

\fontfam[lm]
B:$T$'

\nonstopmode\showlists
\bye

The first A:T' is printed by classical math font, the second one B:T' by Unicode math font. You can see in the log file:

### vertical mode entered at line 0
### current page:
\glue(\topskip) 3.11125
\hbox(6.88875+0.0)x455.24408, glue set 414.95593fil, direction TLT
....
.\hbox(0.0+0.0)x20.0, direction TLT
.\_tenrm A
.\_tenrm :
.\mathon
.\_mF T
.\kern1.3889 (italic)
.\mathoff
.\_tenrm '
.\penalty 10000
....
.\hbox(0.0+0.0)x20.0, direction TLT
.\_tenrm B
.\_tenrm :
.\mathon
.\_mF 𝑇
.\mathoff
.\_tenrm ’
.\penalty 10000

The italic correction \kern...(italic) is missing in the B:T'. Why? We can insert the italic correction only manually and only if \mathitalicsmode=1. Why? Why there is unusable \mathitalicsmode=2 which breaks \int operators?

\fontfam[lm]
\mathitalicsmode=1
C:$T\/$'

\nonstopmode\showlists
\bye

prints to the log file:

.\hbox(0.0+0.0)x20.0, direction TLT
.\_tenrm C
.\_tenrm :
.\mathon
.\_mF 𝑇
.\kern1.48 (italic)
.\kern0.0 (font)
.\mathoff
.\_tenrm ’
.\penalty 10000

Why this feature isn't default? What does mean \kern1.48 (italic) and \kern0.0 (font)?

We can try to implement automatic italic correction using mlist_to_hlist callback. But first of all, we need to know the answers to questions formulated above in order to know the concept of italic corrections in Unicode math.

olsak commented 4 months ago

You can experiment with the following code, which adds the italic correction at the end of the inline math mode. But I don't understand why this is not default behavior. Is it a bug in luaTeX?

\directlua{ % adds italic correction of the last character after inline math
luatexbase.add_to_callback("mlist_to_hlist",
    function(head, style, penalties)
        head = node.mlist_to_hlist(head, style, penalties)
        if style == "text" then
            for n in node.traverse(head) do
                if n.next == nil and n.id == 29 then % last is glyph
                    local k = font.fonts[n.font].characters[n.char].italic
                    if not(k==nil) and (k>0) then
%                                 print("kern", k)
                        local kn = node.new ("kern")
                        kn.kern = k
                        node.insert_after(head, n, kn)  
                    end
                end
            end
        end
        return head
    end, "italcorr after math")
}

\fontfam[lm]

B:$T$'

\nonstopmode\showlists
\bye
Udi-Fogiel commented 4 months ago

To my understanding it is not a bug. I'm not sure why this is not the default, but one can argue that in some cases the italic correction is redundant, namely before punctuation (comma or a dot). Additionally Hans claims that the OpenType Math fonts standard is a mess, so maybe when LuaTeX was developed this was their choice.

Note that in addition to the redundant italic correction before punctuation, the code does not fix sublists. As demonstrated above, the missing italic correction is causing the fraction rule to be too short.