The glossaries
package already provides a mechanism for counting the number of times an entry has been used, but it must be enabled using \glsenableentrycount
. This was added to version 4.14 and is documented in section 14.1 of the user manual.
Example:
\documentclass{article}
\usepackage{glossaries}
\makeglossaries
\glsenableentrycount
\newglossaryentry{sample}{name={sample},description={an example}}
\newacronym{ab}{AB}{sample abbreviation}
\begin{document}
Number of times ``sample'' was used on the last run:
\glsentryprevcount{sample}.
\gls{sample}. \gls{sample}. \gls{ab}.
Number of times ``sample'' has been used in this run \emph{so far}:
\glsentrycurrcount{sample}.
\newpage
\gls{sample}. \gls{ab}.
\newpage
\printglossaries
\end{document}
This produces (after a second run):
Notes:
\glsenableentrycount
must be used before you define your entries.
- This mechanism imposes a preamble-only restriction on the use of
\newglossaryentry
(and, therefore, also \newacronym
which internally uses \newglossaryentry
).
You can then use \glsentryprevcount
in your glossary style:
\newglossarystyle{mylist}
{%
\setglossarystyle{list}%
\renewcommand*{\glossentry}[2]{%
\item[\glsentryitem{##1}%
\glstarget{##1}{\glossentryname{##1}}]
\glossentrydesc{##1}\glspostdescription\space
\ifnum\glsentryprevcount{##1}>1 pp.\else p.\fi\ ##2}%
}
Take care as the previous count value represents the total number of times the entry has been used. This value is reset back to zero with \glsreset
and it also doesn't necessarily match the number of locations in the location list. For example, if an entry is used twice on one page but nowhere else in the document, then the location list will have a single location (that page) but the entry count will be 2.
The (currently experimental) glossaries-extra package enhances this feature and provides per-unit counting.
Edit:
Just using glossaries
:
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks]{hyperref}
\usepackage[toc,acronym,xindy,section=section]{glossaries}
\newglossarystyle{mylist}{%
\setglossarystyle{list}% base this style on the list style
\renewcommand*{\glossentry}[2]{%
\item[\glsentryitem{##1}%
\glstarget{##1}{\glossentryname{##1}}]
\glossentrydesc{##1}\glspostdescription\space
\ifnum\glsentryprevcount{##1}>1 pp.\else p.\fi\ ##2}%
}
\newglossary[ntg]{notation}{not}{ntn}{Glossaire}
\newglossary[slg]{symbols}{sym}{sbl}{Nomenclature}
\makeglossaries %
\glsenableentrycount
\newglossaryentry{glscard}{
name=cardinality,
description={The number of elements in the specified set}}
\newglossaryentry{mesh}{type=notation,
name={Mesh},
description={maillage},
sort={m}}
\newglossaryentry{mbb}{type=symbols,
name={\ensuremath{M}},
text={M},
description={matrix},
sort=m,
see=[see also]{glscard}}
\newacronym{pc}{PC}{personal computer}
\setglossarystyle{mylist}
\begin{document}
\printglossary[toctitle=Lexique,type=main]
\printglossary[toctitle=Acronyms,type=acronym]
\printglossary[type=notation]
\printglossary[type=symbols]
\chapter{Introduction}
\section{ab}
\gls{pc}; \gls{pc}; \gls{glscard}; \gls{mbb}; \gls{mesh}; \newpage
\gls{mesh}
\end{document}
This produces:
This mostly works except for the pc
entry which occurs twice on page 2 but not on any other page. Since the total usage for that entry is greater than 1, pp
is used instead of p
even though there's only a single page in the location list. Below is an alternative solution that uses glossaries-extra
.
Caveat: glossaries-extra
is still experimental and this may fail in the odd occasions where entries occur in a page-break spanning paragraph due to TeX's asynchronous output routine.
The following adapts your mwe to use glossaries-extra
:
% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[colorlinks]{hyperref}
\usepackage[nopostdot=false,% put a dot after the descriptions
abbreviations,% create a separate list of abbreviations
xindy,section=section]{glossaries-extra}
\GlsXtrEnableEntryUnitCounting
{general,abbreviation}% apply to both general and abbreviation categories
{0}% don't trigger index suppression
{page}% count per page
\newglossarystyle{mylist}{%
\setglossarystyle{list}% base this style on the list style
\renewcommand*{\glossentry}[2]{%
\item[\glsentryitem{##1}%
\glstarget{##1}{\glossentryname{##1}}]
\glossentrydesc{##1}\glspostdescription\space
\ifnum\glsentryprevtotalcount{##1}>1 pp.\else p.\fi\ ##2}%
}
% In case an entry is used two or more times on one page but not on any of the
% other pages.
\let\ORGglsxtrpostunset\glsxtrpostunset
\renewcommand*{\glsxtrpostunset}[1]{%
\ifnum\glsentrycurrcount{#1}>0 \else\ORGglsxtrpostunset{#1}\fi
}
\newglossary[ntg]{notation}{not}{ntn}{Glossaire}
\newglossary[slg]{symbols}{sym}{sbl}{Nomenclature}
\makeglossaries
% general entries
\newglossaryentry{glscard}{
name=cardinality,
description={The number of elements in the specified set}}
\newglossaryentry{mesh}{type=notation,
name={Mesh},
description={maillage},
sort={m}}
\newglossaryentry{mbb}{type=symbols,
name={\ensuremath{M}},
text={M},
description={matrix},
sort=m,
see=[see also]{glscard}}
% abbreviations
\newabbreviation{pc}{PC}{personal computer}
\setglossarystyle{mylist}
\begin{document}
\printglossary[toctitle=Lexique,type=main]
\printabbreviations
\printglossary[type=notation]
\printglossary[type=symbols]
\chapter{Introduction}
\section{ab}
\gls{pc}; \gls{pc}; \gls{glscard}; \gls{mbb}; \gls{mesh}; \newpage
\gls{mesh}
\end{document}
This produces:
Notes:
- Although the
glossaries-extra
package internally loads the glossaries
package, it sets different defaults (e.g. toc
and nopostdot
). To illustrate this I've removed the toc
option and added nopostdot
to emulate the settings of your mwe.
- The
glossaries-extra
package has a different (and better) way of dealing with abbreviations.
- With the per unit counting,
\glsentrycurrcount
and \glsentryprevcount
refer to the count for the current unit. So if you have, say, \glsentryprevcount{pc}
on page 1, you'll get the number of times the entry was used on page 1 (where the unit counter is page
). This means that it's of no use in the glossary style, since you're not interested in how many times the entry has been used in the glossary (which happens to be 0).
- The total count (from the previous run) is obtained with
\glsentryprevtotalcount
so the style tests if this value is greater than one.
- To cater for the possibility of an entry being used two or more times on one page of the document but not used at all on any other page, I've adjusted the definition of
\glsxtrpostunset
. For example, I've used pc
twice on one page but nowhere else, which would've set the total count to 2, and "pp" would've appeared in the glossary for the pc
entry (as in the previous example). This modification effectively sets a maximum count of 1 per page.
I can handle this by adding a sort={...} to each glossary entry, but
since I have a lot of entries, I don't want to add this everytime. Is
there another solution for this?
The simplest solution is to sort by definition or sort by order of use:
\printnoidxglossary[type=symbolslist,sort=def]% order of definition
or
\printnoidxglossary[type=symbolslist,sort=use]% order of use
Another method is to define your own sort handler that will strip the maths commands. For example:
\documentclass{article}
\usepackage{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries
\makeatletter
\newcommand*{\@glo@sortmacro@math}[1]{%
\@glo@sortentries{\@glo@sorthandler@math}{#1}%
}
\newcommand*{\@glo@sorthandler@math}[2]{%
\begingroup
\cslet{ensuremath }{\@firstofone}%
\cslet{mathbb }{\@firstofone}%
\let\protect\empty
\edef\sortA{\glsentrysort{#1}}%
\edef\sortB{\glsentrysort{#2}}%
\edef\x{%
\endgroup
\noexpand\dtlicompare{\noexpand\dtl@sortresult}%
{\expandonce\sortB}%
{\expandonce\sortA}%
}%
\x
}
\makeatother
\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}
\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist,sort=math]
\end{document}
Any other commands that occur in the name
will similarly need to be temporarily redefined. Note the spaces in
\cslet{ensuremath }{\@firstofone}%
\cslet{mathbb }{\@firstofone}%
This is because the sort
key is in the form:
\protect \ensuremath {\protect \mathbb {N}}
The commands may look like \ensuremath
and \mathbb
, but they're actually \ensuremath
and \mathbb
(trailing space is part of the command name).
Edit: Another method is to use the sanitizesort
package option. This will convert command names like \mathbb
into the sequence of characters \
m
a
t
h
b
b
, so it will behave much like makeindex
.
\documentclass{article}
\usepackage[sanitizesort]{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries
\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}
\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist]
\end{document}
The difference in these methods can be observed by adding a few extra sample entries:
Using sanitizesort
:
\documentclass{article}
\usepackage[sanitizesort]{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries
\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}
\newglossaryentry{N}{name={\ensuremath{N}},description={sample},
type=symbolslist
}
\newglossaryentry{M}{name={\ensuremath{M}},description={sample},
type=symbolslist
}
\newglossaryentry{Z}{name={\ensuremath{Z}},description={sample},
type=symbolslist
}
\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist]
\end{document}
Using custom sort:
\documentclass{article}
\usepackage{glossaries}
\usepackage{amssymb}
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makenoidxglossaries
\makeatletter
\newcommand*{\@glo@sortmacro@math}[1]{%
\@glo@sortentries{\@glo@sorthandler@math}{#1}%
}
\newcommand*{\@glo@sorthandler@math}[2]{%
\begingroup
\cslet{ensuremath }{\@firstofone}%
\cslet{mathbb }{\@firstofone}%
\let\protect\empty
\edef\sortA{\glsentrysort{#1}}%
\edef\sortB{\glsentrysort{#2}}%
\edef\x{%
\endgroup
\noexpand\dtlicompare{\noexpand\dtl@sortresult}%
{\expandonce\sortB}%
{\expandonce\sortA}%
}%
\x
}
\makeatother
\newglossaryentry{natnumb}{
name={\ensuremath{\mathbb{N}}},
description={The natural numbers $\mathbb{N} = \lbrace 1,2,3,\ldots
\rbrace$.},
type=symbolslist
}
\newglossaryentry{natnumbzero}{
name={\ensuremath{\mathbb{N}_0}},
description={The natural nubers with $0$, $\mathbb{N}_0 = \mathbb{N}
\cup \lbrace 0 \rbrace$.},
type=symbolslist
}
\newglossaryentry{N}{name={\ensuremath{N}},description={sample},
type=symbolslist
}
\newglossaryentry{M}{name={\ensuremath{M}},description={sample},
type=symbolslist
}
\newglossaryentry{Z}{name={\ensuremath{Z}},description={sample},
type=symbolslist
}
\begin{document}
Text
%
\glsaddall
\printnoidxglossary[type=symbolslist,sort=math]
\end{document}
Best Answer
Multiple parents aren't supported by
glossaries
. If you have hyperlinks, there would be an ambiguity as to which glossary entry commands like\gls
would link to. There are however ways to get around it.Approach 1:
Define two entries with the same name but different parents and use the relevant one in the context of its given parent. This works best if you don't have a location list. Example:
The first reference links to the entry under
First parent
. The second reference links to the entry underSecond parent
.Approach 2:
This approach is more advanced. It stores a list of labels with which a child entry is associated and defines
\mgls
to iterate through the list using\glsadd
for all except the final label for which it uses\gls
. Example:Here
\mgls
will link to the entry under the last named parent used when the child entry was defined with\addchild
.If you would rather link to a different glossary entry, you can use
\mglsadd
and explicitly use\gls
with the label in the form parent.
child. Like this:This produces the same looking result as previously, but now the sample entry links to the entry under the first parent instead of the second parent.