5

I am using the package glossaries to produce an index. I am using sub-entriesand the field parentin my glossary entries definition. In order to ease the use of my index I would like some entries to be displayed several times and in different places. My first try was to simply declare in the glossary entry that the entry has severyl parents by setting

parent={key1,key2}

However it seems that this is not supported by the glossaries package. Is there any other way?

1

1 Answer 1

4

Multiple parents aren't supported by glossaries. If you have hyperlinks, there would be an ambiguity as to which glossary entry commands like \gls would link to. There are however ways to get around it.

Approach 1:

Define two entries with the same name but different parents and use the relevant one in the context of its given parent. This works best if you don't have a location list. Example:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[style=index,nonumberlist]{glossaries}

\makeglossaries

\newglossaryentry{parent1}
{%
  name={parent 1},%
  description={First parent}%
}

\newglossaryentry{parent2}
{%
  name={parent 2},%
  description={Second parent}%
}

\newglossaryentry{child1}
{%
  name={sample},
  description={an example},%
  parent={parent1}%
}

\newglossaryentry{child2}
{%
  name={sample},%
  description={an example},%
  parent={parent2}%
}

\begin{document}

Reference \gls{child1} in the context of \gls{parent1}.

Reference \gls{child2} in the context of \gls{parent2}.

\printglossaries

\end{document}

The first reference links to the entry under First parent. The second reference links to the entry under Second parent.

Image of resulting document

Approach 2:

This approach is more advanced. It stores a list of labels with which a child entry is associated and defines \mgls to iterate through the list using \glsadd for all except the final label for which it uses \gls. Example:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\usepackage{etoolbox}
\usepackage[colorlinks]{hyperref}
\usepackage[style=index]{glossaries}

\makeglossaries

\newglossaryentry{parent1}
{%
  name={parent 1},%
  description={First parent}%
}

\newglossaryentry{parent2}
{%
  name={parent 2},%
  description={Second parent}%
}

\newcommand{\dochild}[3]{%
  \listcsadd{#2.labels}{#3.#2}%
  \newglossaryentry{#3.#2}%
  {%
     #1,parent={#3}%
  }%
}

% syntax: \addchild{label}{parent list}{key=val list}
\newcommand*{\addchild}[3]{%
  \csdef{#1.labels}{}%
  \forcsvlist{\dochild{#3}{#1}}{#2}%
}

\newcommand*{\mgls}[2][]{%
  \def\glslastlabel{}%
  \forlistcsloop
  {%
    \ifdefempty{\glslastlabel}{}{\glsadd[#1]{\glslastlabel}}%
    \def\glslastlabel
  }%
  {#2.labels}%
  \gls[#1]{\glslastlabel}%
}

\newcommand*{\mglsadd}[2][]{%
  \forlistcsloop
  {%
    \glsadd[#1]%
  }%
  {#2.labels}%
}

\addchild{sample}{parent1,parent2}%
{%
  name={sample},%
  description={an example}%
}

\begin{document}

\mgls{sample}.

\printglossaries

\end{document}

Here \mgls will link to the entry under the last named parent used when the child entry was defined with \addchild.

Image of resulting document

If you would rather link to a different glossary entry, you can use \mglsadd and explicitly use \gls with the label in the form parent.child. Like this:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\usepackage{etoolbox}
\usepackage[colorlinks]{hyperref}
\usepackage[style=index]{glossaries}

\makeglossaries

\newglossaryentry{parent1}
{%
  name={parent 1},%
  description={First parent}%
}

\newglossaryentry{parent2}
{%
  name={parent 2},%
  description={Second parent}%
}

\newcommand{\dochild}[3]{%
  \listcsadd{#2.labels}{#3.#2}%
  \newglossaryentry{#3.#2}%
  {%
     #1,parent={#3}%
  }%
}

% syntax: \addchild{label}{parent list}{key=val list}
\newcommand*{\addchild}[3]{%
  \csdef{#1.labels}{}%
  \forcsvlist{\dochild{#3}{#1}}{#2}%
}

\newcommand*{\mgls}[2][]{%
  \def\glslastlabel{}%
  \forlistcsloop
  {%
    \ifdefempty{\glslastlabel}{}{\glsadd[#1]{\glslastlabel}}%
    \def\glslastlabel
  }%
  {#2.labels}%
  \gls[#1]{\glslastlabel}%
}

\newcommand*{\mglsadd}[2][]{%
  \forlistcsloop
  {%
    \glsadd[#1]%
  }%
  {#2.labels}%
}

\addchild{sample}{parent1,parent2}%
{%
  name={sample},%
  description={an example}%
}

\begin{document}

\mglsadd{sample}\gls{parent1.sample}.

\printglossaries

\end{document}

This produces the same looking result as previously, but now the sample entry links to the entry under the first parent instead of the second parent.

3
  • Thanks! I didn't find this idea of multiplying entry with same name but different parent in the user manual. Commented Jul 7, 2013 at 11:27
  • the second approach works fine for me! Still it seems to me that this feature could be added to the glossaries package to allow two and more parents and the use of the same command \gls or \glsadd. It looks to me it would be more in the spirit of the package. Commented Jul 8, 2013 at 13:58
  • @yann.pequignot You can log it as a feature request if you like. I only uploaded a new version of glossaries a few days ago, but if it's logged, I'll see it when I work on the next version. Commented Jul 9, 2013 at 7:30

You must log in to answer this question.

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