Latest news 2021-09-06: new blog post "Legacy Documents and TeX Live Docker Images".

glossaries package FAQ

Is it possible to reference the locations outside of the glossary? 🔗

Yes, but how well it works depends on the indexing method.

With \makenoidxglossaries, the locations are stored in the loclist field using the syntax of an etoolbox internal list (see Iteration With etoolbox’s Internal Lists) where each element is in a particular location format. Note that this method can’t compress consecutive locations into a compact range.

Example document (uses the example-glossaries-brief.tex file provided with glossaries that contains dummy entries for testing):

\documentclass{report}

\usepackage{glossaries}

\makenoidxglossaries

\loadglsentries{example-glossaries-brief}

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}
Test locations: \glsentrynumberlist{lorem}
or \glsdisplaynumberlist{lorem}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls{lorem}

\chapter{Sample E}
\gls{lorem}

\printnoidxglossary
\end{document}

This produces the following text on page 2:

Test locations: 1, 3, 4, 5 or 1, 3, 4 & 5.

Note that there are no ranges and note the difference in output between \glsentrynumberlist and \glsdisplaynumberlist. (In the second list, the last two items are separated by & instead of a comma.) If you use hyperref:

\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}

Then the locations will be hyperlinks (as long as the location format supports them).

If you use \makeglossaries instead of \makenoidxglossaries then it isn’t quite so straight-forward as it’s necessary to gather the information when the glossary is displayed with \printglossary. Now you need the savenumberlist package option because it’s extra work to gather this information, which needlessly adds to the build time if it’s not required.

\documentclass{report}

\usepackage[savenumberlist]{glossaries}

\makeglossaries

\loadglsentries{example-glossaries-brief}

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}
Test locations: \glsentrynumberlist{lorem}
or \glsdisplaynumberlist{lorem}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls{lorem}

\chapter{Sample E}
\gls{lorem}

\printglossary
\end{document}

Since the information can only be gathered when the glossary file is input by \printglossary, an extra LaTeX call is required. If the document is called myDoc.tex then the build process is now:

pdflatex myDoc
makeglossaries myDoc
pdflatex myDoc
pdflatex myDoc

The result is now:

Test locations: 1, 3–5 or 1 & 3–5.

Note that the ranges are now present. Unfortunately, \glsdisplaynumberlist doesn’t work with hyperref. If you add hyperref you will find the following message in the transcript:

Package glossaries Warning: \glsdisplaynumberlist doesn't work with hyperref. Using \glsentrynumberlist instead

Compare this now with bib2gls (the following document uses example-glossaries-brief.bib provided with glossaries-extra):

\documentclass{report}

\usepackage[colorlinks]{hyperref}
\usepackage[record]{glossaries-extra}

\GlsXtrLoadResources[src={example-glossaries-brief}]

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}
Test locations: \glsentrynumberlist{lorem}
or \glsdisplaynumberlist{lorem}.

Directly accessing the location field:
\glsxtrifhasfield{location}{lorem}{\glscurrentfieldvalue}{??}.
Iterating over the loclist field:
\glsxtrifhasfield{loclist}{lorem}{\glsnoidxloclist{\glscurrentfieldvalue}}{??}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls{lorem}

\chapter{Sample E}
\gls{lorem}

\printunsrtglossary
\end{document}

This now produces:

Test locations: 1, 3–5 or 1, 3–5.

Directly accessing the location field: 1, 3–5. Iterating over the loclist field: 1, 3, 4, 5.

So there are now extra ways of accessing the location lists, but note that \glsdisplaynumberlist now produces the same result as \glsentrynumberlist.

The formatted location list is stored in the location field and the individual locations are stored in the loclist field (the same as for the \makenoidxglossaries method). Note that with the hyperref package the locations are all hyperlinks.

With bib2gls, the entries aren’t defined on the first LaTeX call, so I’ve used \glsxtrifhasfield to test if the field has been set and put ?? in the false part so it’s easier to detect when the field hasn’t been set. (This also helps to check that the entry label and field label have been typed correctly.)

Note that this method also works without \printunsrtglossary, unlike the \makeglossaries method (which can’t work without \printglossary) or the \makenoidxglossaries method (which can work without \printnoidxglossary but issues a warning).

You can also instruct bib2gls to save primary locations (identified by designated formats) to a special field, which is useful if you don’t require the entire list. For example:

\documentclass{report}

\usepackage[colorlinks]{hyperref}
\usepackage[record]{glossaries-extra}

\GlsXtrLoadResources[src={example-glossaries-brief},
 save-primary-locations={default format},
 primary-location-formats={primary}]

\newcommand{\primary}[1]{\hyperbf{#1}}

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}

Full location list: 
\glsxtrifhasfield{location}{lorem}{\glscurrentfieldvalue}{??}.

Primary location(s):
\glsxtrifhasfield{primarylocations}{lorem}{\glsnoidxloclist{\glscurrentfieldvalue}}{}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls[format=primary]{lorem}

\chapter{Sample E}
\gls{lorem}

\printunsrtglossary
\end{document}

This now produces:

Full location list: 1, 3–5.

Primary location(s): 4.

The primary location can be saved and removed from the location field by changing the resource settings:

\GlsXtrLoadResources[src={example-glossaries-brief},
 save-primary-locations={remove},
 primary-location-formats={primary}]

The result is now:

Full location list: 1, 3, 5.

Primary location(s): 4.

With a combination of hyperref and record=nameref with the chapter counter as the location, it’s also possible to hyperlink to the chapter where the hyperlink text is the chapter title (rather than the chapter number):

\documentclass{report}

\usepackage[colorlinks]{hyperref}
\usepackage[record=nameref]{glossaries-extra}

\GlsXtrLoadResources[src={example-glossaries-brief},
 save-primary-locations={default format},
 primary-location-formats={primary}]

\newcommand{\primary}[1]{\hyperbf{#1}}

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}

Full location list: 
\glsxtrifhasfield{location}{lorem}{\glscurrentfieldvalue}{??}.

Primary location(s):
\glsxtrifhasfield{primarylocations}{lorem}{\glsnoidxloclist{\glscurrentfieldvalue}}{}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls[format=primary,counter=chapter]{lorem}

\chapter{Sample E}
\gls{lorem}

\printunsrtglossary
\end{document}

This now produces:

Full location list: 1, 3, Sample D, 5.

Primary location(s): Sample D.

Alternatively:

\documentclass{report}

\usepackage[colorlinks]{hyperref}
\usepackage[record=nameref]{glossaries-extra}

\GlsXtrLoadResources[src={example-glossaries-brief},
 loc-counters={equation,page},
 save-primary-locations={default format},
 primary-location-formats={primary}]

\newcommand{\primary}[1]{\hyperbf{#1}}

\GlsXtrAutoAddOnFormat{primary}{counter=chapter}

\begin{document}
\chapter{Sample A}
\gls{lorem}

\chapter{Sample B}

Full location list: 
\glsxtrifhasfield{location}{lorem}{\glscurrentfieldvalue}{??}.

Primary location(s):
\glsxtrifhasfield{primarylocations}{lorem}{\glsnoidxloclist{\glscurrentfieldvalue}}{}.

\chapter{Sample C}
\gls{lorem}

\chapter{Sample D}
\gls[format=primary]{lorem}

\chapter{Sample E}
\gls{lorem}

\printunsrtglossary
\end{document}

Which produces:

Full location list: 1, 3–5.

Primary location(s): Sample D, 4.

2020-06-28 22:24:19


Permalink: https://www.dickimaw-books.com/faq.php?id=239
Alternative link: https://www.dickimaw-books.com/faq.php?itemlabel=savelocationlist

Category: glossaries package
Topic: Referencing Terms