texblog

LaTeX glossary and list of acronyms

According to Wikipedia, a glossary is an alphabetical list of terms in a particular domain of knowledge with the definitions for those terms. It doesn’t come as a surprise that there are several LaTeX packages that assist with the generation of glossaries. Among them are the nomencl package, the glossary package, and the glossaries package. Nomencl has been around for quite some time and I previously wrote about it on this blog. Glossary is obsolete and has been replaced by glossaries, the most recent and flexible of the three packages. Here, I’ll introduce the glossaries package and provide some code that I hope will help non-expert LaTeX users to generate a glossary or list of acronyms.

 

Overview of the steps to generate a glossary
  1. Prepare the tex file
  • Typeset the document
  • Generate the index file
  • Typeset the document again
  •  

    A basic example

    \documentclass{article}
    
    % Load the package
    \usepackage{glossaries}
    
    % Generate the glossary
    \makeglossaries
    
    \begin{document}
    
    %Term definitions
    \newglossaryentry{utc}{name=UTC, description={Coordinated Universal Time}}
    \newglossaryentry{adt}{name=ADT, description={Atlantic Daylight Time}}
    \newglossaryentry{est}{name=EST, description={Eastern Standard Time}}
    
    % Use the terms
    \gls{utc} is 3 hours behind \gls{adt} and 10 hours ahead of \gls{est}.
    
    %Print the glossary
    \printglossaries
    
    \end{document}

    Most commands are straight forward. A few remarks on the term definition command \newglossaryentry. The first argument it takes is the label used to produce the term later. The second argument is a key-value pair defining the term and its description. Curly brackets are required if name or description consist of multiple words, commas, etc.

    \newglossaryentry{⟨label⟩}{name={⟨key⟩}, description={⟨value⟩}}

     

    Capitalize and pluralize terms

    The command \gls produces the name of the term given the label. To capitalize the first letter or pluralize the term, the package implements additional commands.

    % Standard command
    \gls{⟨label⟩}
    
    % Capitalize first letter
    \Gls{⟨label⟩}
    
    % Pluralize term
    \glspl{⟨label⟩}
    
    %Capitalize and pluralize term
    \Glspl{⟨label⟩}

     

    Acronyms

    Acronyms are different from glossary entries. For acronyms, the definition is produced in the text with the acronym in parentheses. For glossary entries, only the name is produced. The package distinguishes between glossary and list of acronyms. To do that, the user defines acronyms differently from glossary entries.

    \newacronym{⟨label⟩}{⟨abbrv⟩}{⟨full⟩}

    By default, acronyms are produced as part of the glossary. To generate a separate list of acronyms, the package needs to be loaded with the acronym option.

    \usepackage[acronym]{glossaries}

    Let’s see what the basic example above looks like when we define the terms as acronyms and use the acronym option.

    And here is an example of a document with a list of acronyms.

    \documentclass{article}
    
    % Load the package with the acronym option
    \usepackage[acronym,nomain]{glossaries}
    
    % Generate the glossary
    \makeglossaries
    
    \begin{document}
    
    \section*{Section with acronyms}
    
    % Acronym definitions
    \newacronym{utc}{UTC}{Coordinated Universal Time}
    \newacronym{adt}{ADT}{Atlantic Daylight Time}
    \newacronym{est}{EST}{Eastern Standard Time}
    
    % Use the acronyms
    \gls{utc} is 3 hours behind \gls{adt} and 10 hours ahead of \gls{est}.
    
    %Print the glossary
    \printglossaries
    
    \small\hfill Created by http://texblog.org
    
    \end{document}

    The same alternative commands as for the glossary are used to capitalize and pluralize acronyms.

     

    Generate the glossary or list of acronyms

    Probably the trickiest part comes next. The formatted glossary or list of acronyms needs to be generated from the list of glossary entries or acronyms using makeindex. Your editor might have a button, but in general the files are generated in the terminal/command-line. The package author provides a Perl script makeglossaries that greatly simplifies this step. However, it requires Perl to be installed on your computer.

    After typesetting the document once (latex/pdflatex), go to the terminal, navigate to the project folder and try the following:

    makeglossaries filename

    Replace filename with the name of your tex document.

    If this doesn’t work, you probably don’t have Perl installed and need to use makeindex instead (see below). If you manage to run the Perl script but some entries are rejected, the problem is in the LaTeX code.

    Here is my makeglossaries output for glossary and list of acronyms:

     

    Using makeindex to generate the glossary or list of acronyms

    Again, replace “filename” with your the name of your tex document.

    # Glossary
    makeindex -s filename.ist -t filename.glg -o filename.gls filename.glo
    
    # List of acronyms
    makeindex -s filename.ist -t filename.alg -o filename.acr filename.acn

    About these files:

     

    Produce the glossary or list of acronyms in the document

    If this worked, you’re almost done. All you need to do now is go back to the LaTeX document and typeset it again. The glossary or list of acronyms should show up in the document now. Typeset again to make sure the table of contents is updated.

     
    This concludes the basics on glossaries. I might write more about this package later. If you have questions, see the package documentation on CTAN or drop me a comment below. Thanks.

    Exit mobile version