# Problem with inserting “page” in Glossary

I read this question to insert "Page" before the occurencing page of a Glossary Entry.

I tried the proposed solution, which rely on the following code :

\renewcommand{\glsnumberformat}[1]{%
(\ifthenelse{\DTLisSubString{#1}{\delimN}\OR\DTLisSubString{#1}{\delimR}}%
{pages #1}{page #1})}


I have acronyms in the frontmatter, mainmatter and backmatter. The problem is they all have different numbering :

• frontmatter : i, ii, iii, iv, ...
• mainmatter : 1, 2 3, ...
• backmatter : A1, A2, ...

It gives thus this :

Several problems appears :

1. When the same acronym appears in at least 2 different parts, their occurence in the acronym liste aren't together anymore

2. When using different commands, the "put-together" fails. I used \acrlong on page 1, and \acrfull on page 3...

3. The hyperlinks on the page numbers have dissapeared (while the ones in the text, referencing the talbe still appears)

4. When the acronym is in a title, the hyperlink in the toc to this part has dissapeard, and only remains the link to the acronym list

EDIT Here is a MWE, when compiled with LaTeX + Glossary + LaTeX + LaTeX (using TeX + DVI):

\documentclass[a4paper,11pt,oneside]{book}

pdfborder={0 0 0}]{hyperref}

% List of abbreviations
\usepackage[acronym,toc]{glossaries}

\makeglossaries
\glsnoexpandfields

% Enable entry counting
\glsenableentrycount

% Columns in acronym table
\newglossarystyle{mylong}{%
\setglossarystyle{long}%
\renewenvironment{theglossary}%
{\begin{longtable}[l]{@{}p{\dimexpr 2cm-\tabcolsep}p{0.8\hsize}}}% <-- change the value here
{\end{longtable}}%
\renewcommand{\glossentry}[2]{%
\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
\glossentrydesc{##1}\glspostdescription\space
\ifnum\glsentryprevcount{##1}=1\relax
Page
\else
Pages
\fi
##2\tabularnewline
}%
}

\newacronym{TLS}{TLS}{Transport Layer Security}
\newacronym{CLV}{CLV}{Some Acronym}

\begin{document}

\frontmatter

\printglossary[type=\acronymtype,style=mylong,title=Abbreviations]

\mainmatter

\chapter{Sample Chapter}

\section[\glsentryshort{TLS}]{\acrshort{TLS}}

\newpage
\subsection{Section}

\chapter{Other chapter}
\acrfull{TLS}

\acrfull{CLV}

\backmatter
\appendix
\setcounter{page}{1}
\renewcommand{\thepage}{A\arabic{page}}

\chapter{Sample Appendix}
\acrlong{TLS}

\end{document}


It shows that the acronym only used once is still referenced as pages 3, as proved by this screenshot :

• The linked answer (which I wrote) is quite old and uses some deprecated commands. I'd forgotten about that answer, but I wrote an answer to a more recent question that may be a better starting point. That one works by counting the number of times an entry has been used to determine whether to use p. or pp.. – Nicola Talbot Mar 18 '16 at 13:59
• @NTalbot : I tried it, but as my numbering is different than simply figures, it screws everything... – EisenHeim Mar 18 '16 at 14:29
• That shouldn't make a difference with the entry-counting answer I linked to. It doesn't try parsing the location list, it just counts the number of times each entry is indexed. – Nicola Talbot Mar 18 '16 at 14:42
• I'll see if I can work out what's going wrong. Number 4 on your list is a separate question but is caused by nested hyperlinks (see Why shouldn't I use commands like \gls in \section, \chapter, \caption etc?). – Nicola Talbot Mar 22 '16 at 9:46
• I've edited my answer. – Nicola Talbot Mar 23 '16 at 10:28

## Method 1:

(I've simplified your example a little.) Rather than redefining \glsnumberformat, I think it's better to make the modification to your new glossary style:

\documentclass[a4paper,11pt,oneside]{book}

pdfborder={0 0 0}]{hyperref}

% List of abbreviations
\usepackage[acronym,toc]{glossaries}

\makeglossaries
\glsnoexpandfields

% Enable entry counting
\glsenableentrycount

% Columns in acronym table
\newglossarystyle{mylong}{%
\setglossarystyle{long}%
\renewenvironment{theglossary}%
{\begin{longtable}[l]{@{}p{\dimexpr 2cm-\tabcolsep}p{0.8\hsize}}}% <-- change the value here
{\end{longtable}}%
\renewcommand{\glossentry}[2]{%
\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
\glossentrydesc{##1}\glspostdescription\space
\ifnum\glsentryprevcount{##1}=1\relax
page
\else
pages
\fi
##2\tabularnewline
}%
}

\newacronym{TLS}{TLS}{Transport Layer Security}
\newacronym{aa}{AA}{Another Abbreviation}

\begin{document}

\frontmatter

\tableofcontents

\printglossary[type=\acronymtype,style=mylong,title=Abbreviations]

\mainmatter

\chapter{Sample Chapter}

\section[\glsentryshort{TLS}]{\acrshort{TLS}}

\gls{aa}.

\newpage
\subsection{Section}

\chapter{Other chapter}
\acrfull{TLS}

\backmatter
\appendix
\setcounter{page}{1}
\renewcommand{\thepage}{A\arabic{page}}

\chapter{Sample Appendix}
\acrlong{TLS}

\end{document}


Using latex, makeglossaries, latex, latex (or pdflatex, makeglossaries, pdflatex, pdflatex) this produces

You can change page/pages to Page/Pages or suppress the post description dot using the nopostdot package option, if required.

Things that will cause this method to fail:

• Resetting the entries: this will reset the counter for the entry back to 0
• Using an entries more than once on one page but not on any other. For example, if \gls{aa} is used three times on page 1 but not on any other page, then the location list will just consist of page 1 but because the entry has been used three times the conditional \ifnum\glsentryprevcount{##1}=1 will be false.
• The entry counting is intended for counting the number of times an entry has been marked as used. This means that the count is only incremented whenever \glsunset is used. This is done internally by commands like \gls, but not for commands like \acrfull, so this method will only be useful if you just use the \gls-like commands.

## Method 2:

As in the answer you linked to in your question, this second method assumes that the presence of \delimN or \delimR in the location list means that there's more than one page in the list. The other answer tries to parse the location list for instances of those commands, but this is tricky as they may be enclosed in an argument. This alternative approach defines a new conditional \ifpages and redefines \delimN and \delimR to switch it to true. This of course means that we only know the required information after the location list has been typeset, so the information needs to be written to the auxiliary file so it can be picked up on the next (pdf|xe|lua)latex run.

\documentclass[a4paper,11pt,oneside]{book}

pdfborder={0 0 0}]{hyperref}

% List of abbreviations
\usepackage[acronym,toc]{glossaries}

\makeglossaries
\glsnoexpandfields

\newif\ifpages

\renewcommand*{\delimN}{\pagestrue, }
\renewcommand*{\delimR}{\pagestrue--}

\makeatletter
\newcommand*{\savepagestag}[2]{%
\protected@write\@auxout{}{\string\pages@tag{#1}{#2}}}
\newcommand*{\pages@tag}[2]{\csgdef{pages@tag@#1}{#2}}
\newcommand*{\usepagestag}[1]{\csuse{pages@tag@#1}}
\makeatother

% Columns in acronym table
\newglossarystyle{mylong}{%
\setglossarystyle{long}%
\renewenvironment{theglossary}%
{\begin{longtable}[l]{@{}p{\dimexpr 2cm-\tabcolsep}p{0.8\hsize}}}%
{\end{longtable}}%
\renewcommand{\glossentry}[2]{%
\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
\glossentrydesc{##1}\glspostdescription\space
\pagesfalse
\usepagestag{##1}\space
##2%
\ifpages
\savepagestag{##1}{pages}%
\else
\savepagestag{##1}{page}%
\fi
\tabularnewline
}%
}

\newacronym{TLS}{TLS}{Transport Layer Security}
\newacronym{CLV}{CLV}{Some Acronym}

\begin{document}

\frontmatter

\printglossary[type=\acronymtype,style=mylong,title=Abbreviations]

\mainmatter

\chapter{Sample Chapter}

\section[\glsentryshort{TLS}]{\acrshort{TLS}}

\newpage
\subsection{Section}

\chapter{Other chapter}
\acrfull{TLS}

\acrfull{CLV}

\backmatter
\appendix
\setcounter{page}{1}
\renewcommand{\thepage}{A\arabic{page}}

\chapter{Sample Appendix}
\acrlong{TLS}

\end{document}


## Method 3

This method uses the glossaries-extra extension package:

\documentclass[a4paper,11pt,oneside]{book}

% List of abbreviations
\usepackage[acronym,postdot]{glossaries-extra}

\makeglossaries

\setabbreviationstyle[acronym]{long-short}

\newacronym{TLS}{TLS}{Transport Layer Security}
\newacronym{aa}{AA}{Another Abbreviation}

\begin{document}

\frontmatter

\tableofcontents

\printglossary[type=\acronymtype,style=long,title=Abbreviations]

\mainmatter

\chapter{Sample Chapter}

\section{\glsfmtshort{TLS}}

\gls{aa}.

\newpage
\subsection{Section}

\chapter{Other chapter}
\glsxtrfull{TLS}

\backmatter
\appendix
\setcounter{page}{1}
\renewcommand{\thepage}{A\arabic{page}}

\chapter{Sample Appendix}
\glsxtrlong{TLS}

\end{document}


• Your MWE (thanks btw) doesn't work if I had another acronym, which apprears only one. It displays pages for the two... – EisenHeim Mar 22 '16 at 11:16
• @EisenHeim It works fine for me if I add another example abbreviation but it requires two pdflatex calls. On the first run the previous count for the entry is 0 (since it wasn't used in the previous run), so the \else part is used. – Nicola Talbot Mar 22 '16 at 15:00
• I compile the document with TeX+DVI (because I need it in my main. But if I try with pdflatexas you suggest, with 2 (or more) runs, I still got the error. I have pages on both acronyms... – EisenHeim Mar 22 '16 at 15:09
• @EisenHeim Sorry, I'm in the habit of writing pdflatex but I meant pdflatex or latex. Whichever one you use needs to be used twice. Have you used the second acronym just once? If it occurs, say, twice on one page but not on any other page it will cause a problem. Unsetting the acronym flag will also affect the count. Can you edit your question with an example? – Nicola Talbot Mar 22 '16 at 15:16
• Cf my updated MWE – EisenHeim Mar 22 '16 at 15:37