14

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?

towi
  • 2,500
  • 2
  • 22
  • 29

1 Answers1

16

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.

egreg
  • 1,121,712
  • Is there an easy way to make this depend to the "draft" documentclass option? – Alessandro Cuttin Nov 06 '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 06 '11 at 13:16
  • 1
    @egreg: The \ifdraft syntax is not \ifdraft then \else else \fi but \ifdraft{ then }{ else } – Schweinebacke Nov 06 '11 at 15:18
  • 1
    @egreg: BTW: Why not \marginpar{\begingroup\texttt{\detokenize{#1}}\endgroup}? – Schweinebacke Nov 06 '11 at 15:24
  • @egreg: In a short test \pretocmd at the argument of \ifdraft seems to fail every time. It even seems to fail at the first argument of \@firstoftwo. So you have go back to \if@draft ... \fi. Sorry. – Schweinebacke Nov 06 '11 at 15:34
  • @Schweinebacke It's a good idea: \marginpar{\texttt{\detokenize{#1}}} – egreg Nov 06 '11 at 17:48
  • This produces no index marks whatsoever for me with documentclass memoir. Can it be made to work with memoir? – murray May 30 '19 at 14:28
  • @murray Probably so, but some more details are needed: memoir changes several things connected to index production. Please, open a new question. – egreg May 30 '19 at 14:47
  • @egreg: See new question https://tex.stackexchange.com/questions/493407/mark-index-entries-in-text-itself-like-showkeys-with-memoir-documentclass. – murray May 31 '19 at 15:17