What does the command \@dblarg do in a class?
It essentially checks if the token after \CJ@title
is [
or not, in order to pass to the main command the same argument twice, if [
is not found. The command \CJ@title
must be defined by
\def\CJ@title[#1]#2{...}
so that, with a call such as
\title{xyz}
the expansions will be (successively}
\@dblarg\CJ@title{xyz}
...<some complex action>...
\CJ@title[{xyz}]{xyz}
and with a call such as
\title[abc]{xyz}
the expansions will be (successively}
\@dblarg\CJ@title[abc]{xyz}
...<some complex action>...
\CJ@title[abc]{xyz}
One can follow ...<some complex action>...
by looking at the definition of \@dblarg
:
\long\def\@dblarg#1{\kernel@ifnextchar[{#1}{\@xdblarg{#1}}}
\long\def\@xdblarg#1#2{#1[{#2}]{#2}}
So with \title{xyz}
one has (one step on each line, I omit the expansions of \kernel@ifnextchar
that are irrelevant here)
\@dblarg\CJ@title{xyz}
\kernel@ifnextchar[{\CJ@title}{\@xdblarg{\CJ@title}}{xyz}
\@xdblarg{\CJ@title}{xyz}
\CJ@title[{xyz}]{xyz}
With \title[abc]{xyz}
one has
\@dblarg\CJ@title{xyz}
\kernel@ifnextchar[{\CJ@title}{\@xdblarg{\CJ@title}}[abc]{xyz}
\CJ@title[abc]{xyz}
There's an extra pair of braces in the first case, but it will be removed by rule of TeX; it’s needed in case the title contains a ]
.
How to do the same with xparse
With the xparse
package one can obtain the same functionality with a clearer syntax (I assume this is done in a class or package file)
\RequirePackage{xparse}
\NewDocumentCommand{\title}{om}{%
\IfNoValueTF{#1}
{\CJ@title{#2}{#2}}
{\CJ@title{#1}{#2}}%
}
\newcommand{\CJ@title}[2]{...}
So no delimited argument is necessary.
With a recent version of xparse
one can do even better:
\RequirePackage{xparse}
\NewDocumentCommand{\title}{O{#2}m}{%
\CJ@title{#1}{#2}%
}
\newcommand{\CJ@title}[2]{...}
because with this code the value supplied for a missing optional argument is the same as the mandatory argument.
This is an easy way of allowing optional arguments to some other function/macro.
For example, using your case of \def\title{\@dblarg\CJ@title}
, you can either use
\title{some title}
without an optional argument, or use
\title[some optional title]{some title}
with an optional argument. "Easy" here refers to the fact that if you don't specify the optional component (like in the former example), it defaults to
\title[some title]{some title}
thereby converting single-argument responses into double-arguments. The macro \CJ@title
is appropriately defined to manage both arguments and could have the form:
\def\CJ@title[#1]#2{%
%...
}
This is typically the case with sectional commands that have an optional argument: \section[<ToC entry>]{<body entry>}
. If you don't specify the optional argument, the <body entry>
still makes its way into the ToC. How? \@dblarg
.
Here are the formal definitions contained within latex.ltx
- you'll find its use inside \@startsection
, just as an example:
\long\def\@dblarg#1{\kernel@ifnextchar[{#1}{\@xdblarg{#1}}}
\long\def\@xdblarg#1#2{#1[{#2}]{#2}}