5

I am writing a small book and try to build up an index reference. To make it a little easier I wrote a small script that enables my to type a word and also add it to the index:

\newcommand{\toindex}[1]{\index{#1}\indexlayout{#1}}

However this results in words added to the index that don't start with a capital letter. This makes the index looks a little bit dirty. Is there a way to generate a string out of parameter #1 that switches the first character to its uppercase counterpart and leave the rest of the string the same?

doncherry
  • 54,637

1 Answers1

5

I would define two commands (actually one command with a *-form):

\newcommand\toindex{\@ifstar{\@dblarg{\@toindexs}}{\@toindex}}
\def\@toindexs[#1]#2{\indexlayout{#2}\index{#1@#2}}
\newcommand\@toindex[2][]{%
  \if\relax\detokenize{#1}\relax
    \indexlayout{#2}%
    \begingroup
    \@splitword#2\@nil%
    \uppercase\expandafter{%
      \expandafter\def\expandafter\@initial\expandafter{\@first}}%
    \toks0=\expandafter{\@initial}%
    \toks2=\expandafter{\@rest}%
    \edef\x{\endgroup\noexpand\index{\the\toks0 \the\toks2 }}\x
  \else
    \indexlayout{#2}\index{#1}
  \fi
}
\def\@splitword#1#2\@nil{\def\@first{#1}\def\@rest{#2}}
\makeatletter

For entries such as $n$-entry you would use \toindex*[nentry]{$n$-entry}, for word you can use \toindex{word}. The optional argument can be used in either case.

For \toindex* the optional argument is used for collation; it's actually more than optional: makeindex would sort \index{$n$-entry} among symbols.

For \toindex the optional argument can be used for the cases when the word to be indexed is special, for instance

\toindex[equipe@\'Equipe]{\'equipe}

However, I don't see the necessity to capitalize words in the index. Look at the TeXbook, for an example. :)

egreg
  • 1,121,712
  • Well it's not that much the fact that an index starts with a capital letter, but more the fact that when you use an item twice (with lowercase and uppercase), two items are added to the index... – willeM_ Van Onsem Jan 27 '14 at 04:45