Bad spacing of math letters within italic text

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,

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}

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}

\letter`a
\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

\letter`A

\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}

table of all OML letters with and without italic corrections

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

examples