9

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*!)

1 Answer 1

14

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}
8
  • @ Thanks, Which is better any why? May 5, 2012 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, 2012 at 16:50
  • How can I change the title of index section produced by index package. (I want translate "Index" word to Persian ) May 6, 2012 at 14:34
  • @Reza See tex.stackexchange.com/questions/19268/…
    – lockstep
    May 6, 2012 at 14:44
  • That solution do not work with \index package May 6, 2012 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .