# Visualize index entries in the text?

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?

-
Related Question: Mark indexed entries in the text itself?. – Peter Grill Aug 18 '14 at 6:49
@PeterGrill Whow. Had I only known. I put my own tikz macro in place, doing exactly that. – towi Aug 18 '14 at 13:22
Well, that is a new question. Perhaps you can post an answer using your tikz macros. – Peter Grill Aug 18 '14 at 17:30

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.

-
Is there an easy way to make this depend to the "draft" documentclass option? – Alessandro Cuttin Nov 6 '11 at 12:04
There’s also the ifdraft package which makes it maybe easier (i.e. more readable) to test if one is in draft mode or not. – Tobi Nov 6 '11 at 13:16
@egreg: The \ifdraft syntax is not \ifdraft then \else else \fi but \ifdraft{ then }{ else } – Schweinebacke Nov 6 '11 at 15:18
@Schweinebacke Thanks! – egreg Nov 6 '11 at 15:20
@egreg: BTW: Why not \marginpar{\begingroup\texttt{\detokenize{#1}}\endgroup}? – Schweinebacke Nov 6 '11 at 15:24