4

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?

3

\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).

1
  • 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
3

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:

\documentclass{article}
\usepackage{tokcycle}
\newcommand\TitleCase[1]{%
  \def\capnext{T}% <- INITIAL ENTRY
  \tokcycle
    {\nextcap{##1}}
    {\def\capnext{T}\processtoks{##1}}% <- GROUP ENTRY
    {\addcytoks{##1}}
    {\addcytoks{##1}\def\capnext{T}}% <-CAT-10 SPACE
    {#1}%
  \the\cytoks
}
\newcommand\nextcap[1]{%
       \edef\tmp{#1}%
       \tctestifx{-#1}{\def\capnext{T}}{}% <- TEST FOR HYPHEN
       \tctestifcon{\if T\capnext}%
         {\tctestifcon{\ifcat\tmp A}% <- TEST FOR NEXT CAT-11
           {\addcytoks{\uppercase{#1}}\def\capnext{F}}%
           {\addcytoks{#1}}}%
         {\addcytoks{#1}}%
}
\begin{document}
This\textsuperscript{\TitleCase{some text}}. 

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

\TitleCase{%
|here, {\bfseries\today{}, is [my]}
  really-big-test  
  (\textit{capitalizing} words). abc\textsuperscript{def}|

 here, {\bfseries\today{}, is [my]} 
  really-big-test 
  (\textit{capitalizing} words). abc\textsuperscript{def}}
\end{document}

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.