About
Shop
LaTeX
Free Software
Books
News
Contact
Blog
News (2017-08-02): Latest blog post The Private Enemy Giveaway.

Gallery (glossaries): Cross-References Sample

Image of cross-references in glossary sample
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 is fetched and stored in a control sequence using \glsletentryfield which is then expanded before being processed by \glsseelist.

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. I've also defined a command called \crossrefentry that can be used in the document, if required.

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

The 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
These commands can be run from a terminal or command prompt or you might be able to run them by clicking on a button in your text editor.

Download: PDF, document source, sample glossary definitions.


Last modified: 2017-02-07.

The free resources on this site are funded by book sales, not by adverts. If you would like to help keep this site free of annoying third-party ads, please consider buying a book.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Site Map FAQs