[Tex/LaTex] Glossary per chapter (not by section)

glossariessubdividing

As in Glossary per chapter or section, I'm trying to use the glossaries package to create acronym lists but only by chapter, not by section.

I've tried to adapt the answer, but I don't manage to do it.

Here's a minimal example :

\documentclass{report}
\usepackage{datatool-base}
\usepackage[counter=chapter,xindy,section=section]{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*{\glsXchapterXglsnumberformat}[2]{##2}%
  \renewcommand*{\delimR}{,}%
  \renewcommand*{\glossaryentryfield}[5]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thechapter}{##5}}%
    \doifinlocation
    {%
      \item ##2 ##3%
    }%
  }%
}
\newcommand{\ifinlocation}[3]{%
 \DTLifinlist{#1}{#2}{#3}{}%
}
\def\striprelax\relax#1\endstriprelax{#1}
\setglossarystyle{mystyle}

\begin{document}
\chapter{Sample Chapter}
\printglossary
\begin{equation}
\gls{E} = \gls{m}\cdot \gls{c}^2
\end{equation}
\glsresetall 
\chapter{Another Chapter}
\printglossary
\begin{equation}
\gls{E} = \frac{\gls{m}\gls{v}^2}{2}
\end{equation}
\end{document}

The result looks like :
Output page 1 : Sample chapter
Output page 2 : Another chapter

How can I remove the glossary entry about velocity in the first chapter and the one about speed of light in the second chapter ?

Thanks in advance

Best Answer

The problem here is that you're mixing old and new commands. My original answer that you linked to is several years old and uses commands that are now deprecated, such as \glossarystyle and \glossaryentryfield. Your MWE has changed the deprecated \glossarystyle to \setglossarystyle but hasn't changed the deprecated \glossaryentryfield. By using the new \setglossarystyle, the backward-compatibility is removed and \glossaryentryfield isn't used.

To fix it, just replace \glossaryentryfield with the newer \glossentry:

  \renewcommand*{\glossentry}[2]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thechapter}{##2}}%
    \doifinlocation
    {%
      \item \glossentryname{##1} \glossentrydesc{##1}%
    }%
  }%

Complete MWE:

\documentclass{report}
\usepackage{datatool-base}
\usepackage[counter=chapter,xindy,section=section]{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*{\glsXchapterXglsnumberformat}[2]{##2}%
  \renewcommand*{\delimR}{,}%
  \renewcommand*{\glossentry}[2]{%
    \edef\doifinlocation{\noexpand\ifinlocation{\thechapter}{##2}}%
    \doifinlocation
    {%
      \item \glossentryname{##1} \glossentrydesc{##1}%
    }%
  }%
}
\newcommand{\ifinlocation}[3]{%
 \DTLifinlist{#1}{#2}{#3}{}%
}
\def\striprelax\relax#1\endstriprelax{#1}
\setglossarystyle{mystyle}

\begin{document}
\chapter{Sample Chapter}
\printglossary
\begin{equation}
\gls{E} = \gls{m}\cdot \gls{c}^2
\end{equation}
\glsresetall 
\chapter{Another Chapter}
\printglossary
\begin{equation}
\gls{E} = \frac{\gls{m}\gls{v}^2}{2}
\end{equation}
\end{document}

First chapter:

image of chapter 1

Second chapter:

image of second chapter

Related Question