2

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.

  • (tt stands for typwriter.) Why do use \textit in math mode? F(#1) would be typeset correct (and in italics) and uses the right spacing between F and (. – Qrrbrbirlbel Nov 07 '12 at 03:06
  • 1
    Just an additional hint: There is the relative new package newtx. You should consider to switch over, take at least a closer look on it. – Speravir Nov 07 '12 at 03:32

2 Answers2

4

If \texttt and \textit are exchanged, then the italics correction is inserted. Also in the example, \textit is only applied to F if not in math mode.

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

\newcommand*{\somefunction}[1]{%
  \ifmmode\else\expandafter\textit\fi F(#1)%
}
\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}
\newcommand*{\ttit}[1]{\texttt{\textit{#1}}}

\begin{document}

\(\somefunction{\ittt{pd}}\)

\(\somefunction{\ttit{pd}}\)

\(\ittt{pd}a\)

\(\ttit{pd}a\)

\end{document}

Result

Heiko Oberdiek
  • 271,626
2

\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

Moriambar
  • 11,466
Qrrbrbirlbel
  • 119,821
  • I don't quite see an effect of \@ifnextchar.{\kern-.1em}{\@ifnextchar,{\kern-.08em}{}}. 2. I like the content of your writing very much (and I understand it I think), but could you expand it a bit with prose?
  • – Lover of Structure Nov 07 '12 at 09:54
  • @user14996 I've updated (read: overhauled) my answer. Hopefully it is more understandable now. The effect of \@ifnextchar is visible when you compare \ttit and \ttitc. – Qrrbrbirlbel Nov 07 '12 at 20:25
  • Is there a way to have the correction of \ttitc work if the macro is within macro but the following punctuation outside? – Lover of Structure Nov 20 '12 at 18:24
  • @user14996 So that {\ttitc{pd}}. works as {\ttitc{pd}.}? I suppose this is doable, but I don't know how, though. Why can't you put the punctuation inside the outer macro? Anyway, I suggest asking a separate question as this isn't related to italic corrections. – Qrrbrbirlbel Nov 20 '12 at 19:25
  • I meant (maybe this is what you have in mind too) that I may write something like \([...] \ttitc{pd}\). and would like italic correction to apply nonetheless. – Lover of Structure Nov 20 '12 at 20:24