# Too much space when mixing typewriter upright and italics

There seems to be too much horizontal whitespace following italic typewriter text. Consider this example:

\documentclass{article}

\begin{document}
\noindent
\texttt{This is a perfectly \textit{fine example} of text\newline
Immediately succeeded by another line of similar text.}
\end{document}


This produces two lines of text. The letters at the beginning of each line are in perfect vertical alignment, but the letters following the end of the italics are not. There's too much space after \textit{fine example}.

How can I avoid this extraneous space, so that I can use the monospace font to line up columns across multiple lines?

Update: Interestingly, the spacing is correct if an upright comma is added after the italics, \textit{fine example},, but not if the comma is italic, too: \textit{fine example,}.

• I don't see too much space. also, why you are using \newline? – CroCo Nov 23 '15 at 22:19
• @CroCo: I don't want justification, I just wanted two naturally-spaced lines. I can see the misalignment e.g. on latexfiddle.com. – Kerrek SB Nov 23 '15 at 22:21
• Of course not but my point is what do you expect the \newline does? – CroCo Nov 23 '15 at 22:27
• you can use the nonbreakable space ~ if you don't want extra space \textit{fine example}~of but in your case, I don't see why you need that. The space between strings is set automatically. – CroCo Nov 23 '15 at 22:39
• That's probably the italic correction. Try {\itshape fine example} – cgnieder Nov 23 '15 at 22:39

The \textit command adds the italic correction, which is usually desirable when in normal text: compare

\textit{full} size

{\itshape full} size


that gives

where the second line has too short space.

However, this is not desirable in a monospaced context, as you notice. Either use {\itshape fine example} or the following more complicated version.

\documentclass{article}
\usepackage{pdftexcmds}

\DeclareTextFontCommand{\latextextit}{\itshape}
\makeatletter
\DeclareRobustCommand{\textit}[1]{%
\ifnum\pdf@strcmp{\f@family}{\ttdefault}=\z@
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\latextextit{#1\nocorr}}%
{\latextextit{#1}}%
}
\makeatother

\begin{document}

\noindent\textit{full} size

\noindent{\itshape full} size

\noindent
\texttt{This is a perfectly \textit{fine example} of text\newline
Immediately succeeded by another line of similar text.}
\end{document}


• Every day something new: I didn't know \nocorr :) – cgnieder Nov 23 '15 at 22:57