[Tex/LaTex] Defining a `\subtitle` macro – `\reserved@e` undefined

expansionexpl3

I want to define a general-ish \subtitle macro that simply adds to the \@title macro set by the user-level \title. Here is what I've got:

\documentclass{article}
\RequirePackage{xparse,expl3}
\makeatletter\ExplSyntaxOn
\NewDocumentCommand \subtitle { m }
  {
    \cs_set:Npx \@title
      {\@title\\[.5ex]\large#1}
  }
\ExplSyntaxOff\makeatother

\title{A New Chapter}
\subtitle{In This Short Book}

\begin{document}
\maketitle
Blah
\end{document}

This is supposed to define a document command \subtitle that takes one mandatory argument.
It then fully expands the current value of \@title and \\[.5ex]\large#1, setting this expanded value to \@title, effectively appending the subtitle to it.
Unfortunately, this raises the error

ERROR: Undefined control sequence.

--- TeX said ---
\\  ->\let \reserved@e 
                       \relax \let \reserved@f \relax \@ifstar {\let \reserv...
l.12 \subtitle{In This Short Book}

--- HELP ---
TeX encountered an unknown command name. You probably misspelled ...

Why am I getting this error and, more importantly, how might I fix it?

Best Answer

The x stands for “exhaustive expansion” (please see the manual, p. 2 or 13 for that). This seems like a bad idea with \\ and \large.

I’d simply expand the \@title once and append the rest:

\expandafter \cs_set:Npn \expandafter \@title \expandafter
   { \@title \\[.5ex] \large #1 }

The \tl_put_right:** (or \appto from etoolbox) macros do this without \expandafters.

I’m using :cn { @title } here but obviously you can just use :Nn \@title with correct catcodes (i.e. in a package).

Code

\documentclass{article}
\RequirePackage{xparse,expl3}
\ExplSyntaxOn
\NewDocumentCommand \subtitle { m }{
  \tl_put_right:cn { @title } % or \tl_put_right:Nn \@title with \makeatletter
    { \\[.5ex] \large #1 }
}
\ExplSyntaxOff
\title{A New Chapter}
\subtitle{In This Short Book}
\subtitle{And another line?}

\begin{document}
\maketitle
Blah
\end{document}
Related Question