Here is an interesting case. Please why is case 1 failing?
\def\expandsecond#1#2{\begingroup\edef\x{\endgroup\unexpanded{#1}#2}\x}
\def\maybe@ic@{%
\ifdim\fontdimen\@ne\font>\z@\else
\maybe@ictrue
% Case 1:
\expandsecond{\@tfor\reserved@a:=}\nocorrlist\do{\t@st@ic}%
% Case 2 (original):
%\expandafter\@tfor\expandafter\reserved@a\expandafter:%
% \expandafter=\nocorrlist\do{\t@st@ic}%
\ifmaybe@ic\sw@slant\fi
\fi
}
\def\t@st@ic{%
\expandafter\let\expandafter\reserved@b\expandafter=\reserved@a\relax
\ifx\reserved@b\@let@token
\maybe@icfalse
\@break@tfor
\fi
}
The error came from
\textit{foof}
Error:
! Incomplete \iffalse; all text was ignored after line 163.
<inserted text>
\fi
<*> italic-correction.tex
I get the same amusing problem with the following modifications:
\def\newtfor#1:={\new@tfor#1 }
\long\def\new@tfor#1#2\do#3{%
\edef\newfortmp{\unexpanded{#2}}%
\ifx\newfortmp\space\expandafter\@gobble\else\expandafter\@iden\fi
{\newtforloop#2\@nil\@nil\newtfor#1{#3}}%
}
\long\def\newtforloop#1#2\newtfor#3#4{%
\edef#3{\unexpanded{#1}}%
\ifx#3\@nnil\expandafter\@gobble\else\expandafter\@iden\fi
{#4\relax\newtforloop#2\newtfor#3{#4}}%
}
\long\def\breaknewtfor#1\newtfor#2#3{}
\def\maybe@ic@{%
\ifdim\fontdimen\@ne\font>\z@\else
\maybe@ictrue
% Case 1:
%\expandsecond{\newtfor\reserved@a:=}\nocorrlist\do{\t@st@ic}%
% Case 2:
\expandafter\newtfor\expandafter\reserved@a\expandafter
:\expandafter=\nocorrlist\do{\t@st@ic}%
\ifmaybe@ic\sw@slant\fi
\fi
}
\def\t@st@ic{%
\expandafter\let\expandafter\reserved@b\expandafter=\reserved@a\relax
\ifx\reserved@b\@let@token
\maybe@icfalse
\expandafter\breaknewtfor
\fi
}
EDIT (2012/08/13)
Here is a M(N)WE:
\documentclass{article}
\usepackage{xspace}
\edef\x{catcode of comma=\the\catcode`\,\space,
\space catcode of stop=\the\catcode`\.}
%\show\x
%\show\nocorrlist
\makeatletter
\def\expandsecond#1#2{%
\begingroup\edef\x{\endgroup\unexpanded{#1}#2}\x
}
%\def\nocorrlist{.,\xspace} % source of the problem.
\def\maybe@ic{\futurelet\@let@token\maybe@ic@}
\def\maybe@ic@{%
\ifdim\fontdimen\@ne\font>\z@\else
\maybe@ictrue
% Case 1:
\expandsecond{\@tfor\reserved@a:=}\nocorrlist\do{\t@st@ic}%
% Case 2:
%\expandafter\@tfor\expandafter\reserved@a\expandafter
% :\expandafter=\nocorrlist\do{\t@st@ic}%
\ifmaybe@ic\sw@slant\fi
\fi
}
\makeatother
\begin{document}
\textit{foof}. \textit{foof}!
\end{document}
The problem was caused by one of the packages I loaded: it did \def\nocorrlist{.,\xspace}.
\nocorrlistsince the main difference is that you fully expand that with edef rather than just once with expandafter – David Carlisle Aug 13 '12 at 08:45