Gallery: Cross-References

image of glossary where some entries have a “see also” list after the description
The glossaries user manual suggests a number of methods for cross-referencing entries in the Cross-Referencing Entries section, but here’s another method that’s an adaptation of the citations in glossary sample. Here, instead of using the see key, I’m going to use the user1 key. This now gives me the flexibility to place the cross-references wherever I like by defining a custom glossary style, instead of having to put up with wherever xindy or makeindex puts the cross-references. This method also works if the number list needs to be suppressed.

As with the citation example, this custom style is based on the list style (\setglossarystyle{list}) but it’s again modified to check if the useri field is set (useri is the internal field name corresponding to the user1 key). To allow for the possibility of a comma-separated list of cross-references, the value of the useri field is fetched and stored in a control sequence using \glsletentryfield which is then expanded before being processed by \glsseelist:

    \ifglshasfield{useri}{##1}{\glspar
     \glsletentryfield{\thisref}{##1}{useri}%
     See also \expandafter\glsseelist\expandafter{\thisref}.}{}%
    }%

Note that if any of the cross-references aren’t actually used in the document, \glsseelist doesn’t add them to the glossary. This will cause undefined hypertargets. This can be fixed by redefining \glsseeitem so that it uses \glstext instead of \glshyperlink. Since \glsseeitem is robust, we need to use etoolbox’s \renewrobustcmd.

\renewrobustcmd{\glsseeitem}[1]{\glstext{#1}}

I’ve also defined a command called \crossrefentry that can be used in the document, if required:

\newcommand*{\crossrefentry}[1]{%
 \glsdoifexists{#1}%
 {%
  \glsletentryfield{\thisref}{#1}{useri}%
  \expandafter\glsseelist\expandafter{\thisref}%
 }%
}

Remember that if the cross-referenced term only appears in the glossary, you’ll need an extra makeglossaries call.

The initial comment lines below are arara directives. You can remove them if you don’t use arara.

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

\usepackage[colorlinks]{hyperref}
\usepackage[nopostdot,toc]{glossaries}

\makeglossaries

\loadglsentries{example-glossaries-seealso}

\newglossarystyle{crossref}
{% based on list style
  \setglossarystyle{list}%
    \renewcommand*{\glossentry}[2]{%
    \item[\glsentryitem{##1}%
          \glstarget{##1}{\glossentryname{##1}}]
       \glossentrydesc{##1}\glspostdescription\space ##2
    \ifglshasfield{useri}{##1}{\glspar
     \glsletentryfield{\thisref}{##1}{useri}%
     See also \expandafter\glsseelist\expandafter{\thisref}.}{}%
    }%
}

\renewrobustcmd{\glsseeitem}[1]{\glstext{#1}}

\newcommand*{\crossrefentry}[1]{%
 \glsdoifexists{#1}%
 {%
  \glsletentryfield{\thisref}{#1}{useri}%
  \expandafter\glsseelist\expandafter{\thisref}%
 }%
}

\begin{document}

\chapter{Sample}

\forglsentries{\thislabel}{\gls{\thislabel}. }

Reference a term: \gls{aliquampurus} (see also
\crossrefentry{aliquampurus}).
\printglossary[style=crossref,nopostdot=false,nogroupskip]

\end{document}

The entries have all been defined in the file example-glossaries-seealso.tex. The entry definitions look like this:

\newglossaryentry{fusce}{name={fusce},
description={suscipit cursus sem},user1={vivamus}}

\newglossaryentry{vivamus}{name={vivamus},
description={risus mi, egestas ac}}

\newglossaryentry{imperdiet}{name={imperdiet},
description={varius, faucibus quis, leo},user1={vivamus}}

\newglossaryentry{aenean}{name={aenean},
description={tincidunt}}

\newglossaryentry{crasid}{name={cras id},
description={justo quis nibh scelerisque dignissim},
user1={aenean,vivamus}}

If you don’t use arara, you need to run the following commands:

pdflatex seealso
makeglossaries seealso
pdflatex seealso

(See Incorporating makeglossaries or makeglossaries-lite or bib2gls into the document build.)

Download: PDF (40.51K), source code (1.09K), sample glossary definitions (1.09K).