[Tex/LaTex] italic correction in math mode

italic-correctionkerningletterspacingmath-mode

I use txfonts and \texttt in italics in math mode, but the letters' italic correction is not working: an italic/"TT" letter "d" adjacent to a closing parenthesis touches it. Here is minimal sample code:

\documentclass{article}
\usepackage{times}
\usepackage{txfonts}

\newcommand*{\halfthinspace}{\kern .08333em} % not sure whether I got this value right
\newcommand*{\shorthalfthinspace}{\kern .06em}
\newcommand*{\somefunction}[1]{\textit{F}(#1)}
\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}

\begin{document}

\(\somefunction{\ittt{pd}}\) (what I get)

\(\somefunction{\ittt{pd}\shorthalfthinspace}\) (I want this)

\(\somefunction{\ittt{pd}\halfthinspace}\) (or this)

This is entity \ittt{pd}. (use of \verb|\ittt| within text; the spacing here is good)

\(\ittt{pd} \times \ittt{pd}\) (the space before the times symbol is minimally too thin)

\end{document}

To include additional space in the definition of \somefunction (\newcommand*{\somefunction}[1]{\textit{F}(#1\shorthalfthinspace)}) seems to be semantically the wrong approach. To include additional space in the definition of \ittt seems equally wrong, especially since I would occasionally like to use \ittt within text (see line 4 generated by my sample code) and don't want additional spacing before the sentence-final period. Finally, I may use \ittt in other contexts, such as around a \times operator (see line 5 generated by my sample code); the spacing doesn't only look too thin, it is too thin (easy to verify by turning the result by 180° in your viewer).

What it the easiest way to get appropriate spacing in all situations?

Note: I don't know whether this is an italic correction issue or something to do with \texttt or the structure of my macros. Feel free to improve the title of my question accordingly.

Best Answer

\somefunction

I have remove \textit entirely. In math mode variables (a-zA-Zα-ο) are usually typeset in italic shape. Math mode takes care of the italic correction (between F and ( in this case).
If you want to use \somefunction in text, use always inline math (\(…\)).


It wouldn't be that wrong to use a simple definition for the entity (pd) and “fix” the spacing inside \somefunction (or better: entityFunction/\funcOfEnt/…) except when parenthesized entities will be used more often and outside of a function, like (\entity{pd} + \entity{qr}) x, than the definition of the function should use the already patched \entity to be more consistent.

\textit and \texttt

I don't think there is a total automatic solution available for fixing the problem with ) and following punctuation.

Let's take a look at the options:

\ittt

Your original \ittt was defined as

\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}

\itttm

If you only use your entities in text mode before punctuation, \itttm can work for you. It adds italic correction only in math mode.

\newcommand*{\itttm}[1]{%
    \ifmmode\mmodebeforetrue\else\mmodebeforefalse\fi%
    \textit{\texttt{#1\ifmmodebefore\/\fi}}%
}

\itttp

The macro \itttp typsets a second optional parameter in italic shape (but not in the typewriter family). This has been discussed on TeX.SE already.
In my eyes, this is even too tight.

\DeclareDocumentCommand{\itttp}{m O{}}{%
  \textit{\texttt{#1}#2\/}%
}

\ttit

Switching \textit and \texttt gives us italic correction. But it also adds this italic correction before punctuation mark where you don't want it (i.e., . and ,).

\newcommand*{\ttit}[1]{\texttt{\textit{#1}}}

\ttitc

Like \ttit, but:
The if-macro \@ifnextchar removes the italic correction before . and , with a negative kern.

\emph or \textit?

I also tested both variants with \emph:

  • \emph{\texttt{pd}} is a little better before ), and as good as \ittt before punctuation
  • \texttt{\emph{pd}} is as bad as \ttit.

\entity[*]{<arg>}

I also declared another macro: \entity. It does exist in a non-starred and a starred form.
Use the starred form if you do not want italic correction (i.e. before . and ,).

Conclusion

I think the best solutions are:

  • \itttm if you need italic correction only in math mode.
  • \ttitc if can handle a few cases where the automatically inserted correction has to be re-corrected
  • \entity if you're willing to use a switch (the *) to toggle italic correction (it should be doable to use \@ifnextchar in \entity, too).

Code

\documentclass{article}
\usepackage{times}
\usepackage{txfonts}
\usepackage{array,multirow,xcolor} %                       for presentation
\def\dont{\color[gray]{.5}} %                              *
\usepackage{xparse}%                                       for \itttp and \entity
\newif\ifmmodebefore%                                      for \itttm

\newcommand*{\somefunction}[1]{F(#1)}

\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}
\newcommand*{\ttit}[1]{\texttt{\textit{#1}}}

\newcommand*{\itttm}[1]{%                                  \itttc inserts italic correction \/ if in math mode
  \ifmmode\mmodebeforetrue\else\mmodebeforefalse\fi%
  \textit{\texttt{#1\ifmmodebefore\/\fi}}%
}

\makeatletter %                                            needed because @ is used in macro name
\newcommand*{\ttitc}[1]{%                                  \ttitc checks if . or , follows and kerns
  \texttt{\textit{#1}}%
  \@ifnextchar.{\kern-.1em}{\@ifnextchar,{\kern-.08em}{}}%
}
\makeatother

\DeclareDocumentCommand{\itttp}{m O{}}{%                   optional punctuation will be typeset italic
  \textit{\texttt{#1}#2\/}%
}

\DeclareDocumentCommand{\entity}{s m}{%                     star = no italic correction (before , and .)
  \textit{\texttt{#2}%
  \IfBooleanTF{#1}%
    {}%
    {\/}%
  }%
}

%\usepackage{lua-visual-debug} % use this with LuaLaTeX for debug-mode
\begin{document}\noindent
\begin{tabular}{l@{}c@{ }c@{ }c}
    macro                &              math               &              \verb|\times|              &               text                \\ \hline&             text mode             \\ \hline
    \verb|\ittt{pd}|     &         $(\ittt{pd} )$          &         $ \ittt{pd} \times {}$          &       \ittt{pd}, \ittt{pd}.       \\ \hline
    \verb|\itttm{pd}|    &         $(\itttm{pd})$          &         $ \itttm{pd} \times {}$         &      \itttm{pd}, \itttm{pd}.      \\
    \verb|\itttp{pd}|    &         $(\itttp{pd})$          &         $ \itttp{pd} \times {}$         &   \dont \itttp{pd}, \itttp{pd}.   \\
    \verb|\itttp{pd}[.]| &               ---               &                   ---                   &    \itttp{pd}[,] \itttp{pd}[.]    \\ \hline
    \verb|\ttit{pd}|     & \multirow{2}{*}{$(\ttit{pd} )$} & \multirow{2}{*}{$ \ttit{pd} \times {}$} &       \ttit{pd}, \ttit{pd}.       \\
    \verb|\ttitc{pd}|    &                                 &                                         &      \ttitc{pd}, \ttitc{pd}.      \\ \hline
    \verb|\entity{pd}|   &         $(\entity{pd})$         &        $ \entity{pd} \times {}$         & \dont   \entity{pd}, \entity{pd}. \\
    \verb|\entity*{pd}|  &               ---               &                   ---                   &    \entity*{pd}, \entity*{pd}.    \\
\end{tabular}


\end{document}

Output

Output table