[Tex/LaTex] How to include glossary directly in TeX file

glossaries

The glossaries package uses external files (especially a .gls file) to build the glossary, e.g. when building the following example. Is there a way to include the content of such a file directly into the TeX file? Then it would not be necessary to use makeglossaries or to provide the .gls file, e.g. when submitting the TeX file to a journal.

\documentclass{scrbook}
\usepackage{glossaries}
\makeglossaries

\newglossaryentry{eta}{name=\ensuremath{\eta},description={eta}}
\newglossaryentry{psi}{name=\ensuremath{\psi},description={psi}}

\begin{document}
    \glsaddall
    \printglossary
\end{document}

Directly adding the content of the file to the TeX file gives

! Undefined control sequence.
<argument> \glossarytitle 

By the way: There is no need to update references or anything else. I just need the layout and the entries.

Best Answer

\printglossary doesn't just input the glossary file but performs quite a bit of setup first, which is why you can't simply replace it with \input.

Basically, if you don't want to use an external application, then:

  1. If you want all defined entries, don't want number lists and don't need automatic sorting use \printunsrtglossary provided by the glossaries-extra extension package.
  2. Otherwise use \makenoidxglossary and \printnoidxglossary (as mentioned by Christian).

Example with the first approach:

\documentclass{scrbook}

\usepackage[nogroupskip]{glossaries-extra}

\newglossaryentry{eta}{name=\ensuremath{\eta},description={eta}}
\newglossaryentry{psi}{name=\ensuremath{\psi},description={psi}}

\begin{document}
\printunsrtglossary
\end{document}

image of glossary with no number list

This only requires a single latex call as no sorting is performed and there's no check to determine if the entry has been indexed. I've used the nogroupskip as there are no logical letter groups in this case.

The above method is different to using the \makenoidxglossaries and \printnoidxglossary approach, which requires a second run to determine which entries have been indexed. The noidx method (denoted option 1 in the manual) also tries to sort the entries alphabetically by default and will obtain the sort value from the name key if the sort key is omitted, but the maths markup (\ensuremath{\eta} etc) causes a problem. Example using order of definition (instead of alphabetical ordering):

\documentclass{scrbook}

\usepackage[sort=def,nogroupskip]{glossaries}

\makenoidxglossaries

\newglossaryentry{eta}{name=\ensuremath{\eta},description={eta}}
\newglossaryentry{psi}{name=\ensuremath{\psi},description={psi}}

\begin{document}
\glsaddall

\printnoidxglossary
\end{document}

image of glossary with locations

It is actually possible to add letter groups and location lists with \printunsrtglossary but it has to be done manually:

\documentclass{scrbook}

\usepackage{glossaries-extra}

\glsaddstoragekey{group}{}{\entrygroup}
\glsaddstoragekey{location}{}{\entrylocation}

\glsxtrsetgrouptitle{greek}{Greek Symbols}

\newglossaryentry{eta}{name=\ensuremath{\eta},description={eta},
 group={greek},location={\setentrycounter[]{page}\hyperbf{5}}}
\newglossaryentry{psi}{name=\ensuremath{\psi},description={psi},
 group={greek},location={\setentrycounter[]{page}\hyperit{3}}}

\begin{document}
\printunsrtglossary[style=indexgroup]
\end{document}

image of glossary with letter group heading and locations

Both methods require all entries to be defined before the glossary. The second case requires all entries to be defined in the preamble. The first case may allow document definitions but only with the package option docdefs=restricted. In general it's best to put all document definitions in the preamble.

Related Question