Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Have you read this: Command to uppercase the first letter of each word in a sentence? –  Werner Sep 27 '11 at 18:39
    
Thanks! Nearly the same, but it looks all words get an uppercase character, I will try to convert the script that only the first character gets an uppercase. –  CommuSoft Sep 27 '11 at 18:49
    
furthermore something goes wrong when this script tries to manipulate strings with mathematics inside (I expect the $-signs generate some problems) like $n$-army problem. –  CommuSoft Sep 27 '11 at 19:01
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
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... –  CommuSoft Jan 27 at 4:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.