4

I would like to index some text elements that may contain code, for example:

foo of type \code{bar}

where \code is something like \texttt. But when I stick this text into \index, the collation is different between code and normal text:

\index{foo of type \code{bar}}
\index{foo of type abc}
\index{foo of type unknown}

The collation is wrong, and to get the correct collation, I would need to say:

\index{foo of type bar@foo of type \code{bar}}
\index{foo of type abc@foo of type abc}
\index{foo of type unknown@foo of type unknown}

However, this is tedious to maintain.

Is it possible to write a single command that creates the stripped "bare" text for the collation? Ideally, something like:

\myindex{abc \code{def} ghi}

that becomes

\index{abc def ghi@abc \code{def} ghi}

?

It would be acceptable if the mechanism is only able to strip one particular command (such as \code above).


Small example to reproduce:

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

\begin{document}

\index{aba}
\index{abc}
\index{abe}
\index{ab\texttt{b}}
\index{ab\texttt{d}}

\printindex

\end{document}

This collates incorrectly. I would like a macro I could use in place of \index that would produce the correct collation order "aba", "abb", "abc", "abd", "abe".

2
  • I don't understand this question? Can you give some compilable document to start this.
    – user31729
    Jun 22, 2016 at 19:26
  • @ChristianHupfer: I added a small example.
    – Kerrek SB
    Jun 22, 2016 at 19:34

1 Answer 1

3

I am not sure this is a good way to do this but it seems to work for the minimal example posted. It attempts to solve only the restricted problem described i.e. where just one command is dealt with as described. In this case \code{} will be treated specially so that it does not affect the sorting, but only the format, of the entry.

\documentclass{article}
\usepackage{makeidx}
\makeindex
\newcommand*\ocode[1]{\string\texttt{#1}}
\newcommand*\nocode[1]{#1}
\let\code\ocode
\def\myindexx#1\relax{\index{#1}}
\newcommand*\myindex[1]{%
  \let\code\nocode
  \edef\tempa{#1}%
  \let\code\ocode
  \myindexx\tempa@#1\relax
}
\begin{document}
abc

\myindex{aba}
\myindex{abc}
\myindex{abe}
\myindex{ab\code{b}}
\myindex{ab\code{d}}

\printindex

\end{document}

sorted & formatted

2
  • For what purpose? You can allow an optional argument in the usual way i.e. \newcommand*\myindex[2][<default>]{.... and then turn all my #1 into #2 and use #1 in whatever way you wish in the definition of the command.
    – cfr
    Jun 24, 2016 at 23:40
  • Never mind, yes, it works perfectly. Index options are part of some other package/class (memoir, to be specific), and it composes just fine. Thanks again!
    – Kerrek SB
    Jun 26, 2016 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .