Smile for the Camera: a new cybercrime short story ebook.

Gallery: URLs in Glossary

image of glossary with a URL on the line following the description and page number
This example has URLs associated with some entries. Care needs to be taken if the URL contains special characters. For the percent character % the simplest method is to just use \%. The tilde character ~ is more complicated. In this case, I suggest using either \string~ or \glstildechar. Note that \protect~ doesn’t work (as shown in the PDF).

The URL for each entry is stored in the user1 field (one of the custom fields). Since none of the predefined glossary styles are suitable, this example defines and uses its own custom style (called urls). The style is based on the list style (\setglossarystyle{list}) but it’s modified to check if the useri field is set (useri is the internal field name corresponding to the user1 key). The value of the useri field is fetched and stored in a control sequence using \glsletentryfield which is then expanded before being processed by \url (provided by hyperref).

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

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

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

\makeglossaries

\loadglsentries{example-glossaries-url}

\newglossarystyle{urls}
{% 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{\thisurl}{##1}{useri}%
     \expandafter\url\expandafter{\thisurl}}{}}%
}

\newcommand*{\entryurl}[1]{%
 \glsdoifexists{#1}%
 {%
  \glsletentryfield{\thisurl}{#1}{useri}%
  \expandafter\url\expandafter{\thisurl}%
 }%
}

\begin{document}

\chapter{Sample}

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

The URL associated with the entry \gls{aenean-url}
is \entryurl{aenean-url}.

\printglossary[style=urls,nopostdot=false,nogroupskip]

\end{document}

I’ve used \forglsentries for this example to iterate over all the defined entries. In practice, you would typically just use commands like \gls{label} in the text. For example:

Here is a reference to the term \gls{aenean-url}.

The sample document defines a command (\entryurl) that allows you to reference the URL within the document text.

The entries have all been defined in the file example-glossaries-url.tex, which you should find installed in the same location as the glossaries package (or in a sub-directory called test-entries). The entry definitions look like this:

\newglossaryentry{aenean-url}{name={aenean},
 description={adipiscing auctor est},
 user1={http://uk.tug.org/}}

\newglossaryentry{morbi-url}{name={morbi},
 description={quam arcu, malesuada sed, volutpat et, elementum sit
amet, libero},
 user1={http://www.ctan.org/}}

\newglossaryentry{duis-url}{name={duis},
 description={mattis},
 user1={http://www.tug.org/}}

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

pdflatex urls
makeglossaries urls
pdflatex urls

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

I’ve used the nopostdot option to remove the terminating full stop that is placed by default after the description. The entries all have a “1” after the description. This is the page number on which the entry was referenced. In this sample document all the entries were referenced on page 1. If you don’t want these numbers you can use the nonumberlist option.

Download: PDF (51.74K), source code (977B), sample glossary definitions (1.61K).