Gallery: Images in Glossary

image of glossary with multiple paragraph descriptions containing an image inset on the right
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 (which needs to be installed but not loaded for this document). The graphicx package is needed to include in the image in the document.

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 (called images).

This new custom 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. (See Adjusting the alttree Style for a way of measuring available space.)

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

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

\makeglossaries

\loadglsentries{example-glossaries-images}

 % Define a style for the "images" glossary. The image names are
 % stored in the user1 key.
 % (It's possible that the image insertion can done more neatly using an
 % appropriate package.)

\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 use commands like \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

(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 (132.42K), source code (1.42K), sample glossary definitions (14.11K).