Here's a possible solution using datagidx
. This works by inserting a chapter dependent prefix to the location list, and only listing the entries that have the appropriate prefix when using \printterms
. Within \printterms
the location list needs to be adjusted so that only those locations with the appropriate prefix are listed and, finally, the prefix needs to be removed before it's displayed.
Edit: Modified to list just first use location.
\documentclass{report}
\usepackage{etoolbox}
\usepackage{datagidx}
% Define a database of notations
\newgidx{notation}{Notation}
% Set this as the default database so we don't have to keep
% explicitly mentioning it
\DTLgidxSetDefaultDB{notation}
% Define some terms
\newterm[description={description for A}]{A}
\newterm[description={description for B}]{B}
\newterm[description={description for C}]{C}
% Fake a counter called "notation" to be used in the location list
% Two digits are used to make the substring comparison easier
% (used in the condition of \printterms)
\makeatletter
\newcommand*{\twodigits}[1]{\two@digits{\value{#1}}}
\makeatother
\newcommand*{\thenotation}{C\twodigits{chapter}.\thepage}
\renewcommand*{\DTLgidxCounter}{notation}
% Define a command to print the notation list
\newcommand{\printnotation}{%
\bgroup
\edef\locationprefix{C\twodigits{chapter}.}%
\printterms
[heading={\section*},% put the heading in an unnumbered section
% only list entries with this prefix in the location list:
condition={\DTLisSubString{\Location}{\locationprefix}},%
sort={},% suppress sorting - do it manually
location=first,% only show first location
columns=1% one column list - change as required
]%
\egroup
% Add a vertical gap so it's not right on top of the next paragraph
\vskip\baselineskip
% Set the after heading flag (remove if not required):
\csuse{@afterheading}%
% Something is confusing the rerun warnings, so disable it and
% just remember to use multiple LaTeX runs:
\csdef{@datagidx@dorerun@warn}{}%
}
% Patch \datagidx@formatlocation so that it strips the location prefix
% and inserts "p." before the location
\makeatletter
\let\orgformatlocation\datagidx@formatlocation
\renewcommand*{\datagidx@formatlocation}[2]{%
\expandafter\DTLsplitstring\expandafter{#2}{.}{\locprefix}{\locsuffix}%
\orgformatlocation{#1}{p.\locsuffix}%
}
\makeatother
% after all terms have been defined, so manually to avoid the
% redundancy of sorting each time the notation list is display.
\dtlsort{Sort}{notation}{\dtlwordindexcompare}
\begin{document}
\chapter{First Sample Chapter}
\printnotation
Referencing \gls{A}. Referencing \gls{B}.
\newpage
Referencing \gls{A} again.
\chapter{Second Sample Chapter}
\printnotation
Referencing \gls{A}. Referencing \gls{C}.
\newpage
Referencing \gls{C} again.
\end{document}
In the above example, "A" is used on pages 1 and 2 (in the first chapter) and 3 (in the second chapter. "B" is used on page (in the first chapter) and "C" is used on pages 3 and 4 (in the second chapter).
The notation list for the first chapter looks like:
The notation list for the second chapter looks like:
Two or three LaTeX runs are required.
Patch for pre v2.14:
\makeatletter
\newcommand*{\printterms@condition}{\boolean{true}}
\define@key{printterms}{condition}{\renewcommand*{\printterms@condition}{#1}}
\renewcommand{\DTLgidxForeachEntry}[1]{%
\def\datagidxprevgroup{}%
\edef\datagidx@doforeachentry{%
\noexpand\DTLforeach*[\expandonce\printterms@condition]{\DTLgidxCurrentdb}%
{\expandonce\DTLgidxAssignList}
}%
\datagidx@doforeachentry
{%
\DTLifnull{\Parent}%
{%
\DTLifnull\Location
{%
\DTLifnull\CurrentLocation
{%
}%
{%
\global\let\@datagidx@dorerun@warn\@data@rerun@warn
}%
}%
{%
\ifcsdef{datagidx@prev@loc@\Label}%
{%
\ifcsequal{datagidx@prev@loc@\Label}{CurrentLocation}%
{}%
{%
\global\let\@datagidx@dorerun@warn\@data@rerun@warn
}%
}%
{%
\global\let\@datagidx@dorerun@warn\@data@rerun@warn
}%
}%
\datagidx@doifdisplayed
{%
\edef\datagidx@dowrite{%
\noexpand\protected@write\noexpand\@auxout{}%
{%
\string\datagidx@save@loc{\Label}{\CurrentLocation}%
}%
}%
\datagidx@dowrite
\datagidx@level=1\relax
\expandafter\datagidx@getgroup\Sort{}\datagidx@endgetgroup
#1%
\global\let\datagidxprevgroup\datagidxcurrentgroup
}%
}%
{}%
}%
}
\makeatother
Glossary information is only written to the external files when commands like \gls
are used. Without a minimal working example, it's difficult to say for certain, but if you're using \include
then it may be possible that you're using a command like \includeonly
to only selectively include chapters. If the excluded files contain commands like \gls
they'll be skipped when the file is skipped, which means that they won't be written to the glossary file and no glossary will appear.
Here's an example. The main file test.tex
:
\documentclass{report}
\usepackage{glossaries}
\makeglossaries
\loadglsentries{example-glossaries-brief}
\begin{document}
\include{chapters/test-ch1}
\include{chapters/test-ch2}
\include{chapters/test-ch3}
\printglossaries
\end{document}
In the chapters
directory (folder) the files are:
test-ch1.tex
:
\chapter{Sample}
\gls{lorem}
test-ch2.tex
:
\chapter{Another}
\gls{dolor}
test-ch3.tex
:
\chapter{No Glossary Entries Here}
Now the following document build sequence:
pdflatex test
makeglossaries test
pdflatex test
will correctly generate the glossary. However, suppose I now add
\includeonly{chapters/test-ch3}
to the preamble. This excludes the two chapters that use \gls
and only includes the chapter that doesn't contain any \gls
(or similar) command. This means that nothing is written to the glossary. Now recompile (rebuild) the document:
pdflatex test
makeglossaries test
This produces an error message from makeglossaries
:
makeglossaries version 2.15 (2014-07-30)
added glossary type 'main' (glg,gls,glo)
Warning: File 'test.glo' is empty.
Have you used any entries defined in glossary 'main'?
Remember to use package option 'nomain' if you
don't want to use the main glossary.
This is because nothing has been written to test.glo
because all the commands that perform that write have been skipped.
What should you do? Well, if you're excluding chapters, that means you're still working on the document, in which case it doesn't matter. The document is already an incomplete draft since there are some chapters missing, so just skip the makeglossaries
step until you're ready to check the entire document rather than just the particular chapter you're working on. Once you go back to including the files that contain glossary references, then go back to building the glossary.
Edit: Another possibility is that you may be using an old version of makeglossaries
which didn't pick up \@input
from the .aux
file. This was corrected in makeglossaries
version 1.9 (distributed with glossaries
v2.07 2010/07/10).
Best Answer
As Marco says in his comment, this isn't good style. Your reader arrives at your List of Symbols expecting to look up a particular symbol in an alphabetical list. Splitting that list into several per-chapter lists makes their job harder if they don't know in which chapter the symbol was first defined.
Also, if you split your list into several per-chapter lists, your reader may not expect that "S" has different meanings in different chapters. They might see that "S" is "cell radius" in Chapter 1's list, and not bother to read further to find that it actually meant "area" in the chapter they were reading.
If you really have to assign different meanings to "S" in different chapters, then I think the simplest thing to do is to have a single list of symbols, and for the "S" entry to explicitly acknowledge the two meanings, like so: