About
Shop
LaTeX
Free Software
Books
News
Contact
Blog
News (2017-09-10): New application bib2gls for use with glossaries-extra.sty.

Gallery (glossaries): URLs in Glossary Sample

Image of urls in glossary sample
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. 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 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 just to iterate over all the defined entries for this example. In practice, you would typically just use \gls{label}. 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
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.

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.

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