[Tex/LaTex] Glossary per chapter or section

glossariessubdividing

I'm looking for a way of creating glossaries, with the help of glossaries package, per chapter or section.

In a particular glossary for section should be only the terms used in this section. If term is used in multiple section – appear in correspondent glossaries.

Example (done with the help of description environment, just for an example):

enter image description here

How can I achieve this?

Best Answer

Since you don't seem interested in the location list, you could change the location counter to section and define a glossary style that checks if the current section is in the location list. You'll probably want to neaten the glossary, but here's an example:¹

\documentclass{article}

\usepackage{datatool-base}
\usepackage[counter=section,xindy]{glossaries}

\GlsSetXdyMinRangeLength{0}

\makeglossaries

\newglossaryentry{E}{name={\ensuremath{E}},
 description={energy}}

\newglossaryentry{m}{name={\ensuremath{m}},
  description={mass}}

\newglossaryentry{c}{name={\ensuremath{c}},
  description={speed of light}}

\newglossaryentry{v}{name={\ensuremath{v}},
  description=velocity}

\newglossarystyle{mystyle}%
{%
  \setglossarystyle{list}%
  \renewcommand*{\glossaryentrynumbers}[1]{\striprelax##1\endstriprelax}%
  \renewcommand*{\glsXsectionXglsnumberformat}[2]{##2}%
  \renewcommand*{\delimR}{,}%
  \renewcommand*{\glossentry}[2]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thesection}{##2}}%
    \doifinlocation
    {%
      \item \glossentryname{##1} \glossentrydesc{##1}%
    }%
  }%
}

% \ifinlocation{number}{location list}{body}

\newcommand{\ifinlocation}[3]{%
 \DTLifinlist{#1}{#2}{#3}{}%
}

\def\striprelax\relax#1\endstriprelax{#1}

\setglossarystyle{mystyle}

\begin{document}

\section{Sample Section}

\printglossary

\begin{equation}
\gls{E} = \gls{m}\cdot \gls{c}^2
\end{equation}

\section{Another Section}

\printglossary

\begin{equation}
\gls{E} = \frac{\gls{m}\gls{v}^2}{2}
\end{equation}

\end{document}

Note that this uses xindy rather than makeindex to suppress the range formation, which makes it easier to test if the current section is in the list.

The result looks like:

image of document

Here's an alternative approach that uses bib2gls with the glossaries-extra extension package. The entries are now defined in a .bib file, for example syms.bib:

@symbol{E,
 name={\ensuremath{E}},
 description={energy}
}

@symbol{m,
  name={\ensuremath{m}},
  description={mass}
}

@symbol{c,
  name={\ensuremath{c}},
  description={speed of light}
}

@symbol{v,
  name={\ensuremath{v}},
  description={velocity}
}

The document code now looks like:

\documentclass{article}

\usepackage[record]{glossaries-extra}

\GlsXtrRecordCounter{section}

\GlsXtrLoadResources[
  src={syms}, % entries defined in syms.bib
]

\newcommand{\printsectionglossary}[1][]{%
  \printunsrtglossary*[nonumberlist,#1]{%
    \renewcommand{\printunsrtglossaryhandler}[1]{%
     \glsxtrfieldxifinlist{##1}{record.section}{\thesection}
     {\glsxtrunsrtdo{##1}}%
     {}%
    }%
  }%
}

\begin{document}

\section{Sample Section}

\printsectionglossary

\begin{equation}
\gls{E} = \gls{m}\cdot \gls{c}^2
\end{equation}

\section{Another Section}

\printsectionglossary

\begin{equation}
\gls{E} = \frac{\gls{m}\gls{v}^2}{2}
\end{equation}

\end{document}

The result is the same as before. If you want to use the hyperref package then you need to make a minor adjustment to prevent duplicate target names:

\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[record]{glossaries-extra}

\GlsXtrRecordCounter{section}

\GlsXtrLoadResources[
  src={syms}, % entries defined in syms.bib
]

\newcommand{\printsectionglossary}[1][]{%
  \printunsrtglossary*[nonumberlist,#1]{%
    \renewcommand{\printunsrtglossaryhandler}[1]{%
    \glsxtrfieldxifinlist{##1}{record.section}{\thesection}
    {\glsxtrunsrtdo{##1}}%
    {}%
   }%
   \ifcsundef{theHsection}%
   {%
     \setkeys{printgloss}{targetnameprefix={record.\csuse{thesection}.}}%
   }%
   {%
     \setkeys{printgloss}{targetnameprefix={record.\csuse{theHsection}.}}%
   }%
  }%
}

\begin{document}

\section{Sample Section}

\printsectionglossary

\begin{equation}
\gls{E} = \gls{m}\cdot \gls{c}^2
\end{equation}

\section{Another Section}

\printsectionglossary

\begin{equation}
\gls{E} = \frac{\gls{m}\gls{v}^2}{2}
\end{equation}

\printunsrtglossary
\end{document}

This makes \gls link to the main (full) glossary at the end of the document.

image of document


¹ The original answer used some now deprecated commands \glossarystyle (now \setglossarystyle) and \glossaryentryfield:

\newglossarystyle{mystyle}%
{%
  \glossarystyle{list}%
  \renewcommand*{\glossaryentrynumbers}[1]{\striprelax##1\endstriprelax}%
  \renewcommand*{\glsXsectionXglsnumberformat}[2]{##2}%
  \renewcommand*{\delimR}{,}%
  \renewcommand*{\glossaryentryfield}[5]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thesection}{##5}}%
    \doifinlocation
    {%
      \item ##2 ##3%
    }%
  }%
}

These should not be used with v4.0 onwards.

Related Question