6

I am currently writing a large document (book class divided into several parts) and would like to introduce suitable notation tables at the beginning of each part of the document.

Up to now I have been looking at the documentation of some (standard?) references for making indexes and notation tables such as the package glossaries or the package nomencl, but I have not found any indication for automatically generating such notation tables at the beginning of each part or chapter of the text. In particular I am looking for a way to print the list of the relevant symbols used in each part/chapter at the beginning of the same chapter/part possibly repeating symbols already used in previous parts.

The only solution which came to my mind is to manually defining different glossaries for each part/chapter within the glossaries package. However this could be time consuming and not very portable and might lead to issues when cross.referencing for definitions. Is there any better and more flexible solution?

Example of output which I am trying to obtain

Chapter 1

Notation

A .. p.1

B .. p.2

Text containing first occurrence of symbol A and B

Chapter 2

Notation

A ... p.1

C ... p.11

Text containing first occurrence of symbol C and a tagged occurrence of symbol A

lockstep
  • 250,273
Stefano
  • 163

1 Answers1

3

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:

Image of first notation list

The notation list for the second chapter looks like:

Image of second notation list

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
Nicola Talbot
  • 41,153
  • Thank you very much for this solution, which fits almost perfectly with my question. I have just two dubts.

    When compiling I get the error

    ! Package xkeyval Error: 'condition' undefined in families 'printterms'.,

    which I do not fully understand: could it depend on the LaTeX distribution (MikTeX 2.9) I am using (I could not double-check with TeXLive today).

    Moreover, I was looking to a solution, which would print only the page number of the first (tagged) occurrence of the symbol in the document (and not only in the current chapter) and I do not see how to modify your code to get this.

    – Stefano Jul 01 '13 at 23:30
  • May I ask you some further hint on this? – Stefano Jul 01 '13 at 23:36
  • @Stefano Sorry, I forgot 'condition' is a new key defined in v2.14 which I uploaded to CTAN the other day. Hopefully it should make its way into the catalogue soon. I'll adjust the code with regard to your other query. – Nicola Talbot Jul 02 '13 at 08:08
  • @Stefano I've updated the example and added a patch for pre v2.14. – Nicola Talbot Jul 02 '13 at 08:55
  • Dear @Nicola Talbot, thank you very much! This works wonderfully! I just upvoted and accepted your answer: thanks! I will keep you updated with the effects of v.2.14 of datagidx, if any. – Stefano Jul 02 '13 at 15:05