[Tex/LaTex] Glossaries package: ignore \newacronym if acronym has been defined before

acronymsconditionalsglossaries

I have a document consisting of multiple tex files, one per chapter, I am using the glossaries package to create list of acronyms. Ideally I want to define acronyms as close to where they are used, i.e. in the chapter tex files, in order to make the chapters self contained. However, this leads to some double definitions (acronyms defined in multiple chapters), something which the glossaries package does not like (it throws a "Glossary entry `xx' has already been defined" error).

Therefore I was wondering if it would be possible to change the \newacronym command in a way that it would ignore attempts at defining acronyms that are already known.

Basically it would boil down to an if-then-else structure which first checks whether the acronym label is already known, if so does nothing and if not executes the original \newacronym code.

Best Answer

You could use glossaries' \ifglsentryexists macro to define a \provideacronym macro which will only define an acronym if not already defined. Additionally (as in my MWE) you could redefine \newacronym as \provideacronym.

\documentclass{article}

\usepackage{glossaries}

\let\oldnewacronym\newacronym

\newcommand*{\provideacronym}[3]{%
  \ifglsentryexists{#1}{%
  }{%
    \oldnewacronym{#1}{#2}{#3}%
  }%
}

\renewcommand*{\newacronym}{\provideacronym}

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

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

\begin{document}

Some text about \gls{cd}.

\end{document}

enter image description here

Related Question