[Tex/LaTex] Visualize index entries in the text

indexingmarginpartext-decorations

When I ad an index entry using \index with all the options available for the "format" and "see", and cross-reference, I'd like to see at the place where I make the index entry how this specific entry will/would appear in the index, i.e.

  • Is it bold here?
  • How is it sorted?
  • Will the number be bold?
  • Does it point elsewhere with "see"?

Currently I write the text in the margin with \marginpar but I am not quite satisfied with this. Maybe there is a better way that someone else already uses?

Best Answer

You can automatize the generation of the marginal note:

\usepackage{etoolbox}

\makeindex
\makeatletter
\pretocmd\@wrindex
  {\marginpar{\begingroup\catcode`\{=12\catcode`\}=12 \texttt{\scantokens{#1}}\endgroup}}
  {}{}
\makeatother

Thus each \index command will print in the margin the argument "as is". I believe this is better than trying to replicate the final aspect: all the information is there and in a very distinctive way.

The patching can be made depend on the draft option but not in a very robust way, as that option, in the standard classes, just sets \overfullrule:

\usepackage{etoolbox}

\makeindex
\makeatletter
\ifdim\overfullrule>\z@
  \pretocmd\@wrindex
    {\marginpar{\begingroup\catcode`\{=12\catcode`\}=12 \texttt{\scantokens{#1}}\endgroup}}
    {}{}
\fi
\makeatother

A more robust way might be to check if draft appears in the expansion of \@classoptionslist or putting the code inside a personal package, say margind.sty:

\ProvidesPackage{margind}
\@tempswafalse
\DeclareOption{draft}{\@tempswatrue}
\ProcessOptions\relax

\RequirePackage{etoolbox}

\if@tempswa
\pretocmd\@wrindex
  {\marginpar{\begingroup\catcode`\{=12\catcode`\}=12
     \texttt{\scantokens{#1}}\endgroup}}{}{}
\fi

A possible "definitive" solution that takes into account the suggestions in comments could be

\usepackage{ifdraft}
\usepackage{etoolbox}

\makeatletter
\def\margin@index#1{\marginpar{\texttt{\detokenize{#1}}}}
\ifdraft{}{\let\margin@index\@gobble}
\pretocmd{\@wrindex}{\margin@index{#1}}
\makeatother

The argument to the \index command will be written in the margin only when the documentclass option draft is specified.

Related Question