I've just recently completed my undergraduate mathematics degree, and have been compiling all of my lecture notes into one document for future reference. The document is long, and since it is based on previous lecture notes, \index entries have not been added. However, the majority of important terms have been highlighted with \textbf. Is there any way (either a package or a macro) that would either

  1. Comb through the file, and add \index{*} for any instance of the term \textbf{*}
  2. Change the index to recognize items enclosed in \textbf{}

Thank you for any help or assistance!

  • 2
    Search and replace \textbf{ with \index{, however, this will do so for any occurence
    – user31729
    Commented Oct 12, 2015 at 19:44
  • 4
    try \renewcommand{\textbf}[1]{{\bfseries #1}\index{#1}}
    – touhami
    Commented Oct 12, 2015 at 19:44
  • @touhami: Wouldn't this index any content that has been been put into \textbf{...} then?
    – user31729
    Commented Oct 12, 2015 at 20:34
  • 1
    @ChristianHupfer yes of course. If there is problem because of internal command, in this case one can do a serch replace \textbf{ with \bfindex{ and \newcommand{\bfindex}[1]{\textbf{#1}\index{#1}}
    – touhami
    Commented Oct 12, 2015 at 20:50
  • @touhami: That command worked great. Thank you so very much.
    – user218075
    Commented Oct 14, 2015 at 21:39

1 Answer 1


This is a typical case of visual markup being used to describe logical concepts. If then (at a later point) it becomes necessary to to additional processing for those elements (e.g., adding an index entry in this case) it is not really possible to do this from within TeX/LaTeX/ConTeXt/... because the visual markup is internally also used to format bits and pieces of other elements. E.g., in case of \textbf we might see this as part of a description label or inside a heading definition etc, and those shouldn't also get indexed.

TeX internally knows if a macro appeared on document level (as it keeps track of an input stack) but unfortunately this information is not available at the programming level (not even in LuaTeX as of now though Hans may add it in a next release) and I know of no other way to determine if a macro is called directly by the user or inside a definition of some other macro --- if somebody has a grande idea how to get that info I would be delighted to learn about it.

I therefore always recommend to mark up all logical elements explicitly for that even a remote chance exists that they need special formatting or processing one day. If you get into the habit of doing that it is not much extra work, but it saves you a lot eventually. All that is necessary up front is something like


and then use \term instead of the direct formatting directive. At a later stage you can then worry about making the definition smarter if necessary, e.g., replace it with a definition that also adds an index entry beside bolden the argument.

So in this particular case the best bet is as already outlined in the comments:

  • replace \textbf everywhere in the document with \term
  • and then define \term appropriately, e.g. as


as already pointed out by others.

You must log in to answer this question.

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