4

How can I redefine a glossary record, I am writing a custom class, and want to define some 'default' glossary terms as part of the class, however, may wish to redefine them at the document-level on a case-by-case basis.

At the moment, If I try to define a glossary record that already exists, it throws an already defined error, this is relating to the Glossaries package.

Here is an example, in this, a glossary record is created in the trivial class definition, as a default instance. As it stands, this compiles fine, however, if the attempted redefinition in the actual document is un-commented, the error is produced, reporting that the glossary record already exists.

%===================================================
%A Trivial Class
%===================================================
\begin{filecontents*}{democlass_v3.cls}
    \NeedsTeXFormat{LaTeX2e}
    \ProvidesClass{democlass_v3}[6/6/2013 democlass]
    \LoadClassWithOptions{article}

    \RequirePackage{glossaries}

    %<<<<<< FIRST DEFINITION >>>>>>%
    \newglossaryentry{pg}{   
        name={pg.},
        description={Short Page Abbreviation},
        first={pg.},
        firstplural={pp.},
        plural={pp.}
    }
\end{filecontents*}



\documentclass{democlass_v3}

%===================================================
%BELOW REDEFINITION THROWS ERROR IF UNCOMMENTED...
%===================================================
%<<<<<< ATTEMPTED REDEFINITION >>>>>>%
%\newglossaryentry{pg}{
%   name={page},
%   description={Short Page Abbreviation - Redefined},
%   first={page},
%   firstplural={pages},
%   plural={pages}
%}
%===================================================

\begin{document}

The page glossary record is: \gls{pg}

\end{document}

The specific Error thrown is:

Package glossaries Error: Glossary entry 'pg' has already been defined

4

I'm not sure that you really want to do this. However, here's a way:

\documentclass{article}

%%% This will go in the class file
\usepackage{glossaries}
\newcommand{\renewglossaryentry}[1]{%
  \ifglsentryexists{#1}
   {\global\csundef{glo@#1@name}}
   {}
   \newglossaryentry{#1}
}

\newglossaryentry{pg}{   
   name={pg.},
   description={Short Page Abbreviation},
   first={pg.},
   firstplural={pp.},
   plural={pp.}
}
%%% End of code in the class file

\makeglossaries

\renewglossaryentry{pg}{
   name={page},
   description={Short Page Abbreviation - Redefined},
   first={page},
   firstplural={pages},
   plural={pages}
}

\begin{document}

The page glossary record is: \gls{pg}

\printglossaries

\end{document}

As long as the glossary entry is not used before redefinition, all should go well.

enter image description here

  • That's it. I'll relax the renewglossaryentry{...} command at the beginning of the document, via \AtBeginDocument{\renewcommand\renewglossaryentry\relax} in the custom class, that way, it can only be used in preamble. – Nicholas Hamilton Oct 17 '13 at 22:13
  • @ADP Say \@onlypreamble\renewglossaryentry in the class file, which is the standard method for disabling commands after \begin{document}. – egreg Oct 17 '13 at 22:17
  • Well f me dead, you learn something new every day. Cheers. – Nicholas Hamilton Oct 17 '13 at 22:20

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.