[Tex/LaTex] Command to uppercase the first letter of each word in a sentence

capitalizationindexingmacros

I'm writing a textboox in latex, and I have things like

\textit{some definition}\index{Some Definition}

all over the place. I'd like to combine them into something like

\define{some definition}

and have it expand automagically. So I can have

\newcommand{\define}[2]{\textit{#1}\index{#2}}

which works, but forces me to include both arguments. The second argument is almost always the first argument with the first letter of each word uppercased. How can I write a command which uppercases the first character of each word?

Suggestions of better indexing best practices would also be appreciated.

Best Answer

\documentclass{article}
\usepackage{makeidx}\makeindex
\newcommand*{\formatfirst}[1]{\MakeUppercase{#1}}
\makeatletter
\newcommand*{\mymacro}[1]{%
  \expandafter\formatfirst\expandafter{\@car #1\@empty\@nil}%
  \@cdr #1\@empty\@nil}
\newcommand*\myMakeUpperCase[1]{%
  \def\@myuppercasewords{\myuppercase@i#1 \@nil}%
    {\itshape\@myuppercasewords}\index{#1@\@myuppercasewords}}
\def\myuppercase@i#1 #2\@nil{%
  \mymacro{#1}%
  \ifx\\#2\\%
  \else
    \@ReturnAfterFi{%
      \space
      \myuppercase@i#2\@nil
    }%
  \fi} 
\long\def\@ReturnAfterFi#1\fi{\fi#1} 
\makeatother

\begin{document}
foo
\myMakeUpperCase{capital letter} bar
\myMakeUpperCase{Next one} baz
\myMakeUpperCase{two words}

\printindex

\end{document} 

alt text

Related Question