[book cover]
Latest news 2019-10-04: Competition to win a copy of the children’s illustrated book “Quack, Quack, Quack. Give My Hat Back!”. Closing date: 1st Nov 2019 23:59 +00:00.

Gallery: Standalone Entries

If you have a small screen, you may prefer to switch on the small image setting.

image of sample document with entry name in section header followed by description in italic with an image in the right margin

This example doesn’t have a sorted list but instead uses \glsxtrglossentry to generate standalone entry anchors within the document. With the hyperref package loaded (before glossaries-extra), instances of \gls will link to the corresponding \glsxtrglossentry.

Since no sorting is needed, the package option sort=none is used. This may be omitted, but using it will slightly improve the document build process as it disables the default formation of the sort field. (It doesn’t make much difference for such a small document.)

The document loads the sample entries defined in example-glossaries-images.tex (which is provided with the glossaries package for test documents). Some of these entries have the user1 field set to the name of an image file provided with the mwe package.

For convenient, I defined a helper command to mark these standalone entries:

\newcommand{\entrysection}[1]{%
  \section{\glsxtrglossentry{#1}}%
  \ifglshasfield{useri}{#1}%
  {% this entry has an image
    \marginpar
    {%
     \raisebox{1ex-\height}%
       {\includegraphics[width=\marginparwidth]{\glsentryuseri{#1}}}%
    }%
  }%
  {}%
  \begingroup % scope
    \itshape % needs scoping
    \glsxtractivatenopost % needs scoping
    \glossentrydesc{#1}\glspostdescription
    \par
  \endgroup
  \medskip
}

This starts a new section with \section where the argument is \glsxtrglossentry{#1} which displays the entry name and provides an anchor allowing commands like \gls to link to it (unlike \glsentryname{#1}, which would simply display the entry name). This standalone \glsxtrglossentry{#1} command honours the entrycounter package option, the glossname and glossnamefont attributes and also the post-name hook. This means that I can convert the first letter to uppercase using:

\glssetcategoryattribute{general}{glossname}{firstuc}

The custom command then checks if the useri field is set. (This is the internal name corresponding to the user1 key.) If it’s set, the image is placed in the margin. For convenience, the calc package is used to make it easier to raise the image.

The description then follows. I could just use \glsentrydesc{#1}, but by using \glossentrydesc{#1} instead, it again behaves more like the description when used in a glossary style, so it can be modified by attributes such as glossdesc. To ensure that it also picks up the postpunc package option setting and the post-description hook, \glspostdescription is required. If the description may contain \nopostdesc or \glsxtrnopostpunc, then \glsxtractivenopost is also needed before \glossentrydesc, but note that this must be scoped, so \begingroup and \endgroup are used.

Note that \glsxtrglossentry{#1} doesn’t need to be within a sectioning command. A much simpler in-line form can be created for entries with short descriptions:

\newcommand{\entry}[1]{\glsxtrglossentry{#1} (\glsentrydesc{#1})}

Remember that with the entrycounter package option you can reference the associated number with \glsrefentry and the page number with \glsxtrpageref.

The build process is simple as no external tool is required. The second LaTeX call ensures that the PDF bookmarks are available, but one call is enough for the glossary commands to work. (You can also use this method with bib2gls if you want to fetch the entry definitions from a .bib file, but there’s no need to sort the entries.)

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

% arara: pdflatex
% arara: pdflatex
\documentclass{article}

\usepackage{graphicx}
\usepackage{calc}
\usepackage[colorlinks]{hyperref}
\usepackage[sort=none]{glossaries-extra}

\loadglsentries{example-glossaries-images}

\glssetcategoryattribute{general}{glossname}{firstuc}

\newcommand{\entrysection}[1]{%
  \section{\glsxtrglossentry{#1}}%
  \ifglshasfield{useri}{#1}%
  {% this entry has an image
    \marginpar
    {%
     \raisebox{1ex-\height}%
       {\includegraphics[width=\marginparwidth]{\glsentryuseri{#1}}}%
    }%
  }%
  {}%
  \begingroup
    \itshape % needs scoping
    \glsxtractivatenopost % needs scoping
    \glossentrydesc{#1}\glspostdescription
    \par
  \endgroup
  \medskip
}

\begin{document}

\entrysection{sedfeugiat}

Some text starting this section. This entry has an associated image
(provided with the \textsf{mwe} package).

\entrysection{aliquamlectus}

Some other text starting this section with a reference to
\gls{sedfeugiat} from the previous section.

\entrysection{sedeleifend}

This one doesn't have an associated image.

\end{document}

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

pdflatex sample-noprintglossary
pdflatex sample-noprintglossary

Download: PDF (69.86K), source code (1.05K), sample definitions (14.11K).

© 2019 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 Cookies Site Map FAQs