1

My document contains some landscape-orientation tables that I set inside an \afterpage command. When I try to index material that appears inside these tables, I sometimes end up with duplicate index entry headings. From looking at the .idx file, this is apparently because \afterpage rewrites the argument of my \index commands by removing \protect commands and introducing extraneous spaces. Here is a minimum working example:

\documentclass{article}
\usepackage{afterpage,makeidx}\makeindex

% For indexing material in tables
\newcommand{\tab}[1]{\emph{#1}}

% Stand-in example for a fragile command that needs \protection
\newcommand\fragilecmd{FragileCommand}

\begin{document}
\fragilecmd\index{FragileCommand@\protect\fragilecmd}
\fragilecmd-x\index{FragileCommand@\protect\fragilecmd!\protect\fragilecmd-x}
\afterpage{
\begin{table}
  \begin{tabular}{l}
    \fragilecmd \\
    \fragilecmd-x \\
  \end{tabular}
  \index{FragileCommand@\protect\fragilecmd|tab}
  \index{FragileCommand@\protect\fragilecmd!\protect\fragilecmd-x|tab}
\end{table}
}
\newpage\printindex
\end{document}

I expect the resulting index to look as follows:

Index

FragileCommand, 1, 2

FragileCommand-x, 1, 2

However, it instead looks like this:

Index

FragileCommand, 2

FragileCommand-x, 2

FragileCommand, 1

FragileCommand-x, 1

And here is the contents of the .idx file produced by makeindex:

\indexentry{FragileCommand@\protect\fragilecmd}{1}
\indexentry{FragileCommand@\protect\fragilecmd!\protect\fragilecmd-x}{1}
\indexentry{FragileCommand@\fragilecmd |tab}{2}
\indexentry{FragileCommand@\fragilecmd !\fragilecmd -x|tab}{2}

How can I properly index such entries appearing in \afterpage?

  • This is nothing to do with afterpage, \index reads its argument verbatim but like all verb things that does not work in the argument of another command. if uou use \newcommand\myindex[1]{\index{#1}} then use \myindex instead of index you will disable the verb feature and the form written to the idx file will match – David Carlisle Dec 12 '15 at 17:01
  • by the way you are missing lots of % you have \end{tabular} <space> \index{} <space> \index{} so could get spurious line breaks at those spaces if the table is wide enough. – David Carlisle Dec 12 '15 at 17:03
1

You don't need \protect in the first two cases: \index at the top level (not inside the argument to a macro) uses a verbatim mode for writing the entries in the .idx file.

What you need instead is \string in the entries within \afterpage, because \index now is in the argument to another command.

\documentclass{article}
\usepackage{afterpage,makeidx}\makeindex

% For indexing material in tables
\newcommand{\tab}[1]{\emph{#1}}

% Stand-in example for a fragile command that needs \protection
\newcommand\fragilecmd{FragileCommand}

\begin{document}
\fragilecmd\index{FragileCommand@\fragilecmd}
\fragilecmd-x\index{FragileCommand@\fragilecmd!\fragilecmd-x}
\afterpage{%
\begin{table}
  \begin{tabular}{l}
    \fragilecmd \\
    \fragilecmd-x \\
  \end{tabular}%
  \index{FragileCommand@\string\fragilecmd|tab}%
  \index{FragileCommand@\string\fragilecmd!\string\fragilecmd-x|tab}%
\end{table}%
}
\newpage\printindex
\end{document}

This is what appears in the .idx file:

\indexentry{FragileCommand@\fragilecmd}{1}
\indexentry{FragileCommand@\fragilecmd!\fragilecmd-x}{1}
\indexentry{FragileCommand@\fragilecmd|tab}{2}
\indexentry{FragileCommand@\fragilecmd!\fragilecmd-x|tab}{2}

and so the index will be as expected

enter image description here

  • This works, except that \string gobbles spaces after the command it escapes. So \index{Foo Bar@\foo \bar} produces a different index entry than \index{Foo Bar@\string\foo \string\bar}. Can your answer be adapted to address this? – Psychonaut Dec 14 '15 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.