77

I'm using glossaries at LaTeX:

I have a Acronyms (eg. API) that should be explained in the Glossary. It should be linked to the Glossary at occurrence but should be written out at first occurrence.

How can I do that?


Explanation:

I want something like that:

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

  • 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
  • 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 8 '11 at 0:18
98

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 'Nomenclature'

\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

  • 1
    +1 awesome solution. Can something like this be done somehow in Lyx? – denilw Feb 5 '11 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 '11 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 '11 at 18:12
  • 1
    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 4 '14 at 12:11
21

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.

  • 4
    Nice solution, but I'd use first={\glsentrylong{api}} (\glsentryname{api}). – sergej Jun 19 '14 at 18:27
  • 2
    And also add firstplural={\glsentrylong{api}\glspluralsuffix\ (\glsentryname{api}\glspluralsuffix )} – Jeffrey Goldberg Sep 23 '15 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. – elBradford Jan 15 '18 at 18:58
11

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.

6

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}
  • 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 at 16:39
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
    Regarding the see key, you might want to consider using glossaries-extra which has better cross-reference handling. – Nicola Talbot Jul 3 '17 at 8:59
5

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
  • 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 1:41
  • Cool. You may want to add \renewcommand*\glossaryname{Glossary and Acronyms} right before \printglossary – Max N Nov 27 '17 at 22:12
1

I am trying to implement this myself, and I am torn between making the areas where I would \glsadd{aa} work correctly or just make a newcommand to use \gls{aa}\glsadd{aag} automatically. Notice also that the "plural" option is not very smart and makes this example Alphabet Aerobicss.

    \documentclass{article}
    \usepackage{hyperref}
    \usepackage[acronym,nopostdot]{glossaries}
    \makeglossaries
    % [#1] = unused options
    % {#2} = aa
    % {#3} = AA
    % {#4} = A symbol e.g. \ensuremath\mathbf{A} (somtimes unused)
    % {#5} = Alphabet Aerobics
    % {#6} = Paragraphs of information about AAs.
    \newcommand*{\newdualentry}[6][]{%
        %%% The glossary entry the acronym links to
        \longnewglossaryentry{{#2}g}%
        {%
            name={#3}%title of glossary entry
        }%
        {#6}%description, possibly multiparagraph
        %
        %%% define the acronym and use the see= option
        \newglossaryentry{#2}%
        {%
            type=\acronymtype,%
            description={#5},%
            symbol={#4},%
            name={#3},%
            first={#5 (#3)},%
            plural={#3s},%
            firstplural={#5s (#3s)},%
            see=[Glossary:]{{#2}g}%
        }%
    }%

    \newcommand*{\glac}[2][]{\gls{#2}\glsadd{{#2}g}}
    \newcommand*{\Glac}[2][]{\Gls{#2}\glsadd{{#2}g}}
    \newcommand*{\glacpl}[2][]{\glspl{#2}\glsadd{{#2}g}}
    \newcommand*{\Glacpl}[2][]{\Glspl{#2}\glsadd{{#2}g}}

    \newdualentry[]{aa}{AA}{\ensuremath{\mathbf{A}}}{Alphabet Aerobics}%
    {%
        Ready?\\Begin.\\Artificial amateurs aren't at all amazing.
    }

    \begin{document}
    \Glacpl[]{aa}.
    \clearpage
    Second \glac{aa}.

    \clearpage
    \printglossary[type=\acronymtype]
    \clearpage
    \printglossary[type=main]
    \end{document}
  • ...does this answer the question? – Werner May 24 '17 at 0:27
  • I thought so at first. It was similar to the accepted answer, but used newcommands instead of manually entering information twice. It makes the glossary reference all the page numbers where the \gls{aa} is used versus just the first page. I am not sure which the original author would prefer since their post was not entirely clear to me. It does say "Alphabet Aerobics" as the glossary entry rather than "AA" which would have matched their example better. What else is wrong with? – urnlint May 24 '17 at 16:22
  • "What else is wrong with [it]?" It doesn't seem authoritative in answering the question. As in: "This is how you do it..." or "Here is an approach that solves your question...". You start by saying that "[you're] trying to implement this yourself." It seems like you're seeking input. Just my opinion though. – Werner May 24 '17 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.