I am producing some glossed linguistic texts in a Native American language. The sentences are labeled as, e.g., (2.3)—meaning “text 2, sentence 3”—and I’d like the index to pick up on these numbers, rather than page numbers. In terms of the picture below, I’d like:
Bratwurst, (2.1)
Sauerkraut, (2.2)
as opposed to what’s shown (Bratwurst, 2; Sauerkraut, 2).
How can I achieve this?
I’m using a version of expex for the example numbers. It's too long to include here, but I can email it to any interested parties.
*Clarification. I know how to label examples (by putting e.g., <label>
after \ex
) and how to call labels at a later point (\getref{label}
). This questions isn't about example labelling however: I don't want to have to manually link cabbage to, say, \getref{Sauerkraut}
, \getref{Kohl}
, \getref{Wirsing}
. I want to piggyback off makeindex
to produce an index that lists example numbers instead of page numbers.
\documentclass{report}
\usepackage{expex-x2}
\usepackage{expexchapno}
\lingset{aboveexskip=0pt,glspace=1em plus .5em minus.2em,
glrightskip=0pt plus 8em,aboveglftskip=0pt,glneveryline={\it},
glstyle=nlevel,everygl={\hangindent=1.2em \hangafter=1},
exnotype=chapter.arabic}
\usepackage{makeidx}
\makeindex
\begin{document}
\gathertags
\chapter{First chapter}
\chapter{Second chapter}
\ex Bratwurst --- this example is followed by \verb"\index{Bratwurst}"
\xe \index{Bratwurst}
\ex Sauerkraut --- this example is followed by
\verb"\index{Sauerkraut}" \xe \index{Sauerkraut}
\printindex
\end{document}
Best Answer
Please allow me to share my findings with you. I've divided my answer into three parts: no traffic, one-way traffic and two-way traffic, each part resulting in its own compilable TeX file.
I have used Xindy because MakeIndex is rather limited for non-English languages. On the other hand, I am using rather old
makeidx
package, but I would recommend to use theimakeidx
package because we could save one I/O write slot in the last part. I wanted to compare methods, I used that way.I am using
lualatex
to process the TeX files, but any majorlatex
engine can be used.As it was already pointed out on it, we can redefine
\@wrindex
as it is the key part called by the\index
command. We can store the information directly by calling for\ep@rawexnoprint
, see line 19 in the first file. That's the command theexpex
package uses to store information.Next problem is how to notify the index processor about number dot number location. Xindy uses location class for this purpose, it uses several of them and we can create a new one. It can be done by
define-location-class
plus we define accompanyingmarkup-locref
for this new class, in addition to that we are adding parentheses. The last step would be to define the order of new location classes, with your permission, I will show you later how it can be done.We run:
The content of the
mal-expex-1.idx
file is as follows:I enclose the TeX code and a preview of three cropped pages. As we can see from the last page, this is the requested form, but Houston, we have a problem! Once we want clickable version of our document, the index cross-references are not working.
Well, I have already opened that Pandora's box, let me inform you how we can save our souls. That major problem brings me to load the
hyperref
package and explore our further possibilities.The idea was to use regular cross-references as we know it from
\label
+\ref
+\pageref
combination, no matter of what will be typeset in the index location of index entries.I have redefined the
\@wrindex
, added|myhyperlink{\ep@rawexnoprint}
which helps us to control the output. The key step was adding\label{\ep@rawexnoprint}#1
into this command. It stores reference information and prints the reference for us, therefore we don't have to type it twice.We notify the index processor about a new command by
define-attributes
and we define a new class as in the first example. The result of our efforts is that the number is clickable, the parentheses are not.There is a small problem as
hyperref
is redefining commands that it is clickable but it leads us to the page not to the line where\index
command was called. If we explore themal-expex-2.aux
file further we can see what's going on:It stored all the necessary information, but it leads to the second chapter. That's not the best result we can achieve here as the title itself could be typeset several pages earlier than the
expex
examples. Well, we have got some improvement. We are getting error messages fromxindy
about cross-reference-targets not existing, but we can ignore them, we manage that part by ourselves.We run these three lines:
The content of the
mal-expex-2.idx
file is this:I enclose the TeX code and a preview of the result.
Once we can improve something, we will. We wish to have clickable version of the document with cross-references leading to the proper locations. Moreover, we can try to have cross-references from both directions. From
\index
to the index and from index entries in index back to its\index
counterparts. We must bear in mind that index entries are grouped and we may lose some hyperlinks in index, generally speaking.For this I used a couple of
\hypertarget
and\hyperlink
commands assuring I provide them unique markers, see lines 40-47 in the code below. It is corrected version, it goes to the original location, but there is no way back. Let's focused on that feature.I have prepared a new file for next experiments to separate them from regular
idx
file for purpose of this post. I will usemyi
extension (reflects theidx
file) andiym
extension (reflects theind
file).The core of this method is to use
\hypertarget
and\hyperlink
twice: one pair handling one way, the other pair handling the way back. Please see lines 50-65 in the code. I needed to use\raisebox
to get location from left baseline of the box to the left top corner.I also prepared a new set of commands for regular use. We can store page numbers (it is a common method; plain arabic number), we can store number of
expex
example (arabic number dot arabic number wrapped in the parentheses) and chapter number (arabic number wrapped in the brackets). Using this approach, see lines 68-70, we can store almost anything there.The only remaining thing we must do is to notify
xindy
about all this. Let's extend ourxdy
style file, please see lines 20-36 in the TeX code.To make our work easier I have done one more improvement. I have redefined the
\ex...\xe
statements to the form of just\ex...
by defining the end of the line (^^M
) as a command and using it afterwards. This step is of course not necessary in production, I just wanted to show you that it can be done and we can save typing of\xe
.We run the following four lines:
The content of the
mal-expex-3.idx
file is the same as in the second part, because we redefined commands at the TeX level. The content of themal-expex-3.myi
file is something new for us and it looks like this:I enclose the last example and a preview of our efforts.