6

This is a follow-up question to: How to make indexed words clickable in the text?

I'm having two problems with the code given by @Stephen.

  1. If I use the following code to redefine the \index command, the link to the entry in the Index, will not work with xindy

    ‎\let\oldindex\index‎‎
    \renewcommand*{\index}[1]{\oldindex{#1@\protect‎\hypertarget{#1.ind}{}#1}\href{\#‌​#1.‎ind}{#1}}‎
    
  2. My index entries with subentries (e.g. \index{foo!bar}) could not be used this way.

BTW, my document is in Persian which is an RTL language.

Edit:
Here is the code which I'm having problem with it:

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

\let\oldindex\index
\renewcommand*{\index}[1]{\oldindex{#1@\protect\hypertarget{#1.ind}{}#1}\href{\#‌​#1.ind}{#1}}

\begin{document}
test \index{test}% problem 1
\index{foo!bar}% problem 2

\printindex
\end{document}
9
  • 2
    Most likely you need to use \LetLtxMacro, instead of \let. A detailed description of \LetLtxMacro can be found at this question at closed square root symbol. Commented Nov 10, 2012 at 21:41
  • 1
    Please make a MWE with all the code necessary to show how this "doesn't work".
    – egreg
    Commented Nov 10, 2012 at 22:32
  • @egreg: I added a MWE to my post. Commented Nov 11, 2012 at 6:03
  • The problem is that \subsubitem is written inside the argument of \hypertarget. Probably Xindy gets confused by the two ! it finds in the same entry.
    – egreg
    Commented Nov 11, 2012 at 10:31
  • 2
    @egreg: Isn't there a solution to this problem? Commented Nov 11, 2012 at 18:49

1 Answer 1

3

Since no one has been able to provide an answer, here's an alternative approach. Instead of using makeidx and xindy this uses glossaries-extra and bib2gls. This means that instead of using \index the terms that need indexing must all be defined in a .bib file.

For example, the file entries.bib contains:

% Encoding: UTF-8

@index{test}

@index{foo}

@index{bar,
  name={bar},
  parent={foo}
}

The document test.tex is:

\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[record, % using bib2gls
 nostyles,% don't load the default styles
 stylemods={bookindex},% load glossary-bookindex.sty
 style=bookindex% use the bookindex style
 ]{glossaries-extra}

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en}% sort by this language tag (en = English)
]

\begin{document}
\Gls{test}.\glsadd{bar}

\printunsrtglossary[title=Index]

\end{document}

The document build is:

pdflatex test
bib2gls -g test
pdflatex test
pdflatex test

(Replace pdflatex with xelatex etc as appropriate. The third instance just makes sure that the PDF bookmarks are up-to-date.)

The result is:

Test. Index F foo bar, 1 T test, 1

The red text indicates a hyperlink.

If you forget the -g (--group) switch, you won't get the group headings:

Test. Index foo bar, 1 test, 1

Notes:

  • @index first identifies the label used to reference the term (analogous to BibTeX where the label is used in \cite). This will also be the text used in the index and by commands like \gls unless overridden by the name field.
  • The label can't contain any special characters, commas or spaces. When used with inputenc, the label also can't contain any non-ASCII characters either, but since you're using Persian I'm guessing that you're probably using fontspec with XeLaTeX or LuaLaTeX.
  • \gls{label} displays the term and creates a hyperlink to the relevant line in the index. \Gls converts the first letter to upper case. You can get the plural forms with \glspl and \Glspl.
  • \glsadd{label} just adds the term to the index without producing any text (and therefore no hyperlink).
  • You don't need to specify the language in sort if it matches the document language. I've used sort=en because the document doesn't include any language information.

Here's another example. File entries.bib now contains:

% Encoding: UTF-8

@index{bird}

@index{duck,
  name={duck},
  parent={bird}
}

@index{goose,
  name={goose},
  plural={geese},
  parent={bird}
}

@index{wildfowl,
  alias={bird}
}

The document test.tex contains:

\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[record, % using bib2gls
 nostyles,% don't load the default styles
 stylemods={bookindex},% load glossary-bookindex.sty
 style=bookindex% use the bookindex style
 ]{glossaries-extra}

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en}% sort by this language tag
]

\begin{document}
\Glspl{goose} and \glspl{duck}. \Gls{wildfowl} links to `bird' not 
`wildfowl' in the index.

\printunsrtglossary[title=Index]

\end{document}

The result is now:

Geese and ducks. Wildfowl links to ‘bird’ not ‘wildfowl’ in the index. Index B bird, 1 goose, 1 duck, 1  W wildfowl, see bird

You must log in to answer this question.

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