# Nicely showing 'special' index entries in the margin

I want to show index entries in the margins. Here's what I'm doing:

\documentclass[a4paper, 12pt, twoside]{report}

\usepackage{kantlipsum}

\usepackage{marginnote}
\let\oldmarginnote\marginnote
\renewcommand{\marginnote}[1]{\oldmarginnote{\footnotesize #1}}

\usepackage{makeidx}
\makeindex
\let\oldindex\index
\renewcommand{\index}[1]{\oldindex{#1}\marginnote{\bfseries{#1}}}

\begin{document}

\kant[1]
\index{Kant}
\kant[2]
\index{Kant!Test}
\kant[3]
\index{Kant|textbf}
\kant[4]
\index{Kant|see {blah}}
\kant[5]
\index{Kant|seealso {xyz}}
\kant[6]

\end{document}


This works fine, but not for 'special' index keys: the ones that use the special characters ! and | to indicate special index entries. They're displayed in the margin as well, like so:

Is there a way to not do this, and either:

• Only show the main entry (here always 'Kant')
• Somehow get all the different parts of the index (main entry, sub entry, style, see, see also, etc.) separated and use them myself in the margin. This could give something like this in the end:

\marginnote{\bfseries{Main entry} (Sub entry)}


I have seen Mark indexed entries in the text itself? and Visualize index entries in the text? but they don't seem to take care about this.

You can do it with xparse; the entry is first split at the !, if existent. Then we take care of the |, ignoring it and what follows. Note that only one subindex level is supported. It is possible, however, to add support for two levels.

\documentclass[a4paper, 12pt, twoside]{report}

\usepackage{xparse}
\usepackage{marginnote}
\usepackage{kantlipsum}
\usepackage{makeidx}

\renewcommand{\marginfont}{\footnotesize\bfseries}

\makeindex

\let\oldindex\index
\renewcommand{\index}[1]{%
\oldindex{#1}%
\marginnote{\splitentry{#1}}%
}

\NewDocumentCommand{\splitentry}{ >{\SplitArgument{1}{!}} m }
{%
\splitentryEM#1%
}
\NewDocumentCommand{\splitentryEM}{mm}
{%
\IfNoValueTF{#2}
{\splitentryBAR{#1}}
{#1 -- \splitentryBAR{#2}}%
}
\NewDocumentCommand{\splitentryBAR}{ >{\SplitArgument{1}{|}} m }
{%
\dosplitentryBAR#1%
}
\NewDocumentCommand{\dosplitentryBAR}{mm}{#1}

\begin{document}

\kant[1]
\index{Kant}
\kant[2]
\index{Kant!Test}
\kant[3]
\index{Kant|textbf}
\kant[4]
\index{Kant|see {blah}}
\kant[5]
\index{Kant|seealso {xyz}}
\kant[6]

\printindex

\end{document}


Note that marginnote provides \marginfont for setting up the notes.

## Pages 3 and 4

If you want also to accommodate @ entries, you can modify the code in the following way:

\let\oldindex\index
\renewcommand{\index}[1]{%
\oldindex{#1}%
\marginnote{\splitentry{#1}}%
}

\NewDocumentCommand{\splitentry}{ >{\SplitArgument{1}{!}} m }
{%
\splitentryEM#1%
}
\NewDocumentCommand{\splitentryEM}{mm}
{%
\splitentryBAR{#1}%
\IfValueT{#2}{ -- \splitentryBAR{#2}}%
}
\NewDocumentCommand{\splitentryBAR}{ >{\SplitArgument{1}{|}} m }
{%
\dosplitentryBAR#1%
}
\NewDocumentCommand{\dosplitentryBAR}{ >{\SplitArgument{1}{@}} m m }
{%
\splitentryAT#1%
}
\NewDocumentCommand{\splitentryAT}{ m m }
{
\IfNoValueTF{#2}{#1}{#2}%
}


I tried with \index{Kant@K\'ant!ecole@\'ecole} getting

• Great, thanks a lot once again! Also for showing me \marginfont :) – Keelan Apr 2 '15 at 7:56
• One thing: there's a way to handle accents by doing something like \index{ecole@\'ecole}, and in that case we would want to take everything after the @ sign. It's thus somewhat similar to the \splitentryBAR, but not quiet. I don't fully understand your code and can't get it to work... suggestions? Is it possible to also make it work for things like school system!grand ecole@grand \'ecole? – Keelan Apr 3 '15 at 12:45
• @CamilStaps Done – egreg Apr 3 '15 at 13:00