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.

When I indicate an index entry using \index I have to type the text of the indexed word twice, in context and as a parameter of \index:

 This is an index: Hello! \index{Hello} And ....

And when I decide to change the indexed word I have to make the change for two words. How can I force TeX to output the parameter of \index ? (Perhaps \index*!)

share|improve this question

1 Answer 1

up vote 12 down vote accepted

The index package provides the starred version \index* that both typesets its argument and adds it to the index; there's also the hvindex package and its \Index command.

\documentclass{article}

\usepackage{index}
\makeindex

\begin{document}

Some text about \index*{foo}.

\printindex

\end{document}

\documentclass{article}

\usepackage{makeidx}
\makeindex

\usepackage{hvindex}

\begin{document}

Some text about \Index{foo}.

\printindex

\end{document}

And if you want to do without additional packages, you could save the original version of \index and include it in a redefinition that does what you want. (The following example assumes that \index doesn't feature an optional argument.)

\documentclass{article}

\usepackage{makeidx}
\makeindex

\let\oldindex\index
\renewcommand*{\index}[1]{#1\oldindex{#1}}

\begin{document}

Some text about \index{foo}.

\printindex

\end{document}

EDIT: I seem to have spotted a bug in hvindex: A spurious space is added after \Index & friends, apparently because comment signs are missing at the end of various lines in the source code. The following patch corrects this behaviour:

\documentclass{article}

\usepackage{makeidx}
\makeindex

\usepackage{hvindex}

% The following must NOT be enclosed in `\makeatletter`/`\makeatother`
\def\IndexXXi#1@#2@#3\IndexNIL{%
  \ifx\relax#2\relax %              no @ in last arg
    \def\hvTemp{#1}%
    \ifx\hvTemp\hvEncap\index{{#1}}#1\else% {{...}} to get "| with hyperref
      \ifx\hvIDXfont\hvIDXfontDefault\index{#1}#1% <-- THIS COMMENT SIGN WAS MISSING
      \else\index{#1@\hvIDXfont{#1}}\hvIDXfont{#1}\fi\fi%#1
  \else\index{#1@#2}#2\fi}%     % #1@#2
%
\def\IndexXXii#1!#2@#3@#4\IndexNIL{%
  \ifx\relax#3\relax        %       no @ in last arg
    \def\hvTemp{#2}%
    \ifx\hvTemp\hvEncap\index{#1!{#2}}#2\else
      \ifx\hvIDXfont\hvIDXfontDefault\index{#2}#2% <-- THIS ONE, TOO
      \else\index{#1!#2@\hvIDXfont{#2}}\hvIDXfont{#2}\fi\fi%
  \else\index{#1!\protect#2@#3}#3\fi}
%
\def\IndexXXiii#1!#2!#3@#4@#5\IndexNIL{%
  \ifx\relax#4\relax        %       no @ in last arg
    \def\hvTemp{#3}%
    \ifx\hvTemp\hvEncap\index{#1!#2!{#3}}#3\else
      \ifx\hvIDXfont\hvIDXfontDefault\index{#3}#3% <-- AND THIS ONE, TOO
      \else\index{#1!#2!#3@\hvIDXfont{#3}}\hvIDXfont{#3}\fi\fi%
  \else\index{#1!#2!\protect#3@#4}#4\fi}

\begin{document}

Some text about \Index{foo}.

\printindex

\end{document}
share|improve this answer
    
@ Thanks, Which is better any why? –  PHPst May 5 '12 at 16:45
    
@Reza That depends. :-) If you're using a non-standard document class, the index package may be problematic. Simply redefining \index yourself may fail if you have multiple indexes (and an \index command with optional argument). –  lockstep May 5 '12 at 16:50
    
How can I change the title of index section produced by index package. (I want translate "Index" word to Persian ) –  PHPst May 6 '12 at 14:34
    
@Reza See tex.stackexchange.com/questions/19268/… –  lockstep May 6 '12 at 14:44
    
That solution do not work with \index package –  PHPst May 6 '12 at 15:57

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.