1

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?

4
  • Are you aware that a second use of \subtitle adds another line and the use of \title will overwrite any previous subtitles? Oct 11, 2013 at 2:16
  • @Qrrbrbirlbel I am, but thanks for pointing it out! Oct 11, 2013 at 2:19
  • Whatever your final decision for code is, please place it as argument to \AtBeginDocument; you'll know why when one of the users of your class will be asking you about the disparition of a subtitle (you'll certainly find somebody putting \subtitle before \title). Wouldn't a syntax such as \title[Subtitle]{Title} be better?
    – egreg
    Oct 11, 2013 at 9:36
  • @egreg Very good point about users possibly having \subtitle before \title; the optional argument syntax is much nicer, actually. I didn't put too much thought into it, as this document class is only for a team of around five people. Oct 12, 2013 at 3:50

1 Answer 1

3

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}
7
  • I'd really like to avoid the use of \expandafter; this is expl3 after all. I didn't think about the issues with \\/\large though, I was focussed on \@title when I was writing my macro. \\/\large are definitely the source of the problem, but how might I resolve this without \expandafter? Oct 11, 2013 at 2:18
  • Also, my apologies about @; I moved this into a test file where it had been in a class file. Thanks for pointing it out, though! Oct 11, 2013 at 2:19
  • @SeanAllred Is \noexpand allowed? I’m no 3 expert so I wouldn’t use it in the first place. ;) (You need two ` for \\ in comments.) Oct 11, 2013 at 2:19
  • 3
    Indeed, \@title is clearly equivalent to an expl3 tl rather than a function, so really \tl_put_... is the right choice. Of course, you should really only be storing the text and dealing with the design by altering \@maketitle, but that's another question!
    – Joseph Wright
    Oct 11, 2013 at 6:16
  • 1
    Besides \@title being a variable -- the \expandafter's could be replaced with an o type argument: \cs_set:Npo \@title { \@title \\[.5ex] \large #1 }
    – cgnieder
    Oct 11, 2013 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .