# Bad spacing of math letters within italic text

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!

• I don't no much TeX to have an answer, but you need to prevent italic correction inside the second formula after the U, the italic correction is the extra white space you see after the U. Sep 26, 2010 at 17:58
• nobody has mentioned that if a line in an italic paragraph begins with a word like "Very", that line will look indented relative to the lines above and below. so it's not only math that's a problem ... the solution will need to know the shapes of the characters, not just the metrics. Sep 20, 2011 at 20:26
• @barbara: Thanks for your comment. I've mentioned (actually rather complained about) this in another question. The solution is using microtype with an improved protrusion table. I've mailed this improved table to Robert Schlicht, but I don't know if it's incorporated into microtype already. Sep 21, 2011 at 7:43
• LuaTeX seems to have some mmathitalicsmode thing tex.stackexchange.com/questions/642201/…. I didn't try it myself however. Jul 29, 2022 at 1:46

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,

• Nice job. Maybe you should submit this patch to mathtools? Nov 4, 2010 at 18:27
• @Matthew: Glad that you like it, but as I wrote above: It's more a case study since it is tested only with 10pt CM, and works only for single latin letters. I'd need someone professional to make a good patch out of it, I think. Nov 5, 2010 at 9:17
• @EricPeterson - Thanks for providing the addendum for thetxfonts package. The txfonts package is actually considered to be somewhat obsolete, and a successor package -- indeed, a pair of packages: newtxtext and newtxmath -- has been available for several years now. Might you be interested in providing a similar patch that's applicable to the newtxmath package? (You could probably use your work for the txfonts package as a good starting point.)
– Mico
Oct 15, 2017 at 0:20
• I didn't realize that—Cambridge University Press is still advocating it as their standard font choice, so I thought it was still in rotation. In fact, something seems to be wrong with what I've posted above; I'll use the newer package and fix the tables both. Oct 15, 2017 at 2:58
• I know this is quite an old subject, but in case you are interested I somehow automated your code here: tex.stackexchange.com/questions/513441/… Oct 25, 2019 at 20:07

This is partially fixed in the mathtools package (see section 4.1 of the package documentation).

Here is an example. Note that math must be typed using $$ and $$:

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
\mathtoolsset{mathic=false}
\textit{If $$U$$ or $$V$$ \dots.}
\par Good:
\mathtoolsset{mathic=true}
\textit{If $$U$$ or $$V$$ \dots.}
\end{document}


• Hmm, no, this only solves half of my problem: It automatically puts the italic correction after "If" that I mentioned in my question (so it saves me typing \/ all the time), but it does not correct the space between "U" and "or". Moreover, I don't like using $$ and $$. Still, this answer is not bad: The output does look better with mathic=true. Sep 26, 2010 at 16:46
• I'm not a guru, but mathtools just redefines '$$' so that it can do the italic correction, but it doesn't alter '' Oct 7, 2010 at 16:52 • So there's a need for a better package for italic correction in maths than mathtools. Oct 12, 2010 at 5:29 • Has Knuth given any recommendations about how to deal with this problem? I can't find anything explicitly about it in the TeXbook. Interestingly, on page 340 is, effectively, {\sl ... of math is ...}. (It does look odd in typeset form on page 341.) – MSC Mar 10, 2011 at 19:35 • @MSC-- as far as i know, knuth is aware of the problem but it's a much bigger one than he wanted to tackle, and it really didn't affect "the art of computer programming" which, of course, is the reason tex exists at all. he's also quite willing to make small manual adjustments for things that don't happen too often. for example, i'm willing to bet that he always puts \/ after an "f" that comes just before math in an italic environment (unless, of course, the "f" happens to fall at the end of a line, in which case i'm not sure). Sep 21, 2011 at 12:33 Update: this previous answer to another, related, question already mentioned the \noic macro which is discussed here. Here is how to suppress the italic correction when exiting math after a letter. \documentclass[a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[vscale=0.82]{geometry} \begin{document} \ttfamily \def\noic{\sb{}\kern-\scriptspace } \def\mathfont{\usefont{OML}{cmm}{m}{it}} \mathsurround0pt % is default anyhow \newbox\letterbox \newcount\letter %\the\scriptspace \begin{verbatim} \def\noic{\sb{}\kern-\scriptspace } <letter>\noic gives the same as \usefont{OML}{cmm}{m}{it}<letter> ©jfbu, 2013. <letter> <letter>\/ <letter> <letter>_{} <letter>\noic \end{verbatim} \lettera \noindent\loop \makebox[.05\linewidth]{\char\letter}% \setbox\letterbox=\hbox{\mathfont\char\letter}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\mathfont\char\letter\/}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter_{}}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter\noic}% \makebox[.18\linewidth][r]{\the\wd\letterbox}\\ \ifnum\letter<z \advance\letter by 1 \repeat %\clearpage \letterA \noindent\loop \makebox[.05\linewidth]{\char\letter}% \setbox\letterbox=\hbox{\mathfont\char\letter}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\mathfont\char\letter\/}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter_{}}% \makebox[.18\linewidth][r]{\the\wd\letterbox}% \setbox\letterbox=\hbox{\char\letter\noic}% \makebox[.18\linewidth][r]{\the\wd\letterbox}\\ \ifnum\letter<Z \advance\letter by 1 \repeat \end{document}  On popular request, \clearpage \newgeometry{hscale=0.9} \thispagestyle{empty} \def\original{If U or V and X, and f from j. Let T be S if Y.} \def\improved{If\/ U\noic or\/ V\noic and\/ X\noic, and\/ f from\/ j\noic. Let\/ T\noic be\/ S\noic if\/ Y\noic.} \normalfont\itshape \small \original \improved \medskip \normalsize \original \improved \medskip \large \original \improved \medskip \huge \original \improved \medskip \bigskip \bfseries\boldmath \small \original \improved \medskip \normalsize \original \improved \medskip \large \original \improved \medskip \huge \original \improved  • you've produced a table of the values of the italic correction for cmmi10, but you haven't shown how to use the values in a simple example. i'd like to see a minimal example using the sentence shown in the accepted answer, as \textit{...}, using only the defined macro in the recommended manner. oh, please do it also in several sizes: normal size, \small and \large, without changing the macro definition. Jan 11, 2013 at 19:09 • @barbarabeeton what do you mean by 'how to use the values'? the values are not used, they were just displayed to see the effect of the \noic macro I defined. – user4686 Jan 11, 2013 at 20:07 • i was mistaken about how to use \noic. the use example clarifies that very nicely. thanks. however, the input is really too complicated for most people (and rather error prone for your average typist); i think that using mathtools and \mathtoolsset{mathic=true} us probably the best compromise, even though it requires using \(...$$ rather than $...$. Jan 11, 2013 at 22:47
• @HendrikVogt yes you are right on it not being a foolproof fix for "post math" and I am sorry for the duplicate _{}\kern-\scriptspace! I did not know about this other answer of yours ...
– user4686
Jan 12, 2013 at 13:09
• @jfbu: No need to be sorry! Your answer still gives a huge improvement over the default spacing, and I already gave it a +1. Thanks again! Jan 12, 2013 at 15:25