# Mark index entries in text itself, like showkeys, with memoir documentclass

This question is just like that of Mark indexed entries in the text itself?, except for use with the memoir document class.

How can index entries be marked within the text adjacent to where the \index command is placed — and not in the corner of the page as showidx does it, either:

• above the line, the way showkeys does it; and/or
• in the margin on the line where the \index appears?

Ideally, the user should have control over the format (font family, size, and color) of the entries that appear.

The answer at the cited question does not work with memoir.

A solution needs to work with:

1. hyperref (and Heiko Oberdiek's "quick and dirty fix to allow showidx and hyperref to coexist).
2. Multiple indexes, e.g. he main one and an index of names (where one uses \index[names]{..}).

Here's a sample source file, albeit with only a single index:

\documentclass[draft]{memoir}
\usepackage[usenames]{xcolor}

\makeindex

\usepackage[final]{hyperref}

%Begin the quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
\def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
\let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
\def\HyInd@showidx#1{%
\@showidx{#1}%
\if@nobreak
\ifvmode
\nobrak
\fi
\fi
}%
\let\siOrg@makeindex\makeindex
\let\siOrg@@index\@index
\let\siOrg@@wrindex\@wrindex
\let\siOrg@index\index
\RequirePackage{showidx}
\let\makeindex\siOrg@makeindex
\let\@index\siOrg@@index
\let\@wrindex\siOrg@@wrindex
\let\index\siOrg@index
% rest of hyperref part
\let\HyInd@org@wrindex\@wrindex
\def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
\def\HyInd@@wrindex#1#2|#3|#4\\{%
\ifx\\#3\\%
\HyInd@org@wrindex{#1}{#2|hyperpage}%
\else
\def\Hy@temp@A{#3}%
\ifx\Hy@temp@A\HyInd@ParenLeft
HyInd@org@wrindex{#1}{#2|#3hyperpage}%
\else
\HyInd@org@wrindex{#1}{#2|#3}%
\fi
\fi
}%
}{%
\def\@wrindex#1{\@@wrindex#1||\\}
\def\@@wrindex#1|#2|#3\\{%
\ifx\\#2\\%
\protected@write\@indexfile{}{%
\string\indexentry{#1|hyperpage}{\thepage}%
}%
\else
\def\Hy@temp@A{#2}%
\ifx\Hy@temp@A\HyInd@ParenLeft
\protected@write\@indexfile{}{%
\string\indexentry{#1|#2hyperpage}{\thepage}%
}%
\else
\protected@write\@indexfile{}{%
\string\indexentry{#1|#2}{\thepage}%
}%
\fi
\fi
\endgroup
\HyInd@showidx{#1}%
\@esphack
}%
}%
\fi
\makeatother
%
%End the quick and dirty hack'' due to Heiko Oberdiek which loads showidx

\begin{document}

\tableofcontents

\chapter{We begin}\index{beginning}

Hello\index{greeting!hello|(}%
\index{Hello}  %
to the World!\index{World}
\index{greeting|seealso{Hello,World}}

\newpage
abc\index{abc}%
\index{alphabet|see{abc}} %
is before def.\index{greeting!hello|)}

\printindex

\end{document}


And here's an accompanying .ist file:

headings_flag 1

• I think that you are asking for a change to memoir. I have looked at my original code but through fading capabilities I can't understand it. Perhaps the current maintainer could help. – Peter Wilson May 31 at 18:07
• @PeterWilson: Thank you for doing that much! I was hoping there could be a simple patch the user could include in a preamble, given that (1) memoir emulates showidx, and (2) the answer at tex.stackexchange.com/questions/64249/… does what I want if one is using showidx itself (with book or article documentclass). – murray Jun 1 at 0:30