# Error with entries in index page header, memoir class

I wanted my "see" cross-references within the index to be formatted in the form:

this (see that)

So thanks to Change index "see also" and "see" format, I use the definitions of \seeonly shown in the following source file.

But I also want the page headings of the index to show the first and last index entry on each page there. So I also defined \idxmark, use the index page style, and employ the index .ist style file described in the memoir documentation and now also included in my source.

% File indexdoc.tex
\documentclass{memoir}

\newcommand{\idxmark}[1]{#1\markboth{#1}{#1}}

\makepagestyle{index}
\makeevenfoot{index}{}{\thepage}{}
\makeoddfoot{index}{}{\thepage}{}

\newcommand\gobbleone[1]{}
\newcommand{\seeonly}[2]{\, (\emph{\seename} #1)}
\let\oldindex\index
\renewcommand{\index}[1]{\def\exptoindex{#1}\expandafter\oldindex\expandafter{\exptoindex}}
\newcommand{\indexsee}[2]{\index{#1@#1\protect\gobbleone|seeonly{#2}}}

\begin{filecontents}{indexdoc.ist}
% MakeIndex style file
% output main entry <entry> as: \item \idxmark{<entry>},
item_0  "\n\\item \\idxmark{"
delim_0 "}, "
% not forgetting the subitem case
item_x1 "} \n \\subitem "
\end{filecontents}

\makeindex

\begin{document}

This is some text\indexsee{text}{verbiage}. Which is a synonym for verbiage\index{verbiage}.

\backmatter

\pagestyle{index}
\printindex

\end{document}


Those two things, alas, clash; I get error message:

 Writing index file indexdoc.idx
(./indexdoc.aux) [1{/Users/user/Library/texlive/2016/texmf-var/fonts/map/
pdftex/updmap/pdftex.map}] [2] (./indexdoc.ind
./indexdoc.ind:3: Argument of \gobbleone has an exra }.Runaway argument?
./indexdoc.ind:3: Paragraph ended before \gobbleone was complete.
\par
l.3 \item \idxmark{text\gobbleone }
, \seeonly{verbiage}{1}
?
<inserted text>
\par
l.3 \item \idxmark{text\gobbleone }
, \seeonly{verbiage}{1}
?


Without the \idxmark definition, the index page style, and the index .ist style file from memman.pdf, the definition of \seeonly (in terms of \gobble) causes no problem.

How can the clash be resolved?

Originally accepted answer not robust with math

I had originally accepted "siracusa"'s answer, which redefined \idxmark. However, I now find that it fails the \indexsee command — but is OK for the \indexAlso command — when the first argument has the form

alfatxt@$alfamath$


(for alphabetizing math expression properly in the index).

% File indexdoc2.tex
\documentclass{memoir}

\newcommand{\idxmark}[1]{{\let\gobbleone\relax\markboth{#1}{#1}}#1}

\makepagestyle{index}
\makeevenfoot{index}{}{\thepage}{}
\makeoddfoot{index}{}{\thepage}{}

\let\oldindex\index
\renewcommand{\index}[1]{\def\exptoindex{#1}\expandafter\oldindex\expandafter{\exptoindex}}

\newcommand\gobbleone[1]{}
\newcommand{\seeonly}[2]{\, (\emph{\seename} #1)}
\newcommand{\indexsee}[2]{\index{#1@#1\protect\gobbleone|seeonly{#2}}}
\newcommand{\indexAlso}[2]{\index{#1!zzzz@\protect\gobbleone|Also{#2}}}

\begin{filecontents}{indexdoc2.ist}
% MakeIndex style file
% output main entry <entry> as: \item \idxmark{<entry>},
item_0  "\n\\item \\idxmark{"
delim_0 "}, "
% not forgetting the subitem case
item_x1 "} \n \\subitem "
\end{filecontents}

\makeindex

\begin{document}

This is some text\indexsee{text}{verbiage}. Which is a synonym for verbiage\index{verbiage}.

$\pi$\index{pi@$\pi$ (math constant)} % OK

$\rho$ \indexsee{$\rho$}{density} % OK
$\sigma$ \indexsee{sigma@$\sigma$}{conductivity}    % NO entry in index!

$\mu$ \indexAlso{$\mu$}{magnetic moment} % OK
$\alpha$ \indexAlso{alpha@$\alpha$}{angular acceleration}  % OK !

\backmatter

\pagestyle{index}
\printindex

\end{document}


Use the following definition for \idxmark instead:

\newcommand{\idxmark}[1]{{\let\gobbleone\relax\markboth{#1}{#1}}#1}


The \gobbleone takes one parameter which then is discarded from the output, in this case it's supposed to discard the following comma. The problem is that by wrapping the \idxmark call around the index text, the \gobbleone goes as part of the parameter into this command. The expanded result when \idxmark is called looks like this:

text\gobbleone\markboth{text\gobbleone}{text\gobbleone}


Not only that now the \markboth is gobbled, the extra \gobbleones are completely wrong. To fix this, we first move the plain #1 in \idxmark at the end such that the \gobbleone actually gobbles the following comma. Now the extra \gobbleones have to be removed which is done by redefining them to \relax ("do nothing") in a local group. The expanded result now is equivalent to

\markboth{text\relax}{text\relax}text\gobbleone


Edit: The problem in your edit isn't really math but that the definition of \indexsee already uses the @-syntax for providing an additional term for sorting. Now, if you provide another one with the call of that macro, makeindex seems to be confused and rejects the entry.

To work around that, here's a new version for \indexsee:

\def\indexsee#1{\indexseeI#1@\end}
\def\indexseeI#1@#2\end{%
\def\temp{#2}%
\ifx\temp\empty
\def\next{\indexseeIII{#1}{#1}}%
\else
\def\next{\indexseeII#1@#2\end}%
\fi
\next
}
\def\indexseeII#1@#2@\end{\indexseeIII{#1}{#2}}
\def\indexseeIII#1#2#3{\index{#1@#2\protect\gobbleone|seeonly{#3}}}


That new version uses some helper macros to check if the first argument already includes an @. If so, it uses the provided term for sorting. If no @ occurs in the parameter, the original version is used (which used the same term as index entry and for sorting).

Here's what the output for your sample looks like with the new version:

Edit 2: Thinking about it, you're probably better off with just using a version that doesn't duplicate the parameter in the definition:

\newcommand{\indexsee}[2]{\index{#1\protect\gobbleone|seeonly{#2}}}


If your entry is plain text, you gain nothing from duplicating it. If it is math, it's moved to the wrong position (as you can see with \mu and \rho in your sample). So for all other uses than plain text you probably want to provide the correct sorting term yourself anyway.

• See end of revised question: this answer's method fails with \seeonly when its 1st argument has form txt@$math$: no index entry appears in that case. – murray Oct 9 '16 at 0:27
• @murray I added a version to work around that – siracusa Oct 9 '16 at 6:10
• The fix fixes the indexing omission I encountered -- thanks again! – murray Oct 9 '16 at 16:00