In my code this works fine:

\textsuperscript{\titlecap{some text}} 

But this doesn't compile:

\titlecap{\textsuperscript{some text}}

Why does the order of the tags matter here?


\titlecap has to inspect its contents character by character. It includes special provisioning for some formatting commands, in particular, the base LaTeX font selection commands (alongside its own \textnc command which designates text that should not be changed by \titlecap.

If you type texdoc titlecaps at a command line on your computer, you'll pull up the documentation on the titlecaps package which will talk about the package's limitations and how it works (the complete code listing concludes the documentation for those who care to investigate the gory details and see how one might add \textsuperscript into the allowed formatting commands (which is an admittedly non-trivial task given the structure of the package).

  • Thanks both for those explanations. So order matters here. It would be interesting to know if there are any other command pairs that are sensitive to their order. – dedalus May 7 at 6:56

Since Don just beat me at my own package (titlecaps), I'll have to reach deep into my bag of tricks and pull out a tokcycle approach that works with both syntax forms.

As it is currently set up, a future-capitalization of the next catcode-11 token will be triggered when any of the following is encountered in the input stream:

  1. The initial entry into the routine

  2. a catcode-10 space token

  3. a hyphen -

  4. upon entry into a group {...}

Obviously, additional tests could be added. Here is the MWE:

  \def\capnext{T}% <- INITIAL ENTRY
    {\def\capnext{T}\processtoks{##1}}% <- GROUP ENTRY
    {\addcytoks{##1}\def\capnext{T}}% <-CAT-10 SPACE
       \tctestifx{-#1}{\def\capnext{T}}{}% <- TEST FOR HYPHEN
       \tctestifcon{\if T\capnext}%
         {\tctestifcon{\ifcat\tmp A}% <- TEST FOR NEXT CAT-11
This\textsuperscript{\TitleCase{some text}}. 

versus\TitleCase{\textsuperscript{some text}}.

|here, {\bfseries\today{}, is [my]}
  (\textit{capitalizing} words). abc\textsuperscript{def}|

 here, {\bfseries\today{}, is [my]} 
  (\textit{capitalizing} words). abc\textsuperscript{def}}

enter image description here

EDITED to achieve better kerning, by performing the conditional-testing prior to the typesetting, rather than in the midst of it. Thus, by the time typesetting occurs, this is what is actually typeset, for the last block (seen with \detokenize\expandafter{\the\cytoks}):

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.