I am writing a scientific text/book which will contain acronyms, which should have a glossary and should also have an index. The question is: which LaTeX setup is best for this? Which are the best packages, etc.
Obviously some of the problems are overlapping.
- I want to have a glossary where I explain some terms or where I give a one sentence explanation and refer to the section where the term is discussed in detail.
- For some of these terms it might be common to use the acronym/abbreviation. Examples are HTTP, TCP or Dow Jones.
- Other terms are usually written out, but I might want to use an abbreviation (in some sections) because they appear often or because they are so lengthy. (Examples: Distributed Event-Based System: DEBS or Peer-to-Peer: P2P)
- I want to have a List of Abbreviations and Acronyms.
- I also want to have a Glossary where some of the acronyms/abbreviations appear, but not all.
- I also want to have an index that links to the relevant pages where some of the index terms appear. It should also be possible for the index to contain terms that do not appear in the glossary or acronym list.
Searching for possibilities to achieve this I found several partial solutions, but I am not sure how they will work together, especially since some seem to overlap.
acronym
package- seems to be quite common and powerful when it comes to acronyms – maybe ever a little overkill.
- unclear how to make use of it for index/glossary purposes
glossaries
package- supports glossaries nicely
- also supports acronyms. But how can I define acronyms that should not appear in the glossary, but some should appear in the glossary
- seems to work well with hyperref
- uses makeindex
- index with
makeindex
,\index{..}
and\printindex
- is build-in in LaTeX
- can I combine it with the glossaries package?
- other glossary packages like
makeglos
,gloss
,glossary
,glosstex
- other acronym packages like
nomencl
- other index packages like
makeidx
So there are many solutions, but which is the best?
Best Answer
With the
glossaries
package, each term is first defined (in the preamble if you don't want nasty surprises that can occur in certain circumstances) using\newglossaryentry
. For example:(Make sure you use braces
{ }
around the values if they contain a comma,
or equal sign=
.)The first argument is a label (avoid special characters) so that you can reference the term in the document text. For example:
Use
\printglossaries
where you want all the glossaries, lists of abbreviations etc to occur. These are displayed in order that each type of glossary was defined (the defaultmain
is usually first), but if you want to change the ordering or have some in the front matter and some in the back matter you'll need to use\printglossary
and specify the type in the optional argument.Complete minimal example:
The comments at the start are
arara
directives. If you don't usearara
, you can remove those lines. If you do usearara
, then you can build the complete document usingarara myDoc
where the document is in the filemyDoc.tex
. If you're not usingarara
, you can build the document usingThe
makeglossaries
application is actually a Perl script, so you'll need Perl installed if you want to use it. If you don't have Perl installed, you can use a Lua alternative instead:(The advantage of
makeglossaries
is that it has some diagnostic tools to check for common problems.) This Lua script is actually distributed with the filenamemakeglossaries-lite.lua
but the TeX distributions usually arrange it so that you don't use the.lua
extension.For help integrating
makeglossaries
ormakeglossaries-lite
into your document build process, see Incorporating makeglossaries or makeglossaries-lite or bib2gls into the document build.If you're still stuck, another possibility is to use the
automake
package option:This just requires two LaTeX calls for the above example. Note that you can't use this if the shell escape has been disabled. (For a large document, it's more efficient to just call
makeglossaries
ormakeglossaries-lite
when the glossaries need updating rather than useautomake
which tries to rebuild them on every LaTeX run.)The above example produces:
The number
1
that appears after the description is the page number where the term was referenced. You can change the counter used with thecounter
package option. For example, to switch to thesection
counter:or you can completely omit the number lists using the
nonumberlist
package option.If you're really stuck and you're happy to omit the location numbers and do the sorting manually, then you can use
\printunsrtglossary
(or\printunsrtglossaries
) provided byglossaries-extra
:In this case the entries must be defined in the preamble (or using the package option
docdefs=restricted
they can be defined in the document anywhere before they are referenced and before the glossary). This method doesn't perform any indexing and simply iterates over all defined entries (so they must be defined first) in the order in which they were defined. There's no check to determine if the entries have actually been used in the document.Note that the sentence terminating dot hasn't been automatically inserted after the description in this case. With
glossaries-extra
you need to use the package optionpostdot
ornopostdot=false
to add it.The
sort=none
option may be omitted (\printunsrtglossary
is always according to definition) but this option prevents the automated assignment of thesort
field, which isn't required in this case. (It's not significant for such a small example.)An alternative approach is to use
glossaries-extra
withbib2gls
. This requires entries to be defined in.bib
files, which means that if you have a large set of terms that you commonly use in your documents then you can use a.bib
management system like JabRef to organise your definitions. (Bothbib2gls
and JabRef require Java.)For example, the file
entries.bib
might contain:(I can create this
entries.bib
file from my earliermyDoc.tex
file usingconvertgls2bib myDoc.tex entries.bib
which searches for commands like\newglossaryentry
and converts them to the require.bib
format.)I now need to edit
myDoc.tex
to become:The build process is now:
If letter groups are required then you need
--group
(or-g
):and a glossary style that supports groups. For example:
Abbreviations and acronyms can be defined with
\newacronym
. This internally uses\newglossaryentry
and assigns a long and short form. The first argument is optional and can be used to provide additional fields for the second argument of\newglossaryentry
. With just the baseglossaries
package, you can set the style using\setacronymstyle
but this needs to be done before you define the terms. If you want a separate list of acronyms/abbreviations you can use theacronym
package option. For example:This produces:
Note that this expands on first use and uses the abbreviated form on subsequent use. (You can reset this if required.)
For abbreviations that shouldn't be expanded, there are three approaches:
Define them with
\newacronym
and then unset them (which pretends they have already been used):Define them as a regular term but use the
type
key to put them in the list of abbreviations/acronyms:Use the extension package glossaries-extra, which allows different abbreviation styles. This package provides
\newabbreviation
to define an abbreviation with the entry'scategory
(a new key provided byglossaries-extra
) set toabbreviation
and redefinesglossaries
's\newacronym
to use\newabbreviation
with thecategory
set toacronym
(and thetype
set to\acronymtype
). The abbreviation style is set using\setabbreviationstyle[
category]{
style}
(not\setacronymstyle
). The default style for theabbreviation
category islong-short
and the default style for theacronym
category isshort
, so we can just use\newabbreviation
for the abbreviations that need expansion on first use and\newacronym
for abbreviations that don't need expansion on first use.With
bib2gls
, the earlierentries.bib
supplies the terms (duck and parrot) and the abbreviations can be stored in another.bib
file called, say,abbrvs.bib
:and the common abbreviation in
common.bib
:(or they can be combined in the same file). The document is now:
The first two examples both produce:
The third and fourth examples produce:
The only differences here are the missing post-description dot (which can be added with
postdot
) and the title of the list of abbreviations. This is because I used theabbreviations
package option. If I'd usedacronym
(oracronyms
) instead, I would have to add the lineto ensure the abbreviations defined using
\newabbreviation
are placed in theacronym
glossary (otherwise they'd end up in themain
glossary).Alternatively, (with
glossaries-extra
) instead of using\newacronym
, you can provide your own category and use\newabbreviation
, which ensures it's placed in the same glossary list as the other abbreviations. For example:to set the abbreviation style for the
common
category andor
to define the abbreviation. Alternatively, you can instruct
bib2gls
to set thecategory
field to the base name of the corresponding.bib
file. For example, if the general abbreviations are now inabbreviation.bib
and the common abbreviations are incommon.bib
then you can use:You can use the
type
key to put terms into a specific glossary. You can also create an "ignored" glossary, in which you can put any entries you want to reference but don't want listed. For example, to create an ignored glossary labelled "ignored":Now you can define terms to go in it. For example:
Or with
glossaries-extra
:Or with
bib2gls
, the easiest way is to move the ignored abbreviations to another file, say,ignored.bib
and use:(If you want to keep them in the same
abbrvs.bib
file you can apply pattern matching in the selection, but that's more complicated.)You can use the
index
package optionwhich will create a new glossary labelled
index
and will also define the command\newterm
, which is a shortcut for\newglossaryentry
that defines a term without a description and usestype=index
to add it to theindex
glossary. For example:If the term contains any special characters, you'll need to use the
name
key in the optional argument and strip the special characters from the mandatory argument. For example:With
bib2gls
you can use:(With XeLaTeX or LuaLaTeX you can use UTF-8 characters in the label but with
inputenc
you can't.)The best style for displaying an index is
bookindex
, which is provided by theglossary-bookindex.sty
distributed withglossaries-extra
. This doesn't show the description, so even with the earlier@entry
examples, it will still only show the name and location list.bib2gls
has special dual entries that essentially define two linked entries. If one is referenced in the text then the other is automatically selected. Suppose I have a file calledterms.bib
that contains:Then the document can be changed to:
This produces:
It's not particularly convenient to have to edit the
.bib
files to substitute@entry
with@dualindexentry
and@abbreviation
with@dualindexabbreviation
, sobib2gls
allows aliasing with theentry-type-aliases
, which makesbib2gls
treat the given entry types as though they were actually defined with the aliased type:Alternatively, just use one of the indexing packages, such as
makeidx
orimakeidx
. Withmakeidx
you'll need an extra step in the build process that runs themakeindex
application. This can be done aftermakeglossaries
(and should be done after rather than beforemakeglossaries
if\index
occurs any of the glossary lists). Theimakeidx
package runsmakeindex
through the shell escape.For example:
The
glossaries-extra
package provides a way of automatically indexing entries so they appear both in the glossary list and the index. For example, if I addto the preamble, then the HTTP entry (that has the
category
set tocommon
) referenced on page 1 will also appear in the index. Alternatively, I can use:which will instead index the page where HTTP occurs in the list of abbreviations. If I want to change the encap (how the page number is formatted in the index), then I can replace
true
with the encap value. For example:There are various different glossary styles see, for example, the glossaries gallery.