In the early days of this site we had a question Automatically capitalize first letters of words in titles? with one solution. Since expl3 has matured well since then I have worked out a partial solution using expl3. It fails on a couple of exceptions and I am looking at ways to improve it.
\documentclass{article}
\usepackage{expl3,xparse}
\usepackage{hyperref}
\ExplSyntaxOn
\clist_gset:Nn \title_words_not_capitalized_en
{ a, an, the, at, by, for, in, of, on, to, up, and, as, but, it, or, nor, do, for, this, be,
A, An, The, At, By, For, In, Of, On, To, Up, And, As, But, It, Or, Nor, Do, For, This, Be }
\cs_new:Npn \ucfirst_aux:w #1#2 \q_stop { \uppercase { #1 } #2 }
\cs_new:Npn \ucfirst #1 {
\exp_after:wN \ucfirst_aux:w #1 \q_stop
}
\cs_new:Npn \lowerfirst #1 {
\tex_lowercase:D {#1}
}
\DeclareDocumentCommand\UppercaseTitle {s +m }
{
\IfBooleanTF { #1 } { {\bfseries {#2} } }
{
\seq_set_split:Nnn \g_tmpa_seq {~} {#2}
\seq_use:Nn \g_tmpa_seq {~}\\
\seq_pop_left:NN \g_tmpa_seq \l_tmpa_tl
{\bfseries\ucfirst \l_tmpa_tl \space}
\seq_map_inline:Nn \g_tmpa_seq
{
\clist_if_in:NnTF \title_words_not_capitalized_en { ##1 }
{ {\bfseries \lowerfirst {##1}~}} { {\bfseries \ucfirst{##1}~ } }
}
}
}
\ExplSyntaxOff
\begin{document}
\tableofcontents
\parindent0pt
\section{\UppercaseTitle {The l3seq package Sequence and stacks}}
\UppercaseTitle {Top ten things to do in Paris}\\
\UppercaseTitle {How to use {\LaTeX} sequence lists effectively}\\
\UppercaseTitle {Senate Votes to Confirm Elena Kagan For U.S. Supreme Court}\\
\UppercaseTitle {what would be a ``correct'' capitalization for the title of this question?}\\
\UppercaseTitle* {How about {$e=mc^2$}? }\\
\end{document}
Exceptions are:
- fails to capitalize words in quotes.
- Has issues with maths
- Prefer not to bracket macros (not user friendly)
- Would fail on two or more sentence titles where the first word of the second sentence starts with an exception word.
- Exceptions such as "p-th root" of a "p-adic".
Comments on LaTeX3 coding are welcome. Still a rookie!
\tl_upper_case:n, etc.)? It's intended for the lowest-level part of case changing and deals happily with for example math mode (though I need to do more revision). Will Robertson suggested on I think LaTeX-L an implementation of 'sentence case' build on top of it. – Joseph Wright May 20 '15 at 21:23expl3uses is where it emulates some pdfTeX primitives. The idea is that we should get the same behaviour on pdfTeX/XeTeX/LuaTeX as far as possible, so doing a Lua path just makes life more complex. Of course, there is the whole business about things that cannot be done in the same way, but at the moment we've not really tackled many of them. Note in this context that XeTeX and LuaTeX should work in the same way, but we accept pdfTeX has to be more restricted! – Joseph Wright May 21 '15 at 07:22