[Tex/LaTex] Bad spacing of math letters within italic text

fontsitalicitalic-correctionspacing

I use the standard AMS theorem style, which means that my theorems are set in italic. In combination with math variables, this sometimes gives horrible spacing: The input If $U$ or $V$ \dots yields

I see two spacing problems here: The space between "If" and "U" is too small, and the space between "U" and "or" is too large. Thus, the output would look a lot better if the "U" would be moved a bit to the right. One non-solution is to remove the dollar signs: If U or V \dots yields

Here the spacing is a lot better, but now the problem is that a different font (namely italic) is used for "U" and "V", which is similar but not quite the same. Another non-solution is to use italic correction \/ after "If": This only corrects the first space (and it is not nice if one has to remember typing \/ all the time).

My present "solution" is to apply manual corrections where I find it appropriate, which of course is a real nuisance. Does anyone have a better solution? Do XeTeX or LuaTeX offer something?

(I think I do understand what causes the problem. The idea is to show the bounding boxes of the relevant characters in both examples:

       

What you see is that the spacing of the bounding boxes is good in both cases. But the italic letters tend to stick out of their boxes to the right, and with "U" (in the right picture) and "o" you see that they have some white space in the left of the box. The math "U" (in the left picture), however, does not have this white space in the left, and it doesn't stick out to the right. As a result, the math "U" sits too far to the left.)

EDIT:

Khaled is quite right, the space between the math "U" and "or" is so large since the math "U" includes an italic correction. This is explicitly described in the infamous Appendix G of the TeXbook, rule 17. So the math "U" doesn't stick out of its box since the box includes the italic correction, and this is quite alright if the math in embedded in roman text. I just have no idea how to get rid of the italic correction if the math is already in some italic text!

Best Answer

OK, I produced an absolutely crazy "solution" myself. This is mostly to make clearer what the problems are; I wouldn't suggest using the (very long) code below. This "solution" only provides italic correction for single letters A to Z and a to z, and it works by making $ active. (I could also have used \( and \), but I don't like those.) Moreover, everything is adjusted "by hand" for 10pt CM fonts, so it won't work for other fonts (but should approximately work for other font sizes). Here's the output:

In the 1st line you see the result of If $U$ or $V$ ... without any correction; in the 2nd line my correction is applied, and in the 3rd line the $s are omitted, i.e., the usual italic font is used. I'm not claiming that the 2nd line is good on all counts, e.g. the space between "f" and "from" is rather small. What I wanted to achieve is that the spacing is just as with the "normal" italic font, that is, in the second and third lines the spacing is (almost) the same. (The 2nd line is slightly longer since the math letters are wider.)

Note in particular that the spacing before punctuation in the 2nd line is different from the 1st line. (I'm not sure which version is the better one.) Clearly, the positioning of "U", "V" and "Y" in the 1st line is not good (I would say horrible); in the 2nd line it's a lot better.

Of course one could change all these numbers in my code to try and further improve the spacing. But I only wanted to point out something else: If you look at the numbers, then you see that it would be very hard indeed to have this correction "automatically" and without changing the font metrics.

\documentclass{article}

\makeatletter
\let\mydollar=$
\catcode`\$=\active
\def\my@testtoken{\my@testtoken}
\def$#1${\ifx\my@testtoken#1\my@testtoken
           \mydollar\mydollar
         \else
           \test@single@character#1\my@testtoken
         \fi
        }
\def\test@single@character#1#2\my@testtoken{%
         \def\math@format##1{\mydollar##1\mydollar}%
         \ifx\mytesttoken#2\mytesttoken
           \ifcat#1a%
             \ifdim\fontdimen\@ne\font>\z@
               \def\math@format##1{\mydollar\xdef\currentfont{\the\textfont1}\mydollar
                                   {\corrected{##1}}%\currentfont##1}%
                                  }%
             \fi
           \fi
         \fi
         \math@format{#1#2}%
        }
\def\corrected#1{\csname @correct@#1\endcsname}
\def\correct#1#2,#3,{\expandafter\def\csname @correct@#1\endcsname{\mydollar\mskip#2mu#1\mskip-#3mu\mydollar}}
\makeatother

\correct A0.15,0,    %1st number is the correction before the letter,
\correct B0.3,1.5,   %2nd number is (minus) the correction after it.
\correct C1.75,2.2,
\correct D0.25,1.4,
\correct E0.3,1.7,
\correct F0.3,1.95,
\correct G1.8,1.15,
\correct H0.25,2.6,
\correct I0.3,2.6,
\correct J0.1,2.2,
\correct K0.3,2.4,
\correct L0.25,0.6,
\correct M0.3,2.6,
\correct N0.3,2.6,
\correct O1.75,1.3,
\correct P0.2,1.5,
\correct Q1.75,1.3,
\correct R0.2,0.2,
\correct S0.4,1.8,
\correct T2.7,1.9,
\correct U2.4,2.6,
\correct V2.4,2.95,
\correct W2.4,2.9,
\correct X0.4,2.5,
\correct Y2.6,3.1,
\correct Z0.4,2.2,
\correct a1.2,1,
\correct b1.2,0.3,
\correct c1.2,0.2,
\correct d1.2,0.8,
\correct e1.2,0.85,
\correct f-1.5,3.5,
\correct g0.7,1.2,
\correct h0.4,1,
\correct i1,1.4,
\correct j-0.5,2.2,
\correct k0.4,1.5,
\correct l0.9,1.8,
\correct m1,0.95,
\correct n1,0.95,
\correct o1.2,0.3,
\correct p1,0.3,
\correct q1.2,1.2,
\correct r1,2,
\correct s0.5,1,
\correct t1,1.25,
\correct u1,0.95,
\correct v1,1.55,
\correct w1,1.5,
\correct x0.4,1.75,
\correct y1,1.2,
\correct z0.4,1.75,

\newcommand\test[1]{%
    {\let$\mydollar #1} \par
    #1 \par
    \let$\relax #1
    }

\begin{document}
\it
\test{If $U$ or $V$ and $X$, and $f$ from $j$.  Let $T$ be $S$ if $Y$.}
\end{document}

--- edit ---

Here's a version of the above correction table for use with newtxtext and newtxmath.

\correct A-1.3,-0.2,
\correct B-0.6,0.3,
\correct C0.2,0.9,
\correct D-0.6,0.4,
\correct E-0.55,1.4,
\correct F-2.6,1.1,
\correct G0.0,0.5,
\correct H-0.4,1.0,
\correct I-0.75,1.3,
\correct J-1.0,1.15,
\correct K-0.25,1.3,
\correct L-0.9,0.95,
\correct M-1.0,1.35,
\correct N-2.55,1.55,
\correct O0.25,0.5,
\correct P-0.65,0.45,
\correct Q0.2,0.5,
\correct R-0.8,0.2,
\correct S-0.4,0.6,
\correct T0.7,1.85,
\correct U0.5,1.05,
\correct V0.5,1.65,
\correct W0.5,1.5,
\correct X-1.0,1.45,
\correct Y1.0,1.95,
\correct Z-1.0,1.45,
\correct a-0.3,0.25,
\correct b-0.5,-0.3,
\correct c-0.3,0.35,
\correct d-0.5,0.9,
\correct e-0.25,0.35,
\correct f-3.15,3.15,
\correct g-0.05,0.55,
\correct h-0.6,0.35,
\correct i0.35,0.7,
\correct j-2.0,1.3,
\correct k-0.65,1.3,
\correct l-0.1,1.1,
\correct m-0.2,0.15,
\correct n-0.2,0.0,
\correct o-0.25,0.05,
\correct p-0.75,0.0,
\correct q-0.2,0.4,
\correct r0.2,1.3,
\correct s-0.55,0.45,
\correct t0.15,1.2,
\correct u0.1,0.25,
\correct v-0.1,0.75,
\correct w-0.25,0.6,
\correct x-0.95,0.85,
\correct y-1.0,0.7,
\correct z-0.5,0.55,