# 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. –  Khaled Hosny Sep 26 '10 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. –  barbara beeton Sep 20 '11 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. –  Hendrik Vogt Sep 21 '11 at 7:43
add comment

## 3 Answers

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. –  barbara beeton Jan 11 '13 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. –  jfbu Jan 11 '13 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 $...$. –  barbara beeton Jan 11 '13 at 22:47
@barbarabeeton perhaps the developpers of mathtools will find a way to modify \) and add the \noic macro if necessary. But this will be more difficult than the modification of $$ (this modification just adds the italic correction in the appropriate manner, which requires testing the \fontdimen1 parameter of the font; for $$ it seems what one would have to know if the formula ended with a letter: I have not at all tested what \noic does if the formula ends otherwise). Besides, there is no \everyendofmath hook in TeX (so $ is difficult). – jfbu Jan 12 '13 at 10:07 Thanks for your answer - the "post math correction" is quite good in many (most?) cases! But it doesn't always work since the italic correction in math mode is designed for subscript positioning only: compare, e.g., $F\noic$f with $H\noic$f. Also, the "pre math correction" doesn't quite work; see my 3rd comment to Konrad's answer. (I've actually learned a few months ago that one can use _{}\kern-\scriptspace to get rid of the italic correction, but I never tried it here!) – Hendrik Vogt Jan 12 '13 at 12:10 show 3 more comments This is 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} Bad: \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. – Hendrik Vogt Sep 26 '10 at 16:46 I see, it doesn't fully do what it advertises.... And perhaps some guru can explain why mathtools needs the ( )' delimiters to work. – Konrad Swanepoel Sep 26 '10 at 18:18 I'm not a guru, but mathtools just redefines '$$' so that it can do the italic correction, but it doesn't alter '' – Hendrik Vogt Oct 7 '10 at 16:52 So there's a need for a better package for italic correction in maths than mathtools. – Konrad Swanepoel Oct 12 '10 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 '11 at 19:35 show 4 more comments 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}

-
Nice job. Maybe you should submit this patch to mathtools? –  Matthew Leingang Nov 4 '10 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. –  Hendrik Vogt Nov 5 '10 at 9:17
add comment