107

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?

youseeus
  • 1,193
  • 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 '11 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? – Damien Pollet Feb 08 '11 at 00:18

8 Answers8

131

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.
}
Martin H
  • 18,164
  • 1
    +1 awesome solution. Can something like this be done somehow in Lyx? – denilw Feb 05 '11 at 00: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 07 '11 at 09:51
  • 2
    http://tex.stackexchange.com/q/12346/978 is a follow up question on how to do this in Lyx – denilw Mar 01 '11 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 '14 at 22:41
  • 1
    The solution for this is to move the \glsadd{API} into the \first definition – Tom Brien Mar 04 '14 at 12:11
  • This is amazing. How would I create a new tex file with all my glossary entries, instead of having makeglossaries and \newglossaryentry in the section where I use all my packages (in main.tex). main.tex is massive and I would rather place these elsewhere. – Dhruv Ghulati Aug 24 '16 at 13:49
  • Just FYI for those adopting/migrating this answer, if you get two Glossary pages instead of an Acronym and a Glossary page, you might have toc selected when including the glossaries package, like so \usepackage[toc]{glossaries} – puk Jan 04 '17 at 16:27
  • Is there a similar way possible to reference an acronym in a list of symbols entry (descriptive column)? If I use the way described here, I get an empty field in the list of symbols, where the acronym symbol should be. – raedma May 15 '17 at 16:36
  • @krtek Then it could help to use automake \usepackage[acronym,automake]{glossaries}. See: Trouble making a glossary – Bobyandbob Nov 22 '17 at 13:44
  • If you use \glsaddallunused later on, you might get an unwanted comma at the end. A solution can be found in this answer to Spurious comma by use of \glsaddallunused. – dexteritas Sep 12 '23 at 14:41
  • I found that using \newglossaryentry for defining acronyms resulted in a blank () when referencing ti via \glsxtrfull / \acrfull. To fix it, I used \newacronym[see={[Glossary:]{#1_gls}}]{#1}{#1}{#2\glsadd{#1_gls}} instead of \newglossaryentry for defining acronyms and it now works fine :-) Ref https://en.wikibooks.org/wiki/LaTeX/Glossary#Dual_entries_with_reference_to_a_glossary_entry_from_an_acronym – starbeamrainbowlabs Oct 10 '23 at 12:01
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.

Coxa
  • 1,373
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.

Speravir
  • 19,491
OSHi
  • 121
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}
datu-puti
  • 239
  • 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. – Marcus Karpoff Feb 11 '19 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: https://tex.stackexchange.com/a/74354/11820). Further, I don't think the \glspluralsuffix is needed in the braces after the \glsentryname{#1}. – white_gecko Dec 01 '19 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 '23 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}
  • 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 '17 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 '17 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 '17 at 01:41
  • Cool. You may want to add \renewcommand*\glossaryname{Glossary and Acronyms} right before \printglossary – Max N Nov 27 '17 at 22:12
  • 2
    @MaxN why not use \printglossary[title={Glossary and Acronyms}] or just \printglossary[title={List of Terms}] – white_gecko Dec 01 '19 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.

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}

  • For some reason the sorting is incorrect now. Entries defined by \newdefineabbreviation are always on top. – Paul Jun 21 '23 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}
white_gecko
  • 2,101