News (2017-09-10): New application bib2gls for use with glossaries-extra.sty.

Gallery (glossaries): Images in Glossary Sample

This example has an image associated with each entry. For simplicity I've just used the sample images that are provided by the mwe package. The image file name 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 tree style (\setglossarystyle{tree}) but it's modified to check if the useri field is set (useri is the internal field name corresponding to the user1 key). If it's set, the image file (provided by that key) is scaled so that it's approximately the height of four lines and is stored in a box called \glsimagebox. (This box is defined using \newsavebox before the glossary style.) The width of this box (which is the width of the scaled image) is stored in the register called \glswidth. (This register is defined using \newlength before the glossary style.) The first paragraph of the description has a "cutout" section in which the image will be placed. This is created using TeX's \parshape primitive. The first four lines of the description are computed as \linewidth-\glswidth-1em. The extra 1em provides a little padding between the text and the image. If there's no image for the given entry, the paragraph shape isn't changed, but in either case the paragraph indentation is suppressed using \noindent. The image is inserted using a little bit of trickery by placing it inside a box whose width is given by \linewidth and is right-aligned. Some vertical adjustment is required and the whole thing is then placed inside a zero-width box so it doesn't interfere with the text.

One thing you need to be careful about is the situation where an entry starts close to the end of the page, in which case the image will poke out into the bottom margin. This hasn't been taken into account here, and you can see the problem with the "nulla non mauris" entry.

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{graphicx}

\usepackage[nopostdot,toc]{glossaries}

\makeglossaries

\newsavebox\glsimagebox
\newlength\glswidth

\newglossarystyle{images}%
{%
\setglossarystyle{tree}%
\renewenvironment{theglossary}{}{}%
\renewcommand{\glossentry}[2]{%
\ifglshasfield{useri}{##1}%
{\savebox
{\glsimagebox}%
{\includegraphics[height=4\baselineskip]{\glsentryuseri{##1}}}%
\settowidth{\glswidth}{\usebox\glsimagebox}%
\glswidth=\dimexpr\linewidth-\glswidth-1em\relax
\parshape=5
0pt \glswidth
0pt \glswidth
0pt \glswidth
0pt \glswidth
0pt \linewidth\relax
\noindent
\makebox[0pt][l]{%
\smash{\makebox[\linewidth][r]{\raisebox{-3\baselineskip}{\usebox\glsimagebox}}}}%
}%
{%
\noindent
}%
\glsentryitem{##1}\textbf{\glstarget{##1}{\glossentryname{##1}}}%
\space\glossentrydesc{##1}\par
\indexspace
}%
\renewcommand*{\glsgroupskip}{}%
}

\begin{document}

\chapter{Sample}

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

\printglossary[style=images]

\end{document}

This document loads the hyperref package, which creates hyperlinks from the entries in the document (referenced using commands like \gls) to their definition in the glossary. These hyperlinks are displayed in red text. 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{sedfeugiat}.

I can also reuse the image in the document:
\glsuseri{sedfeugiat}.

(which will hyperlink to the glossary) or just
\glsentryuseri{sedfeugiat}.


The entries have all been defined in the file example-glossaries-images.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:

\longnewglossaryentry{sedfeugiat}{name={sed feugiat},user1={example-image}}%
{%
Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Ut pellentesque augue sed urna. Vestibulum
diam eros, fringilla et, consectetuer eu, nonummy id, sapien. Nullam
at lectus. In sagittis ultrices mauris. Curabitur malesuada erat sit
amet massa. Fusce blandit. Aliquam erat volutpat.  Aliquam euismod.
Aenean vel lectus.  Nunc imperdiet justo nec dolor.

Etiam euismod. Fusce facilisis lacinia dui.  Suspendisse potenti. In
mi erat, cursus id, nonummy sed, ullamcorper eget, sapien. Praesent
pretium, magna in eleifend egestas, pede pede pretium lorem, quis
consectetuer tortor sapien facilisis magna.  Mauris quis magna
varius nulla scelerisque imperdiet. Aliquam non quam. Aliquam
porttitor quam a lacus. Praesent vel arcu ut tortor cursus volutpat.
In vitae pede quis diam bibendum placerat. Fusce elementum convallis
neque. Sed dolor orci, scelerisque ac, dapibus nec, ultricies ut,
mi. Duis nec dui quis leo sagittis commodo.
}


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

pdflatex images-glossary
makeglossaries images-glossary
pdflatex images-glossary

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.

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.