# Indexing and robust commands

I need to do a little massaging of my index entries before putting them into the index, so I defined a helper command. For the MWE, I'll just let the helper pass straight to \index.

\documentclass{amsart}
\makeindex
\newcommand\indexhelper[1]{\index{#1}}
\DeclareRobustCommand\RobustIndexHelper[1]{\index{#1}}
\begin{document}
A
\index{$\vec a$}
\indexhelper{$\vec a$}
\RobustIndexHelper{$\vec a$}
\end{document}


(The A is there so that there's some output, or else the .idx file is blank.) The resulting .idx file is

\indexentry{$\vec a$}{1}
\indexentry{$\mathaccentV {vec}17Ea$}{1}
\indexentry{$\mathaccentV {vec}17Ea$}{1}


I assume that this is something to do with fragility, which is why I tried the \DeclareRobustCommand; but that's basically cargo culting and, even worse, it didn't work. Aside from "don't put such entries into your bibliography" and "don't use a helper command", how can I deal with this?

• Note that, also with Christian’s solution, \bar{d} would be sorted before \vec{a}. So the best thing would be to help the indexing with the @ feature, which would make irrelevant the issue you're facing. – egreg Jan 6 '17 at 22:32
• @egreg, I agree (and that's part of what the actual helper does), but I'm not sure how that makes the issue irrelevant. No matter how it's sorted, I still need to get commands like \vec unmolested into the index file, and \indexhelper{a@$\vec a$} (without ChristianHupfer's \detokenize suggestion) doesn't work either. – LSpice Jan 7 '17 at 0:00

You have to write the literal string of the input for the wrapper command, i.e. such that possible fragile content for the wrapper, (and \vec is not robust).

The easiest way is \detokenize (an e-TeX - addition) (but I am not sure whether there might be some catcode issues )

\documentclass{amsart}
\makeindex
\newcommand\indexhelper[1]{\index{\detokenize{#1}}}
\DeclareRobustCommand\RobustIndexHelper[1]{\index{\detokenize{#1}}}
\begin{document}

A
\index{$\vec a$}
\indexhelper{$\vec a$}
\indexhelper{$\vec b$}
\RobustIndexHelper{$\vec c$}

\printindex
\end{document}

• Thank you! I take it that \RobustIndexHelper is there just to parallel my document? Just \indexhelper by itself seems to work fine, but maybe there's something important I'm missing. – LSpice Jan 6 '17 at 22:05
• @LSpice: both helper commands do the same right now. It depends what you really have in mind. If you are thinking about writing commands to a file, the robust command version is the better way – user31729 Jan 6 '17 at 22:07
• I'm not interested in writing to a file for its own sake, but, of course, indexing does use an .idx file. My helper is basically just \newcommand\indexhelper[1]{\index{#1}#1}, that is, indexing the term and dropping it in the document at the same time (which, it seems from other questions here, is a common task). For those purposes, is the robust version better? – LSpice Jan 6 '17 at 22:14
• @LSpice: No.... and filler text :D – user31729 Jan 6 '17 at 22:15
• The only objection might be if you would use imakeidx package -- in this case you can do more than one index (i.e. you can have a person name index and a 'science term' index) separately. In this case, \index has an optional argument. You would lose that possibility with your wrapper (as being defined in that way) – user31729 Jan 6 '17 at 22:27