3

I am making an index for my document using \makeindex. And in my document I have subindex entries like :

\index{word1!subword11}
\index{word1!subword12}

But when there is only one subentry in the whole document associated to a given word, I would like to know how to do as if there was no subentry. In other terms, if the only sub-entry of word2 is subword21 in the whole document, I want the result to be equal to : \index{word2!subword21} == \index{word2 subword21}.

How to do that?

1 Answer 1

1

I don't think there is a way to achieve this with the standard makeindex/xindy approach and, given that this question has been unanswered for so long, it seems that no one else can think of a way. So here's an alternative method that uses a completely different approach. Instead of using \index, this uses the glossaries interface with glossaries-extra and bib2gls.

All terms need to first be defined in one or more .bib files. For example, entries.bib:

% Encoding: UTF-8

@index{test}

@index{samplephrase,
  name = {sample phrase}
}

@index{word1}

@index{subword11,
  name={subword11},
  parent={word1}
}

@index{subword12,
  name={subword12},
  parent={word1}
}

@index{word2}

@index{subword21,
  name={subword21},
  parent={word2}
}

@index{subword22,
  name={subword22},
  parent={word2}
}

The first MWE uses the default settings, which results in a lonely sub-item, for comparison:

\documentclass{article}

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

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

\begin{document}
\Gls{test}, \gls{samplephrase}, \gls{subword11}, \gls{subword12} and 
\gls{subword21}.

\printunsrtglossary[title=Index]
\end{document}

If the document is called test.tex then the document build is:

pdflatex test
bib2gls -g test
pdflatex test

(Replace pdflatex with xelatex etc, as appropriate.)

The result is:

Test, sample phrase, subword11, subword12 and subword21. Index S sample phrase, 1 T test, 1 W word1 subword11, 1 subword12, 1 word2 subword21, 1

The lonely sub-item can be shifted up a hierarchical level with the flatten-lonely option. The value must be one of false (don't modify), presort (which adjusts the hierarchical level before sorting) or postsort (which adjusts after sorting).

flatten-lonely=presort will move the item since it will be sorted by its name rather than following its parent's position:

\documentclass{article}

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

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en},% sort according to this language tag (en = English)
  flatten-lonely=presort% flatten lonely sub-items
]

\begin{document}
\Gls{test}, \gls{samplephrase}, \gls{subword11}, \gls{subword12} and 
\gls{subword21}.

\printunsrtglossary[title=Index]
\end{document}

Test, sample phrase, subword11, subword12 and subword21. Index S sample phrase, 1 subword21, 1 T test, 1 W word1 subword11, 1 subword12, 1

The sub-item has moved to the S letter group and its parent (word2) has been removed completely.

flatten-lonely=postsort makes the adjustments after sorting so the sub-item is moved up a hierarchical level but stays in the same relative position. The name is adjusted and the parent item is removed:

\documentclass{article}

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

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en},% sort according to this language tag (en = English)
  flatten-lonely=postsort% flatten lonely sub-items
]

\begin{document}
\Gls{test}, \gls{samplephrase}, \gls{subword11}, \gls{subword12} and 
\gls{subword21}.

\printunsrtglossary[title=Index]
\end{document}

Test, sample phrase, subword11, subword12 and subword21. Index S sample phrase, 1 T test, 1 W word1 subword11, 1 subword12, 1 word2, subword21, 1

The text used by \gls{subword21} hasn't changed, but the entry is now listed as "word2, subword21". The name field is actually changed to:

\bibglsflattenedchildpostsort{word2}{subword21}

which is simply defined as

\providecommand*{\bibglsflattenedchildpostsort}[2]{#1, #2}

in the .glstex file that's input by \GlsXtrLoadResources, so you can modify the formatting. For example:

\newcommand*{\bibglsflattenedchildpostsort}[2]{#1 #2}

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en},% sort according to this language tag (en = English)
  flatten-lonely=postsort,% flatten lonely sub-items
]

By default, you can't flatten a lonely sub-item if its parent has been indexed. For example:

\documentclass{article}

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

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en},% sort according to this language tag (en = English)
  flatten-lonely=postsort% flatten lonely sub-items
]

\begin{document}
\Gls{test}, \gls{samplephrase}, \gls{word1}, \gls{subword12} and
\gls{subword21}.

\printunsrtglossary[title=Index]
\end{document}

There are now two lonely sub-items, but only one is adjusted because the other has a parent that's been indexed:

Test, sample phrase, word1, subword12 and subword21. Index S sample phrase, 1 T test, 1 W word1 1 subword12, 1 word2, subword21, 1

In this case, setting flatten-lonely-rule={discard unrecorded} can adjust the lonely sub-items but will only remove unindexed parents:

\documentclass{article}

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

\GlsXtrLoadResources[
  src={entries}, % data in entries.bib
  sort={en},% sort according to this language tag (en = English)
  flatten-lonely=postsort,% flatten lonely sub-items
  flatten-lonely-rule={discard unrecorded}
]

\begin{document}
\Gls{test}, \gls{samplephrase}, \gls{word1}, \gls{subword12} and
\gls{subword21}.

\printunsrtglossary[title=Index]
\end{document}

The result is now:

Test, sample phrase, word1, subword12 and subword21. Index S sample phrase, 1 T test, 1 W word1 1 word1, subword12, 1 word2, subword21, 1

You must log in to answer this question.

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