9

I want to reference the glossary in the text. The solution given to this question on this website works for me. But adding \usepackage{hyperref} before \usepackage{glossaries} makes it not work anymore (the output is "The glossary starts on page ??"). How can I reference the page the glossary is on while also using the hyperref package?

\documentclass{book}

\usepackage{hyperref}

\usepackage{glossaries}
\makeglossaries

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@glossarysection}{%
  \@@glossaryseclabel%
}{%
  \@@glossaryseclabel%
  \label{\glsautoprefix\@glo@type}% "main" for main glossary, "acronym" for list of acronyms
}{}{}
\makeatother

\newglossaryentry{electrolyte}{name=electrolyte,%
    description={solution able to conduct electric current}}

\begin{document}

Some text about \gls{electrolyte}.

The glossary starts on page~\pageref{main}.

\printglossaries

\end{document}
1
  • With the hyperref package you can use \autopageref{main} instead of page~\pageref{main}.
    – mhp
    Mar 28, 2012 at 8:38

3 Answers 3

6

I’d suggest the following simple solution based on redefining \glossarypreamble:

\documentclass{book}

\usepackage{hyperref}

\usepackage{glossaries}

\makeglossaries

\newglossaryentry{electrolyte}{name=electrolyte,
  description={solution able to conduct electric current}}

\renewcommand*{\glossarypreamble}{%
  \label{\currentglossary}%
}

\begin{document}

Some text about \gls{electrolyte}.

The glossary starts on \autopageref{main}.

\printglossaries

\end{document}

Alternatively, you could load the glossaries package with

\usepackage[numberedsection=autolabel]{glossaries}

In this case, the glossary sections are numbered, though.

4
  • Thanks mhp. This way the reference displays as "page XX". Is there a way to omit the word "page" and just display the page number?
    – mmm
    Jul 13, 2012 at 9:51
  • 1
    @mmm: Simply use \pageref instead of \autopageref.
    – mhp
    Jul 13, 2012 at 12:18
  • Thanks. I noticed I'm referencing the first glossary this way. I added two additional glossaries using \newglossary in my document - can those be referenced separately?
    – mmm
    Jul 13, 2012 at 12:37
  • 1
    @mmm: The label is added to each defined glossary. So, \pageref{acronym} etc. should work, too.
    – mhp
    Jul 13, 2012 at 12:58
6

You have to create a mark for referencing and this can be done by \phantomsection

\documentclass{book}
\usepackage{hyperref}
\usepackage{glossaries}
\makeglossaries
%-------------------------------------------------------
\newglossaryentry{electrolyte}{name=electrolyte,%
    description={solution able to conduct electric current}}
%-------------------------------------------------------
\begin{document}
Some text about \gls{electrolyte}.
The glossary starts on page~\pageref{glossaries}.
\cleardoublepage
\phantomsection <------------------- here
\label{glossaries}<-----------------and here
\printglossaries
%-------------------------------------------------------
\end{document}

enter image description here

The Glossary page looks like this:

enter image description here

3
  • For me, the actual glossary start at page 5 in your example.
    – lockstep
    Mar 28, 2012 at 6:24
  • @lockstep, I do not understand. I get it at page 3. Pl. see the picture.
    – user11232
    Mar 28, 2012 at 6:48
  • Whoops, sorry I'm pretty new to this. Your solution works, but I think I'm going to mark one of the others as "the answer" just because they allow you to reference the main glossary and the acronyms separately, so it seems a bit better. Unless I can select more than one green check mark? Thank you for your help.
    – debbie
    Mar 28, 2012 at 17:26
2

For a solution that works with hyperref and (as my original one) does without modifications in the document body, you have to patch \@p@glossarysection instead of \@glossarysection.

\documentclass{book}

\usepackage{hyperref}

\usepackage{glossaries}
\makeglossaries

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@p@glossarysection}{%
  \@@glossaryseclabel%
}{%
  \@@glossaryseclabel%
  \label{\glsautoprefix\@glo@type}% "main" for main glossary, "acronym" for list of acronyms
}{}{}
\makeatother

\newglossaryentry{electrolyte}{name=electrolyte,%
    description={solution able to conduct electric current}}

\begin{document}

Some text about \gls{electrolyte}.

The glossary starts on page~\pageref{main}.

\printglossaries

\end{document}
3
  • This works, thank you so much for the help. I'm going to mark mhp's answer as my "selected answer", just because it does the same thing but seems a little simpler. Unless I can select more than one answer. Thank you!
    – debbie
    Mar 28, 2012 at 17:27
  • @debbie It's up to you which answer to accept, but you can (and should) upvote all "helpful" answers -- note that upvoting is different from accepting.
    – lockstep
    Mar 28, 2012 at 17:31
  • OK, thanks for the tip. I'm pretty new to all this.
    – debbie
    Mar 28, 2012 at 17:33

You must log in to answer this question.

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