please make a full MWE then demonstrates the error case. I tried your first code block in a document and got no error
–
David CarlisleAug 13 '12 at 7:43

My guess would be that you have a active punctuation characters in your \nocorrlist since the main difference is that you fully expand that with edef rather than just once with expandafter
–
David CarlisleAug 13 '12 at 8:45

1 Answer
1

As David Carlisle has already said, do not fully expand \nocorrlist. LaTeX's code for
the italic correction looks for the next token using \futurelet (in macro \maybe@ic).
If the next token is an element of the list \nocorrlist, then the italic correction is suppressed. LaTeX's original definition only includes the comma and period as tokens
with catcode 12 (other). But the list can be extended, by other packages, by the user (there is no @ in the name). The extended tokens can also be macros, see package biblatex:

\appto\nocorrlist{\isdot\adddot\addperiod\addcomma}

I assume, the purpose of \expandsecond is to expand a token after the first argument to
provide the expanded list for \@tfor. It would behave similar to \expandafter with
the difference, that it jumps over a token list instead of a single token. And the inserted
group makes the macro form non-expandable. The following definition of \expandsecond expands the token once, not fully. And it does not need e-TeX's \unexpanded:

However, the definition remains non-expandable because of the macro definition.

In case of \newtfor I do not know, what you want to improve. The redefinition is probably a tiny bit less efficient because of the additional macro expansion steps (\@gobble,\@iden`). Both variants can handle unmatched conditional in the token list.

A remark for the lines

\edef\newfortmp{\unexpanded{#2}}%
\edef#3{\unexpanded{#1}}%

The point of \unexpanded is the prevention of the expansion, thus in the end
the result is (with the exception of #, see "Edit"):

\def\newfortmp{#2}%
\def#3{#1}%

as in the original definitions for the \@tfor loop in LaTeX.

Edit: There is one exception, pointed out by Joseph Wright. Tokens with catcode 6 (parameter), usually the hash char #, needs to be doubled without \edef + \unexpanded.

While I'm not sure if it's the aim, the \edef\foo{\unexpanded{<stuff>}} construct is not identical to \def\foo{<stuff>} if the <stuff> contains # tokens. The latter will give an error, the former is allowed. (This is how we construct token list variables in expl3.)
–
Joseph Wright♦Aug 13 '12 at 12:41

@JosephWright: That was the reason for using \unexpanded: to parse tokens with parameter characters. Eg, \newtfor\x:=a \ifx {\def\x#1{#1}} \fi\do{\edef\y{\detokenize\expandafter{\x}}}.
–
Ahmed MusaAug 13 '12 at 16:37