2

I use \ifglsused command in my document. It works well as long as the entry it refers to is already used (e.g., when line 2. \ac{at}.\\ in MWE body is uncommented). However, if this entry has not been used in the document, the following error is returned by LaTeX:

l.27 Package etoolbox Error: Boolean '\ifglo@at@flag' undefined.

Solutions like adding \glsadd{at} to the preamble or changing \GlsXtrLoadResources[src={entries}] to \newacronym{ao}{AO}{acronym one} and \newacronym{at}{AT}{acronym two} are not sufficient, because in such cases an unnecessary entry for (unused) at acronym entry would be added to the list of acronyms.

MWE:

\documentclass{article}

\usepackage[record,acronym,shortcuts=ac]{glossaries-extra}

\usepackage{relsize}
\setabbreviationstyle[\acronymtype]{long-short-sm}

\usepackage{filecontents}
\begin{filecontents}{entries.bib}
    @acronym{ao,
        long      = {acronym one},
        short     = {AO}
    }

    @acronym{at,
        long      = {acronym two},
        short     = {AT}
    }
\end{filecontents}

\GlsXtrLoadResources[src={entries}]

\begin{document}

    1. \ac{ao}.\\
%   2. \ac{at}.\\
    3. \ifglsused{at}{Acronym 2 has been used}{Acronym 2 has \emph{not} been used}.

    \printunsrtglossary[type=\acronymtype]

\end{document}

Desired output file: Desired result.

Is it possible to obtain such result, i.e., to be able to use \ifglsused{at}{…}{…} even when there are no commands like \ac{at} in the document (but there still might be an entry for at in acronyms’ BIB source file for future usage)?

2

This bug is now fixed in version 1.33 of glossaries-extra (2018-07-26).

A patch for older versions is to redefine \ifglsused as follows:

\renewcommand*{\ifglsused}[3]{%
  \glsdoifexists{#1}{\ifbool{glo@\glsdetoklabel{#1}@flag}{#2}{#3}}%
}

The base glossaries package just defines this command as:

\newcommand*{\ifglsused}[3]{%
  \ifbool{glo@\glsdetoklabel{#1}@flag}{#2}{#3}%
}

which relies on \ifbool to generate an error and do nothing if the supplied boolean variable doesn't exist.

Usually glossaries uses \glsdoifexists for cases that require the existence of the given entry. If the entry given by the label isn't defined this command triggers an error. The base glossaries package omits an extra check in \ifglsused as it can defer to \ifbool. (Although it doesn't make much of a saving omitting just one test in the code, \ifglsused is internally used many times, so it adds up, and there are users who complain about the build time when creating a document that uses the glossaries package.)

The glossaries-extra package provides the package option undefaction=warn, which changes the behaviour of \glsdoifexists to generate a warning instead of an error message and, if used in the document environment, ?? will be displayed in the text. This means that \ifglsused now does need an extra test to allow for this setting.

If only \ifglsentryexists is used (instead of \glsdoifexists) then you won't get any indication when something goes wrong. For example, if the label is misspelt or if bib2gls failed to select the entry. This is why the new version uses \glsdoifexists to make it consistent with the base package's behaviour. The purpose of undefaction=warn is to simply change errors to warnings and provide a visual indicator, without making any other modification. This mode was originally intended as way of working on draft documents with incomplete references. When bib2gls and the record were later introduced, the undefaction=warn option conveniently deals with the first LaTeX run where no entries are defined (because they're not defined until bib2gls creates the associated files).

Version 1.34 (2018-07-29)¹ provides \GlsXtrIfUnusedOrUndefined{label}{true}{false} which does true if either the entry given by label is undefined or if it hasn't been marked as used. This may be a better command for use with bib2gls.

Neither \ifglsused nor \GlsXtrIfUnusedOrUndefined should be used in a post-link hook to reference the associated entry's first use flag, as it will have already been unset by the time the hook is used. Instead you can use \glsxtrifwasfirstuse. For example:

\documentclass{article}

\usepackage[undefaction=warn]{glossaries-extra}

\setabbreviationstyle{long-noshort}
\newabbreviation{ab}{AB}{Abbreviation}

\glsdefpostlink{abbreviation}{\glsxtrifwasfirstuse{ (\glsps{\glslabel})}{}}

\begin{document}
First use: \gls{ab}.
Next use: \gls{ab}.

\end{document}

First use: Abbreviation (AB). Next use: Abbreviation.


¹ Allow a few days for it to reach the TeX distributions.

  • 1
    Your patch will not output anything if the acronym is not used. Imho the op is more looking for \ifglsentryexists{#1}{\ifbool{glo@\glsdetoklabel{#1}@flag}{#2}{#3}}{#3}%. – Ulrike Fischer Jul 26 '18 at 11:31
  • @UlrikeFischer That's intentional and documented in the manual. The patch will produce ?? in the text, which is what all the \gls etc commands do when the entry isn't defined when used with the record option. – Nicola Talbot Jul 26 '18 at 11:47
  • This is actually useful for me, as I define own command like \newcommand*{\ace}[1]{\ifglsentryused{#1}{\acl{#1}}{\ac{#1}}} that expands to "acronym two (AT)" on first use and to "acronym two" on subsequent ones. Here, \ifglsentryused might be defined as \newcommand* according to Ulrike Fischer's comment (I don't know if this is the best solution, but the effect is achieved). \ifglsused is probably more useful for other purposes, as basing \ace on it would return undesired "??" in case when only \ace's are used in the document. – Peter Jul 26 '18 at 13:25
  • @Peter I'm sorry, I don't understand the purpose of your \ace command. Why not change the abbreviation style or use the post-link hook to achieve that effect with just \ac? – Nicola Talbot Jul 26 '18 at 16:57
  • Normally I use the \ac command that gives “acronym two (AT)” on first use in a given chapter (using \preto\chapter{\glsresetall}) and just “AT” further with the chosen style. However, I would like to have a special command that uses the long version (i.e., “acronym two”, like \acl) everywhere to emphasize the meaning of the acronym. Additionally, when it is the first acronym use in a chapter, the short version should be appended to the long one (i.e., “acronym two (AT)” as in the first use of \ac). That is why I defined \ace that acts like \ac on first time and like \acl later. – Peter Jul 26 '18 at 18:44

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.