Glossaries: suppress pages when using glsaddall

Here is an approach that works with only a minor restriction: Instead of issuing \gladdall in the beginning, we issue a \glsaddallunused right before we do \printglossaries. This new command \glsaddallunused will write all entries that have not been used up to this point to the glossaries, with suppressed page numbers. For the suppression we will use lockstep's answer that you linked to.

If we were to write all entries with \glsaddall to the auxiliary glossary file, we would have a hard time to teach makeindex which page numbers to suppress, and how to sort and compress that correctly. In my approach, we only have entries with all page numbers shown (those explicitly referenced), and entries with completely suppressed page list.

\documentclass{article}

\usepackage{amsfonts}
\newcommand*{\Z}{\mathbb{Z}}
\newcommand*{\Q}{\mathbb{Q}}

\usepackage{lipsum} % to have some dummy text, and therefore larger page numbers
\usepackage[colorlinks]{hyperref}
\usepackage[
  style=long3colheader,
  hyperfirst=false
  ]{glossaries}

\newcommand*{\glsgobblenumber}[1]{}
% \renewcommand*{\glsgobblenumber}[1]{#1}% uncomment for testing

\makeatletter
%% lockstep's code
\newcommand*{\glsaddnp}[2][]{%
  \glsdoifexists{#2}%
  {%
%     \def\@glsnumberformat{glsnumberformat}% DELETED
    \def\@glsnumberformat{glsgobblenumber}% NEW
    \edef\@gls@counter{\csname glo@#2@counter\endcsname}%
    \setkeys{glossadd}{#1}%
    \@gls@saveentrycounter
    \@do@wrglossary{#2}%
  }%
}
%% new code; modified \glsaddall
\newcommand{\glsaddallunused}[1][]{%
  \edef\@glo@type{\@glo@types}%
  \setkeys{glossadd}{#1}%
  \forallglsentries[\@glo@type]{\@glo@entry}{%
    \ifglsused{\@glo@entry}{}{%
     \glsaddnp[#1]{\@glo@entry}}}%
}
\makeatother


\newglossaryentry{integers}{name=\ensuremath{\Z},
  description={the ring of integers}, sort=Z}

\newglossaryentry{rationals}{name=\ensuremath{\Q},
  description={the field of rational numbers}, sort=Q, nonumberlist}

\newglossaryentry{vector-space}{name=\ensuremath{V},
  description={a vector space}, sort=V}

\makeglossaries

\begin{document}
A vector space \gls{vector-space} \ldots
\lipsum[1-10]

\glsaddallunused
\printglossaries
\end{document}

sample output


Here's an alternative that doesn't use internal commands:

\documentclass{article}

\usepackage{amsfonts}
\newcommand*{\Z}{\mathbb{Z}}
\newcommand*{\Q}{\mathbb{Q}}

\usepackage{hyperref}
\usepackage[
  style=long3colheader,
  hyperfirst=false
  ]{glossaries}

\newcommand{\ignore}[1]{}

\newglossaryentry{integers}{name=\ensuremath{\Z},
  description={the ring of integers}, sort=!}
\newglossaryentry{rationals}{name=\ensuremath{\Q},
  description={the field of rational numbers}, sort=!, nonumberlist}

\newglossaryentry{vector-space}{name=\ensuremath{V},
  description={a vector space}, sort=V}

\makeglossaries

\begin{document}

A vector space \gls{vector-space} \ldots

\printglossaries


\forallglsentries{\thislabel}%
{%
  \ifglsused{\thislabel}{}{\glsadd[format=ignore]{\thislabel}}%
}

\end{document}

Result:

Image of glossary

Tags:

Glossaries