103

I'm using the glossaries package.

I have an acronym (e.g., API) that should be explained in the glossary. It should be linked to the glossary at every occurrence, but its first occurrence should be written out like this:

This is a test of Application Programming Interface (API).

And this is the second occurrence of API.


Acronyms

API Application Programming Interface

Glossary

API An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API

How can I do that?

2
  • If you define a entry and then use it with its label \gls{label} the first time you use it it will be written as longversion (acronym) and on all subsequent \gls{label}-uses it will come out just as acronym.
    – Martin H
    Jan 14, 2011 at 14:46
  • 1
    It seems redundant to define the acronym expansion and the term meaning two separate steps… why not index the acronyms as well as the full term in the glossary, and link them together there? Feb 8, 2011 at 0:18

8 Answers 8

128

a simple example

\documentclass{article}

\usepackage[acronym]{glossaries}
\makeglossaries

%from documentation
%\newacronym[⟨key-val list⟩]{⟨label ⟩}{⟨abbrv ⟩}{⟨long⟩}
%above is short version of this
% \newglossaryentry{⟨label ⟩}{type=\acronymtype,
% name={⟨abbrv ⟩},
% description={⟨long⟩},
% text={⟨abbrv ⟩},
% first={⟨long⟩ (⟨abbrv ⟩)},
% plural={⟨abbrv ⟩\glspluralsuffix},
% firstplural={⟨long⟩\glspluralsuffix\space (⟨abbrv ⟩\glspluralsuffix)},
% ⟨key-val list⟩}

\newacronym{cd}{CD}{compact disk}


\begin{document}
\noindent
First use \gls{cd}\\
subsequent \gls{cd}

\printglossaries
 
\end{document}

alt text

glossaries supports multiple nomenclatures so you can still use something like this

\newglossaryentry{tree}{name={tree},
description={trees are the better humans}}

and because in the above case the type is automatically set to 'main' it will give you a second list called 'Glossary'

\documentclass{article}

\usepackage[acronym]{glossaries}
\makeglossaries

%from documentation
%\newacronym[⟨key-val list⟩]{⟨label ⟩}{⟨abbrv ⟩}{⟨long⟩}
%above is short version of this
% \newglossaryentry{⟨label ⟩}{type=\acronymtype,
% name={⟨abbrv ⟩},
% description={⟨long⟩},
% text={⟨abbrv ⟩},
% first={⟨long⟩ (⟨abbrv ⟩)},
% plural={⟨abbrv ⟩\glspluralsuffix},
% firstplural={⟨long⟩\glspluralsuffix\space (⟨abbrv ⟩\glspluralsuffix)},
% ⟨key-val list⟩}

\newacronym{cd}{CD}{compact disk}

\newglossaryentry{tree}{name={tree},
    description={trees are the better humans}}

\begin{document}
\noindent
First use \gls{cd}\\
subsequent \gls{cd}

Nomenclature \gls{tree}

\printglossaries
 
\end{document}

alt text

To finally get what you are after, you could use

\documentclass{article}
\usepackage{hyperref}
\usepackage[acronym]{glossaries}
\makeglossaries

%from documentation
%\newacronym[⟨key-val list⟩]{⟨label ⟩}{⟨abbrv ⟩}{⟨long⟩}
%above is short version of this
% \newglossaryentry{⟨label ⟩}{type=\acronymtype,
% name={⟨abbrv ⟩},
% description={⟨long⟩},
% text={⟨abbrv ⟩},
% first={⟨long⟩ (⟨abbrv ⟩)},
% plural={⟨abbrv ⟩\glspluralsuffix},
% firstplural={⟨long⟩\glspluralsuffix\space (⟨abbrv ⟩\glspluralsuffix)},
% ⟨key-val list⟩}

%\newacronym{api}{API}{Application Programming Interface }

%%% The glossary entry the acronym links to   
\newglossaryentry{apig}{name={API},
    description={An Application Programming Interface (API) is a particular set
of rules and specifications that a software program can follow to access and
make use of the services and resources provided by another particular software
program that implements that API}}

%%% define the acronym and use the see= option
\newglossaryentry{api}{type=\acronymtype, name={API}, description={Application
Programming Interface}, first={Application
Programming Interface (API)\glsadd{apig}}, see=[Glossary:]{apig}}
\begin{document}
\noindent
First use \gls{api}\\
subsequent \gls{api}
\newpage

\printglossary[type=\acronymtype]
%%% \newpage just to demonstrate that links are correct
\newpage
\printglossary[type=main]
 
\end{document}

alt text

You can turn it into a custom command, more convenient, like this:

% Command to create a glossary entry with correspondent acronym.
% Args : 1: acronym/name, 2: long name, 3: description
\newcommand{\newglossaryentrywithacronym}[3]{
    %%% The glossary entry the acronym links to   
    \newglossaryentry{#1_gls}{
        name={#1},
        long={#2},
        description={#3}
    }

    % Acronym pointing to glossary
    \newglossaryentry{#1}{
        type=\acronymtype,
        name={#1},
        description={#2},
        first={#2 (#1)\glsadd{#1_gls}},
        see=[Glossary:]{#1_gls}
    }
}

And call it like that:

\newglossaryentrywithacronym{API}{Application Programming Interface}{
    An Application Programming Interface (API) is a particular set of rules and
    specifications that a software program can follow to access and make use of
    the services and resources provided by another particular software program
    that implements that API.
}
11
  • 1
    +1 awesome solution. Can something like this be done somehow in Lyx?
    – denilw
    Feb 5, 2011 at 0:37
  • @denilw: I am not a Lyx expert and don't know much about writing new modules and such.. However, you can always put in commands directly in code by pressing the "TeX" button in Lyx. You can also load the package in the "Latex Preamble" in the document settings. What I don#t know in Lyx is how to call the makeglossaries script or how to modify the makeindex call. It might be a lot easier to switch editors
    – Martin H
    Feb 7, 2011 at 9:51
  • 2
    tex.stackexchange.com/q/12346/978 is a follow up question on how to do this in Lyx
    – denilw
    Mar 1, 2011 at 18:12
  • 2
    This solution is really fantastic and even now, nearly three years after, I'm still glad that I can profit of it. I know it is very ambitious, but I've got a question concerning this solution: would it somehow be possible to "transfer" page-references (which now appear only with the acronym-entry and the glossary-entry is always said to be referenced on page one) to the entry in the glossary? This might be a very tricky task, but it'd make this solution even better than it is already.
    – user43961
    Jan 18, 2014 at 22:41
  • 1
    The solution for this is to move the \glsadd{API} into the \first definition
    – Tom Brien
    Mar 4, 2014 at 12:11
30

My solution looks like that:

\newglossaryentry{api}
{
    name={API},
    description={An Application Programming Interface (API) is a particular set
            of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API},
    first={Application Programming Interface (API)},
    long={Application Programming Interface}
}

So I don't have to split it into two parts, one for the glossary and one for the acronym section. Imho a clean solution.

Cheers :-)

Edit: But if you really want to split into two sections, then OSHis solution is perfect.

3
  • 6
    Nice solution, but I'd use first={\glsentrylong{api}} (\glsentryname{api}).
    – sergej
    Jun 19, 2014 at 18:27
  • 3
    And also add firstplural={\glsentrylong{api}\glspluralsuffix\ (\glsentryname{api}\glspluralsuffix )} Sep 23, 2015 at 20:27
  • 1
    If you do nested references like the two suggestions above, be sure to load your glossary entries from an external file (instead of defining them in your document) or you'll get weird behavior.
    – datu-puti
    Jan 15, 2018 at 18:58
12

I have extended this very very nice example (thanks at this place ;) ) thourgh which it is not necessary any more to add the glossary entry manually:

\newglossaryentry{APIG}{
name=\glslink{API}{Application Programming Interface (\gls{API})},
description={
Application Programming Interface Desc}
}

\newglossaryentry{API}{
type=\acronymtype,
name=API,
first=Application Programming Interface (API),
firstplural={Application Programming Interfaces (API's)},
see=[Glossary:]{\gls{APIG}}, 
description=\glslink{APIG}{Application Programming Interfaces}
}

The main key is \glslink{APIG}{Application Programming Interfaces}. Everytime the (API) acronym is added it "adds" the glossary entry.

0
9

Building off of the two answers above, and rolling in a couple of the comments, I have exactly what I was looking for, and I hope it helps others.

This doesn't create two separate glossaries, but it does allow for a defined abbreviation/acronym that

  • shows the expanded form at the first use
  • shows the expanded form in the glossary
  • allows for another argument, the definition, which is shown in the glossary
  • uses a single simple command with no repetition of terms

enter image description here enter image description here

The \newcommand code block adds a new command, \newdefinedabbreviation that's simply an alias for the commands already discussed.

Example Code

\documentclass{article}
\usepackage{glossaries}
\makeglossaries

\newcommand{\newdefinedabbreviation}[4]{
    \newglossaryentry{#1}{
        text={#2},
        long={#3},
        name={\glsentrylong{#1} (\glsentrytext{#1})},
        first={\glsentryname{#1}},
        firstplural={\glsentrylong{#1}\glspluralsuffix (\glsentryname{#1}\glspluralsuffix )},
        description={#4}
    }
}

\newdefinedabbreviation{api}{API}{Application Programming Interface}{An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API}

\begin{document}
\noindent
First use: \gls{api}\\
Subsequent: \gls{api}

\pagebreak
\printglossaries
\end{document}
3
  • 1
    This method worked best for me. Especially with the way that the references section comes out and how it plays out in section heads. Feb 11, 2019 at 16:39
  • I think the firstplural should be firstplural={\glsentrylong{#1}\glspluralsuffix\ (\glsentryname{#1})},. Because \glspluralsuffix "eats" the space it has to be escaped, alternatively one can use a ~ (unbreakable space; see: tex.stackexchange.com/a/74354/11820). Further, I don't think the \glspluralsuffix is needed in the braces after the \glsentryname{#1}. Dec 1, 2019 at 16:57
  • I guess thats the same as \usepackage[automake, acronyms, abbreviations, shortcuts, nopostdot, toc, style=super]{glossaries-extra} \setabbreviationstyle{long-short-desc} \newabbreviation[description={description text here}]{cits}{C-ITS}{Cooperative Inteligent Transport System}.
    – Paul
    Jun 21 at 10:57
7

I landed here trying to do something similar, but with just a single entry and without the separate use of glossary entries and acronyms. For everyone looking for the same, here is my solution.

What this does:

  • single entry in the glossary
  • entry contains full name and abbreviation
  • first use will show name + abbrv.
  • subsequent uses will show only abbrv.

Code snippet:

\newglossaryentry{API} 
{
    name={Application Programming Interface (API)},
    description={An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API},
    first={Application Programming Interface (API)},
    text={API}
}

"name" specifies the name used for listing in the glossary. "text" specifies the text used when referencing the entry, which is overridden at first use by "first".

How it looks:

enter image description here

Full example:

\documentclass{article}
\usepackage{hyperref}
\usepackage[nonumberlist]{glossaries}
\makeglossaries

\newglossaryentry{API} 
{
    name={Application Programming Interface (API)},
    description={An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API},
    first={Application Programming Interface (API)},
    text={API}
}

\begin{document}
\noindent
First use: \gls{API}\\
Subsequent: \gls{API}

\pagebreak
\printglossaries
\end{document}
5
  • Welcome to TeX.SX! Thanks for your answer. Could you please add a compilable example? (compilable code starts with \documentclass and will end with \end{document}.)
    – Bobyandbob
    Nov 19, 2017 at 20:01
  • that is a greate solution thanks :) I have here no environment to test it, so does it produce links in a PDF?
    – youseeus
    Nov 20, 2017 at 10:27
  • 1
    Yes, this is why the \usepackage{hyperref} is included in the full example. This is for links from text to glossary only though. If you want links from glossary to text, you need to remove the nonumberlist from \usepackage[nonumberlist]{glossaries}.
    – Daniel D.
    Nov 22, 2017 at 1:41
  • Cool. You may want to add \renewcommand*\glossaryname{Glossary and Acronyms} right before \printglossary
    – Max N
    Nov 27, 2017 at 22:12
  • 2
    @MaxN why not use \printglossary[title={Glossary and Acronyms}] or just \printglossary[title={List of Terms}] Dec 1, 2019 at 18:25
5

I believe the cleaner way is to define a new command.
One that registers two entries1 :

\newcommand*{\newdualentry}[5][]{%
  \newglossaryentry{main-#2}{name={#4},%
  text={#3\glsadd{#2}},%
  description={{#5}},%
  #1
  }%
  \newglossaryentry{#2}{
  type=\acronymtype,
  first={#4 (#3)},
  name={#3\glsadd{main-#2}},
  description={\glslink{main-#2}{#4}}
  }%
}

Which has the following signature :

\newdualentry[⟨options⟩]{⟨label⟩}{⟨abbrv⟩}{⟨long⟩}{⟨description⟩}

You could use it like this :

\newdualentry{api}{API}{Application Programming Interface}{An Application Programming Interface (API) ...}

1 this snippet is a modified version of one given in the official documentation, p134. One could also give the see option to newacronym instruction, but that would unconditionally include the acronym to the list of acronyms. Also note that you could give a 6th argument, which would be the related glossary, which is of course "main" in the above snippet.

1
  • 1
    Regarding the see key, you might want to consider using glossaries-extra which has better cross-reference handling. Jul 3, 2017 at 8:59
3

I created a combination of several answers here, having an acronym and a glossary entry with the abbreviation included. I believe this is very easy to use and one does not have to check the acronyms in order to understand the short in glossary.

\documentclass{article}
\usepackage{parskip}
\usepackage[acronym]{glossaries}

\makeglossaries

% #1 - reference e.g. api
% #2 - Short e.g. API
% #3 - Full name e.g. Application Programming Interface
% #4 - Description
\newcommand{\newdefineabbreviation}[4]
{
    % Glossary entry
    \newglossaryentry{#1_glossary}
    {
        text={#2},
        long={#3},
        name={\glsentrylong{#1_glossary} (\glsentrytext{#1_glossary})},
        description={#4}
    }

    % Acronym
    \newglossaryentry{#1}
    {
        type=\acronymtype,
        name={\glsentrytext{#1_glossary}}, % Short
        description={\glsentrylong{#1_glossary}}, % Full name
        first={\glsentryname{#1_glossary}\glsadd{#1_glossary}},
        see=[Glossary:]{#1_glossary} % Reference to corresponding glossary entry
    }
}

\newdefineabbreviation
    {api}
    {API}
    {Application Programming Interface}
    {This is a description of the glossary entry}


\begin{document}

\printglossary[type=\acronymtype]
\printglossary[type=main]

\section{Introduction}
First time: \gls{api}

Second time: \gls{api}

\end{document}
1
  • For some reason the sorting is incorrect now. Entries defined by \newdefineabbreviation are always on top.
    – Paul
    Jun 21 at 12:02
1

A very simple possibility is to just set the type to \glsdefaulttype for new acronyms. The type specifies in which glossary the entry should go.

\newacronym \newacronym[⟨key-val list⟩]{⟨label⟩}{⟨abbrv⟩}{⟨long⟩}

This uses \newglossaryentry to create an entry with the given label in the glossary given by \acronymtype. You can specify a different glossary using the type key within the optional argument. The \newacronym command also uses the long, longplural, short and shortplural keys in \newglossaryentry to store the long and abbreviated forms and their plurals. (http://ftp.gwdg.de/pub/ctan/macros/latex/contrib/glossaries/glossaries-user.html#sec:acronyms)

 \newacronym[type=\glsdefaulttype]{api}{API}{Application Programming Interface}

to also add a description just do

 \newacronym[type=\glsdefaulttype, description={An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program that implements that API}]{api}{API}{Application Programming Interface}

You must log in to answer this question.

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