2

How can one refer in the main text to a glossary name followed by the exact page number of the glossary entry, i.e. instead of just:

See \gls{<label>}

which of course works something like:

See \gls{<label>} on page \pagenumber{<label>}

However \pageref{<label>} always returns ?? for a glossary entry label. Maybe I'm missing something?

1
  • The label used with gls is no label which can be handled over to \pageref -- \gls is like \ref -- getting a label value but not setting one (effectively). In addition, this wouldn't be unique, since you could use \gls{foo} on another page --> which page should then be referred too?
    – user31729
    Aug 5, 2015 at 18:09

2 Answers 2

2

This is only a partial solution -- it works for the first use of \gls{foo} label only, later calls with the same label are 'ignored' or do nothing.

glossaries does indeed define a glsentry-foo label when \gls{foo} is used -- but this refers to the location of foo in the glossary, not on the calling page.

I've grabbed a little bit of xparse etc. and other expl3 code and 'redefined' gls to add automatically an additional label glscall:foo, which can be referred to with \pageref{glscall:foo} then.

Please note that \gls* still works as usual, I did not touch that (so far)

\documentclass{article}

\usepackage{letltxmacro}
\usepackage{xparse}

\usepackage{glossaries}

\newcounter{someglscallcounter}

\makeatletter

\ExplSyntaxOn
\seq_new:N \g_glslabel_seq
\LetLtxMacro\orig@gls\gls
\RenewDocumentCommand{\gls}{omo}{%
  \IfValueTF{#1}{%
    \IfValueTF{#3}{%
      \orig@gls[#1]{#2}[#3]%
    }{%
      \orig@gls[#1]{#2}
    }%
  }{%
    \IfValueTF{#3}{%
      \orig@gls{#2}[#3]%
    }{%
      \orig@gls{#2}
    }% 
  }%
  \seq_if_in:NnTF{\g_glslabel_seq}{glscall:#2}{%
    % The label is already used, do nothing. 
  }{%
    \seq_gput_left:Nn{\g_glslabel_seq}{glscall:#2}%
    \typeout{Adding glscall:#2}
    \refstepcounter{someglscallcounter}%
    \label{glscall:#2}%
  }%
}

\ExplSyntaxOff
\makeatother


\makeglossaries

\newglossaryentry{duck}{%
name={Duck},
description={Ducks are great}
}

\newglossaryentry{cat}{%
name={Cat},
description={cats are great too}
}



\newglossaryentry{Elephant}{%
name={Elephant},
description={Elephants are huge!}
}

\begin{document}
A \gls{cat} is a cute animal.

A \gls{duck} is a quacking animal 

\gls{Elephant} and \gls{cat} are mammals. 

The entry for  \gls{duck} is on page \pageref{glsentry-duck} but it was used the first time on page \pageref{glscall:duck}

\clearpage

\printglossaries

\end{document}

enter image description here

1
  • For the expl3 experts: I know, I can drop the % characters :D
    – user31729
    Aug 5, 2015 at 21:39
2

The glossaries package has an option entrycounter, which defines and uses a counter to automatically number each entry in the glossary. This uses \refstepcounter to increment the counter and automatically adds a label, which means that with this option you can reference the label using \pageref.

You can prevent the number from appearing in the glossary by redefining \glsentrycounterlabel to do nothing, but the counter will still be incremented and labelled. The argument of \label isn't simply the glossary label, but is constructed from it with a prefix. The simplest method is to use the extension package glossaries-extra, which provides \glsxtrpageref for easy use.

The example below uses dummy entries defined in example-glossaries-brief.tex, which is provided with the glossaries package:

\documentclass{article}

\usepackage[entrycounter]{glossaries-extra}

\renewcommand{\glsentrycounterlabel}{}

\makeglossaries

\loadglsentries{example-glossaries-brief}
\glsaddall % add all to pad the glossary to two pages.

\begin{document}
See \gls{lorem} on page \glsxtrpageref{lorem}.

\printglossaries
\end{document}

This produces:

See lorem on page 2.

(Build process: latex, makeglossaries, latex, latex)

If you don't want to use glossaries-extra (or your version is too old to provide \glsxtrpageref), then you can simply do:

See \gls{lorem} on page \pageref{glsentry-lorem}.

Note that this method only works with glossary styles that use \glsentryitem (which all the predefined styles use).

You must log in to answer this question.

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