# Multiple parents for entries with glossaries

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?

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[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.

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[style=index]{glossaries}

\makeglossaries

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

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

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

\csdef{#1.labels}{}%
\forcsvlist{\dochild{#3}{#1}}{#2}%
}

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

\forlistcsloop
{%
}%
{#2.labels}%
}

{%
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.

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[style=index]{glossaries}

\makeglossaries

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

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

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

\csdef{#1.labels}{}%
\forcsvlist{\dochild{#3}{#1}}{#2}%
}

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

\forlistcsloop
{%
}%
{#2.labels}%
}

{%
name={sample},%
description={an example}%
}

\begin{document}


• 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. – Yann Pequignot Jul 8 '13 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. – Nicola Talbot Jul 9 '13 at 7:30