[Tex/LaTex] Automatically capitalize first letters of words in titles

capitalization

Is there a way to tell LaTeX to automatically capitalize the first letters of words in chapters and sections?

I imagine something that checks a dictionary for words like for, of, the, in … and leaves them small (if it's not the first word).

I couldn't find anything on the internet but I think it would be useful, especially for switching between one of the many conventions of capitalization of titles in English to another one.

Best Answer

Hows this:

\documentclass{article}

\usepackage{ltxcmds} % get commands: \ltx@empty, \ltx@carzero

\catcode`\@=11 % allow @ to be apart of control sequences

\def\@aaa#1 {\@capital#1 \@bbb}  % Grap word delimited by space and capitalise first char
                                 % (used for first word)

\def\@bbb#1 {\@Capital{#1} \@bbb}% Grap word delimited by space and capitalise first char
                                 % provided its not exempt (used for subsequent words).

\def\:#1\@bbb{}                  % used so we can get a space token at the end of a control sequence,
                                 % and to gobble up all the junk between \: and \bbb inclusive

\def\@capital#1{\uppercase{#1}}  % Capitalises one character.

\def\@Capital#1{%                  Checks whether #1 is exempt and capitalise if not
  \def\@stripcolon##1\:##2{\def\@word{##1}}%
  \@stripcolon #1\:\ltx@empty%     Make sure that #1 does not have \: appended to end                
  \def\@check##1,{%
    \def\@token{##1}%
    \ifx\@token\ltx@empty%         if no more tokens in exception list
      \@capital#1%
      \let\@result\ltx@carzero%    delete everything up to and including \@nil
    \else%
      \ifx\@token\@word%           if in exception list
        #1%
        \let\@result\ltx@carzero% 
      \else%
        \let\@result\@check%       check next token in exception list.
      \fi%
    \fi%
    \@result%
  }%
  \expandafter\@check\@exempt%
}%



\newcommand{\smartuppercase}[1]{\@aaa#1\: }
\newcommand{\exempt}[1]{\def\@exempt{#1,,\@nil}}

\catcode`\@=12 % disallow @ to be apart of control sequences

\begin{document}

\exempt{the,a,of} %comma separated list of words not to capitalise - no spaces allowed

\smartuppercase{hello people of earth}

\smartuppercase{the people of the earth}

\end{document}

This is something that I wrote these past two days. It seems to do the job. I sure that it will fail if you start hyphenating words, using quotation marks, etc. I'm am sure it can be cleaned up further, but I leave that for someone else.