Understanding \@firstofone

You need to use \makeatletter for this to work, but it's the least.

The call \ex{111}{221} expands first to

\f 111 - \expandafter\f\@firstofone{221}

Now \f is expanded and it looks for three arguments, finding 1, 1 and 1, because TeX uses a single token if a brace is missing.

By the definition of \f this is expanded and the replacement text is reinserted in the input stream:

1-1-1 - \expandafter\f\@firstofone{221}

The tokens 1-1-1 - are not expandable and so they are sent to the next stage. Now it remains

\expandafter\f\@firstofone{221}

This will set \f aside and expand \@firstofone which just returns its (unbraced argument), so we get

\f221

that becomes 2-2-1. Thus the net effect of your code is to print

1-1-1 - 2-2-1

which doesn't seem really useful. Wherever you found it, don't trust it.


As egreg pointed out in his answer the use of \@firstofone isn't very useful and doesn't make a difference in this particular case. However, there are cases where it is useful. For completeness I'd like to give two examples.

Expandable macros

In Why the \expandafter\@firstoftwo idiom? a typical usecase for \@firstoftwo and \@secondoftwo in fully expandable macros is given. \@firstofone, kind of LaTeX's "identity function", is useful in similar contexts where only a single parameter should be handled:

\def\gobbleIfZero#1{%
  \ifnum#1=0
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
}

foo\gobbleIfZero{0}{bar}

foo\gobbleIfZero{123}{bar}

will give

foo

foobar

\@gobble (like \@firstofone) takes a single parameter but discards it and expands to "nothing".

Fixing of catcodes

A bit more tricky but also more interesting is the case where catcode changes are involved. When TeX is parsing arguments for macros, it fixes catcodes whenever the argument text is being read. This doesn't matter in your case as there are no catcodes changes here. But suppose following, somewhat contrived example is given:

\begingroup
\catcode`4=\active
\gdef 4{four}
\endgroup

\newcommand*\f{\catcode`4=\active}
\newcommand*\exA{\f}
\newcommand*\exB{\expandafter\f\@firstofone}

{\exA{345}--{345}}

{\exB{345}--{345}}

This results in

3four5-3four5

345-3four5

The first four lines define a global macro 4 which expands to four. Usually 4 has catcode 12 (other), so we change it to 13 (active) to be able to make a macro out of it. \f here brings this macros back into scope when executed.

When \exA is executed, the catcode of 4 is changed to 13 which afterwards makes 4 being expanded to four in both of following two groups. There are no parameters here at all, so this is straightforward.

On the other hand, in the \exB macro the first {345} part is processed by \@firstofone which just returns the same text, but fixes the catcode of 4 to 12. Once it is fixed, it can't be changed anymore! Even though the catcode change from \f is processed before the first 345, it won't have any effect on it. As the catcodes of the second {345} haven't been fixed yet, the changes will still apply to this group.

This behaviour comes in quite handy if you want to inject extra code into existings macros that deal with catcode changes, e.g. \verb (inspired by this TeX pearl):

\begingroup
\catcode`\/\active
\catcode`\_\active
\@firstofone{%
  \verb|%
  \catcode`\/\active \def/{\par}%
  \catcode`\_\active \def_#1_{\textcolor{blue}{#1}}%
}_\documentclass_{article}/_\begin_{document}/  We all love _\LaTeX_!/_\end_{document}|
\endgroup

which outputs

output

I don't know where you read about \@firstofone in the first place, but it might have been in one of these contexts.