21

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?

4
  • Shall the glossary of a section (say) only contain items that are defined first in this section, or all items used in this section? (If the former, the glossaries are disjoint...)
    – mafp
    Jan 3, 2013 at 22:03
  • 1
    I'm seeking for a way to include only the items which is used in the particular section.
    – m0nhawk
    Jan 3, 2013 at 22:05
  • But a glossary item can be used in several sections, so shall it appear in all corresponding glossaries, or only in the first of them?
    – mafp
    Jan 3, 2013 at 22:08
  • 1
    @mafp: if it used - yes, it all corresponding.
    – m0nhawk
    Jan 4, 2013 at 8:04

1 Answer 1

17

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.

2
  • 1
    Hi, my gut feeling is that this redefinition of glossentry messes with the hyperlink, and that one should use \glstarget{##1}{\glossentryname{##1}} instead of just \glossentryname{##1} to have the link set properly. Am I right?
    – Clément
    Nov 12, 2016 at 22:03
  • @Clément In this case it's better to have a main glossary for the entire document and only include \glstarget in that, so any hyperlinks are directed to the main glossary. If \glstarget is included in the per chapter/section glossary it could result in duplicate targets if the same entry is used across difference chapters/sections. Nov 13, 2016 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .