[Tex/LaTex] Multiple parents for entries with `glossaries`

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?

Best Answer

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.

Related Question