3

I'm preparing a document class using the glossaries package to typeset dictionaries. Since many of my entries need figures, tables, and maybe, say, some C++ code, I'd like to know is there a way to put directly these stuff into the description key of the glossaries package?
Here is a simple MWE:

\documentclass[twoside]{book}
\usepackage[]{graphicx} 
\usepackage{lipsum}
\usepackage[xindy,nonumberlist]{glossaries}
\makeglossaries

\newcommand{\dict}[4][]{%
  \newglossaryentry{#2}%
  {%
    name=#2,%
    symbol=#3,%
    description=#4,%
    #1%
  }%
  \glsadd{#2}%
}

\begin{document}
    \dict{zero}{n}{\lipsum[4]}
    \dict{adhesive}{n}{\lipsum[5]}
    \dict{main}{n}{\lipsum[3]}
    \dict{material}{n}{\lipsum[1]}
\printglossary
\end{document}

I know that one solution is storing those stuff in a command and then use it as the description, but I don't like this solution. My descriptions are like the below structure:

    \dict{lion}{n}{%
    Lions live in ...

    \begin{center}
    \includegraphics[width=3cm]{lion}
    \end{center}

    They generally eat ...
    }

BTW, I must only use xindy to sort the entries.

5
  • Is it essential you be able to define the entries after \begin{document}? Or could you define them first and then add them once the document is begun?
    – cfr
    Aug 9, 2014 at 18:07
  • @cfr: Yes, they should be after \begin{document}. Aug 9, 2014 at 18:31
  • 2
    I don't think you can do that. If your entries include 'long' descriptions i.e. they involve paragraph breaks, then you need to use \longnewglossaryentry and that can only be used in the preamble. It does, however, solve the problem. (See below.) Note that the recommendation is to define or load all entries prior to \begin{document} and various commands are only available in the preamble. (See the manual for discussion and details.) Why do you want them to be after \begin{document}?
    – cfr
    Aug 9, 2014 at 19:32
  • @cfr: I didn't know anything about the \longnewglossaryentry because of my old installation. "Why do you want them to be after \begin{document}?" Because I thought by defining the entries before \begin{document}, I con't use commands like \footnote, but now I see I was wrong! Aug 9, 2014 at 20:35
  • 1
    Oh, I see. No. Generally you have a lot more flexibility if you define them in the preamble. (I use longtable in entries and, as you see, you can use paragraph breaks etc. so you have a lot more room to format the entries as you wish.)
    – cfr
    Aug 9, 2014 at 21:10

1 Answer 1

5
+100

You need to use the command for adding new entries with long descriptions so that the paragraph breaks do not cause errors. To do this, you need to move your definitions into the preamble or to load them from an external file in the preamble. For example, the following modifies \dict to use the long entry command and then defines the definitions in the preamble before adding them in the document.

I'm not sure what #1 is intended for so I'm not positive where that should go and you may need to move it appropriately.

\documentclass[twoside]{book}
\usepackage[]{graphicx}
\usepackage{lipsum}
\usepackage[xindy,nonumberlist]{glossaries}
\makeglossaries

\newcommand{\dict}[4][]{%
  \longnewglossaryentry{#2}%
  {%
    name=#2,%
    symbol=#3,%
    #1%
  }{%
    #4
  }%
%   \glsadd{#2}%
}
\dict{zero}{n}{\lipsum[4]}
\dict{adhesive}{n}{\lipsum[5]}
\dict{main}{n}{\lipsum[3]}
\dict{material}{n}{\lipsum[1]}
\dict{lion}{n}{%
  Lions live in\dots%

  \begin{center}
    \includegraphics[width=3cm]{example-image-a}
  \end{center}

  They generally eat\dots%
}
\dict{tiger}{n}{%
  Tigers live in\dots%

  \begin{center}
    \includegraphics[width=3cm]{tiger}
  \end{center}

  They generally eat\dots%
}

\begin{document}
  \glsaddall

\printglossary
\end{document}

Illustrated entries

[The image of the tiger is from the pstricks documentation with a symbolic link from the working directory.]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .