Dealing with spaces in a general way is tricky. What you want here is a peek-ahead-and-remove function. This is available in LaTeX3 as \peek_meaning_remove:NTF, where I would use
\RequirePackage{expl3}
\ExplSyntaxOn
\cs_new_protected:Npn \newlet #1
{
\peek_meaning_remove:NTF =
{ \cs_new_eq:NN #1 }
{ \cs_new_eq:NN #1 }
}
\ExplSyntaxOff
\makeatletter
\newlet\sptoken=\@sptoken
This can of course be 'written out' in primitives. Ignoring the need to be careful about & tokens (using an Appendix D trick), something like
\makeatletter
\protected\long\def\newlet#1{%
\protected\def\@newlet@fcommon{%
\ifdefined#1%
\expandafter\ERROR
\fi
\let#1=
}%
\let\@tempa= =%
\futurelet\@let@token\@newlet
}
\protected\def\@newlet{%
\ifx\@let@token\@tempa
\expandafter\@newlet@true
\else
\expandafter\@newlet@fcommon
\fi
}
\protected\def\@newlet@true{%
\afterassignment\@newlet@fcommon
\let\@temp@= %
}
will work (it's a simplified version of \peek_meaning_remove:NTF with the branches hard-coded). Notice in particular that the = is removed using a \let along with an \afterassignment to regain control, rather than using a parameter.
The basic idea here is not dis-similar to \@ifnextchar, but does not include the loop to remove spaces which the latter does. The LaTeX3 bundle provides both 'pre-packaged' (the equivalent to \@ifnextchar is `\peek_meaning_remove_spaces:NTF).
\newletwhich will carry out a check and which works with implicit spaces, but the optional=is a pain. – Joseph Wright♦ Feb 28 '12 at 19:39\@ifnextchar. I really wanted to do\newlet\temp@sptoken\@sptokenand it failed. I then traced the problem to the fact that\@xifnchexpects an explicit blank space. In essence can\@ifnextcharbe modified to work in 'all' cases? – Ahmed Musa Feb 28 '12 at 19:51\new@ifnextchar, but then\newlet\x= \ywould not work. – egreg Feb 28 '12 at 20:44\new@ifnextcharin AMS packages, but as you have noted,\newlet\x= \yfails. – Ahmed Musa Feb 28 '12 at 21:38