How can I create a glossary as I go?

I'd like to keep defining terms as I go through the document using the description environment, but then have all those terms gathered together (with their descriptions) in a glossary at the end of the document.

The glossaries package would seem to be the obvious choice for how to do this, but I can't see a way to list the entry descriptions in the document body as I go. An MWE would look like

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\begin{document}
\tableofcontents{}
\section{Introduction}
\begin{description}
\item[Ontology] refers to a theory of what is real
\item[Epistemology] refers to a theory of knowledge
\end{description}
\section{Theory} \begin{description}
\item[Potato] is a class of root vegetables
\item[Reductionism] refers to the practice of reducing one set of ideas to another \end{description}

\section{Glossary of Terms}

\begin{description}
\item[Epistemology] refers to a theory of knowledge
\item[Ontology] refers to a theory of what is real
\item[Potato] is a class of root vegetables
\item[Reductionism] refers to the practice of reducing one set of ideas to another \end{description}

\end{document}


Which looks this:

As in, all the terms from the {description} environment get collected, alphabetised, and reproduced with their descriptions in a glossary of terms.

• Could you please show us a minimal working example (MWE)? – Skillmon Mar 19 '18 at 12:54
• I don't recommend redefining the description environment because it's sometimes used internally by other environments, so it could have undesirable side-effects. – Nicola Talbot Mar 19 '18 at 15:30

The simplest method is to create a new file that contains the definitions. This makes it easier to add new terms while you're writing the document as most text editors allow more than one file open at a time, so you can simply switch to the definitions file without losing your place in the document.

Here's definitions.tex:

\newglossaryentry{ontology}{name={ontology},
description={refers to a theory of what is real}}
\newglossaryentry{epistemology}{name={epistemology},
description={refers to a theory of knowledge}}
\newglossaryentry{potato}{name={potato},
category={theory},
description={is a class of root vegetables}}
\newglossaryentry{reductionism}{name={reductionism},
category={theory},
description={refers to the practice of reducing one set
of ideas to another}}


If your definitions span multiple paragraphs then use \longnewglossaryentry instead:

\longnewglossaryentry{reductionism}{name={reductionism},category={theory}}
{refers to the practice of reducing one set
of ideas to another}


Here's a document that uses the all the provided definitions:

\documentclass{article}

\usepackage{glossaries-extra}

\glssetcategoryattribute{general}{glossname}{firstuc}
\glssetcategoryattribute{theory}{glossname}{firstuc}

\newcommand{\printcategory}[1]{%
\printunsrtglossary*{%
\renewcommand{\glossarysection}[2][]{}%
\renewcommand{\printunsrtglossaryhandler}[1]{%
\glsifcategory{##1}{#1}{\glsxtrunsrtdo{##1}}{}%
}%
}%
}

\begin{document}
\tableofcontents

\section{Introduction}
\printcategory{general}

\section{Theory}
\printcategory{theory}

\printunsrtglossary[title={Glossary of Terms}]

\end{document}


If the document is called myDoc.tex then the document build is simply:

pdflatex myDoc


This lists all defined terms in the glossary in the order of definition. If you want the list sorted, then it's more complicated.

Here's an alternative method. Save the definitions in a .bib file, say, definitions.bib:

% Encoding: UTF-8
@entry{ontology,
name = {ontology},
description = {refers to a theory of what is real}
}

@entry{epistemology,
name = {epistemology},
description = {refers to a theory of knowledge}
}

@entry{potato,
name = {potato},
plural = {potatoes},
description = {is a class of root vegetables}
}

@entry{reductionism,
name = {reductionism},
description = {refers to the practice of reducing one set
of ideas to another}
}


Again, it should be simple to switch back and forth between two windows without losing your place in the document. The document is now:

\documentclass{article}

\usepackage[record,% using bib2gls
postpunc=dot% put a full-stop after the descriptions in the glossary
]{glossaries-extra}

src={definitions},% entries in definitions.bib
name-case-change={firstuc}% convert the first character of name to uppercase and set text to the original value of name
]

\newcommand{\glossitem}[1]{\item[\glsentryname{#1}]

\begin{document}
\tableofcontents

\section{Introduction}
\begin{description}
\glossitem{epistemology}
\glossitem{ontology}
\end{description}

\section{Theory}
\begin{description}
\glossitem{potato}
\glossitem{reductionism}
\end{description}

\printunsrtglossary[title={Glossary of Terms}]

\end{document}


This gives you more manual control over the ordering within the document for the different sections. The main glossary is now sorted and includes locations in the glossary that indicate where the term was used (page 1 in all cases for this trivial document).

The build process is now:

pdflatex myDoc
bib2gls myDoc
pdflatex myDoc


If you don't want the locations, either use the resource option save-locations=false:

\GlsXtrLoadResources[
src={definitions},% entries in definitions.bib
name-case-change={firstuc},
save-locations=false
]


or use nonumberlist:

\printunsrtglossary[title={Glossary of Terms},nonumberlist]


By default bib2gls will sort according to the document language (if detected) otherwise it will sort according to the default locale. You can specify a particular language. For example:

\GlsXtrLoadResources[
src={definitions},% entries in definitions.bib
name-case-change={firstuc},
sort={en-GB}
]


If you want to reference a term anywhere else in the document, you can use \gls{label}, for example \gls{ontology}, or at the start of a sentence \Gls, for example \Gls{potato}. The plural can be obtained with \glspl and \Glspl. If you load hyperref (before glossaries-extra), then these will be turned into hyperlinks to the relevant line in the main glossary.

• These solutions are fantastic, especially the second one. Absolutely excellent! – Dan Goldwater Mar 20 '18 at 22:45
• @DanGoldwater Glad it's useful. I added a little note about \gls just in case. – Nicola Talbot Mar 21 '18 at 9:49
• I'm having a bit of trouble implementing the second solution here. I get two problems - one that 'postpunc' is an unknown option for glossaries, and one warning 'Package glossaries-extra Warning No File (path, line 28): 'output.glstex' on input line number 28'. After a bit of research I would have assumed that I need to update my glossaries-extra package, but I'm writing this on ShareLatex and I can't imagine that they don't keep their packages up to date. Any ideas? – Dan Goldwater Apr 18 '18 at 14:24
• @DanGoldwater The postpunc option was introduced in glossaries-extra v1.23 (2017-11-12), so check the log file for the version number. (For older versions you can use just postdot instead or nopostdot=false for even older versions.) The .glstex file is created by bib2gls. Did bib2gls run correctly? I'm not sure if ShareLaTeX has bib2gls installed. The first stable version was only released on 2017-09-09. – Nicola Talbot Apr 18 '18 at 14:40
• I asked ShareLatex and got the following response from them, which I post in case it's helpful to others in the future. – Dan Goldwater Apr 18 '18 at 19:24