# How to index without tagging every entry

Is there a way to index a long document without having to find every entry in text? I am using \makeindex which requires the insertion of \index{xyz} after every entry in text that I want indexed.

• A quick and dirty method is to wrap the text into a command, which both prints your text and the makes an entry to the index: \newcommand{\myindex}[1]{#1\index{#1}}, e.g. \myindex{Einstein} will print Einstein right there and generates the index entry Einstein too. However, this is a cheap 'hack' and will not work for more sophisticated index entries ;-) – user31729 Jul 13 '14 at 15:15
• And please post a MWE such that users can provide better solutions – user31729 Jul 13 '14 at 15:17
• It should be possible to automatize this with a script (in python / perl / ...) without too much effort. However a good index uses page ranges and marks definitions. Besides there are cases in which something is mentioned incidentally so that an index entry is dispensable. So it is a trade-off between convenience and quality. – Rüdiger Voigt Jul 13 '14 at 15:20
• If the document is already written a script (in perl or awk or ...) could implement @ChristianHupfer 's suggestion. – Ethan Bolker Jul 13 '14 at 15:20
• You should not add every occurrence of terms to the index. That is a great way to create an unhelpful index for the reader. Suppose a common term occurs on 10% of pages. That's 40 pages listed for the word in the index. It is unlikely they are all helpful and it is especially likely they are all helpful for the same questions. Either many fewer pages should be listed or the entry should be subdivided by topic. – cfr Jul 13 '14 at 23:55

This is a quick method to insert text and a index entry simultaneously.

If the index entry is more complicated, one can use the optional argument of the command to provide a special index entry.

I called the command \ShowTextIndex, but this is too long, I know. If someone knows a better term, please leave a comment ;-)

I could also have simplified the non-optional argument version of the command, but I have some idea in mind, which would the whole editing useless, so I kept basically as a repetition of the optional argument version.

\documentclass[paper=a4,12pt]{book}

\usepackage{xcolor}%
\usepackage{imakeidx}%

\makeatletter
\newcommand{\ShowTextIndex@opt}[2][]{%
#2\index{\unexpanded{#1}}%
}%

\newcommand{\ShowTextIndex@noopt}[1]{%
#1\index{\unexpanded{#1}}%
}

\newcommand{\ShowTextIndex}{%
\@ifnextchar[{\ShowTextIndex@opt}{\ShowTextIndex@noopt}
}%
\makeatother

\makeindex

\begin{document}
This is a text about \ShowTextIndex{\textcolor{blue}{Einstein}} and his \ShowTextIndex[Theory!of relativy]{theory}

\printindex
\end{document}


I can provide a screen shot later on, if requested, but it shows the expected output.

More sophisticated techniques would use a keyvalue interface, in my point of view, but I do not have enough time right now.

• Would it index every further mention of "Einstein" in the index? My point is to economize by not having to put in bracket every occurence of, say, "Einstein" (my book is 400 pages) – Nero Jul 13 '14 at 16:37
• No, it would not, in this case you have to use a command, say \Einstein, this would be logical markup. However, this could be difficult, because you would have occurences of, say Einstein's , Einsteins etc, i.e. the grammar/semantics is important. – user31729 Jul 13 '14 at 16:41
• This reminds me of my idxcmds package :) – cgnieder Jul 13 '14 at 16:46
• regarding optional arguments for index commands, please note that the form \index[xxx]{yyy} is used by imakeidx to support multiple indexes (identified here by xxx). it would be too bad if that facility were suppressed, since it's a very useful feature. – barbara beeton Jul 13 '14 at 18:40
• @barbarabeeton: Yes, you are right, I should either restrict to 'traditional' makeidx package or update the code. I think, the latter would be better, the first easier. – user31729 Jul 13 '14 at 19:12