13

I am using the glossaries package to handle acronyms and came across the task to automatically decide the use of "a" or "an" in front of them.

\documentclass{scrbook}
\usepackage{hyperref}
\usepackage[toc,acronym,nopostdot,nomain,style=alttree,shortcuts]{glossaries}
  \setacronymstyle{long-sc-short}
  \makeglossaries
  \newacronym{sma}{sma}{SubMiniature version A}
\begin{document}
  a \ac{sma}\\ % gives "a SubMiniature version A (SMA)"
  a \ac{sma}   % gives "a SMA"
\end{document}

How can I achieve that the second yields "an SMA"? I do not mind indicating once for each acronym if it needs the "n" or not when the short form is used.

I do know about the glossaries-prefix package that allows to define prefixes for all the different cases (first-use, plural, singular, etc.). However, using this package, I would have to manually use its different commands (\pgls etc.) instead of the short acronym commands (\ac etc.).

Is there a way to do this automatically?

  • Hello, this is an interesting question! Could you please add a short and full minimal example so that we know what your setup is? – yo' Feb 7 '16 at 14:43
  • 1
    Since the text you want to change has nothing to do with the acronym itself, but the word before it, wouldn't you need some sort of \indef macro whose form depends on the following \ac macro? – Alan Munn Feb 7 '16 at 17:42
  • 1
    Can you clarify how you want this automation without using \pgls? As @AlanMunn commented, the a will need to be replaced with a command since a \ac{sma} can't automatically be converted into an \ac{sma} otherwise (\ac can't look behind). It's much simpler to replace a \ac{sma} with \pgls{sma} than it is to replace it with, say, \indefinitearticle{sma} \ac{sma} (which is essentially what \pgls does). – Nicola Talbot Feb 8 '16 at 13:48
  • I am starting to realize that this is not feasible automatically and it really seems that I won't get out of doing this with \pgls. Now, I am looking for a method so that I can continue using the \ac commands. And it would finally be great to have a method that defaults the prefix to "a" or "an" depending on the first letter so that one only has to indicate something if this method fails. For the moment, I am struggling with non-expandable commands (especially loops) in doing this. – Tiny Tot Feb 8 '16 at 14:13
  • I posted a follow-up question to this. – Tiny Tot Feb 8 '16 at 15:14
1

Maybe this can help get you started:

My guess is that the ideal solution would be a massively complicated package (like the hyphenation pattern recognition for various languages) to recognize a vs an etc. Since this isn't within my abilities here's a proposal.

If your usage with articles is truly first = Article A, and abbreviations = Article B then I would propose adding some keys to glossaries and detecting whether the acronym has been used or not to recognize which of the articles to use.

Note: I haven't added error trapping to the command to recognize when a user tried to use the article substitution but didn't define it, but I did define the command so that the user can pass an article on an as needed basis as an optional argument.

\documentclass{scrbook}
\usepackage{hyperref}
\usepackage{xparse}
\usepackage[toc,acronym,nopostdot,nomain,style=alttree,shortcuts]{glossaries}

  \setacronymstyle{long-sc-short}


  \glsaddkey
  {article-abrv}        % new key
  {\relax}          % default value if "article-abrv" isn't used in \newglossaryentry
  {\glsentryArtA}   % analogous to \glsentrytext
  {\GlsentryArtA}   % analogous to \Glsentrytext
  {\glsArtA}        % analogous to \glstext
  {\GlsArtA}        % analogous to \Glstext
  {\GLSArtA}        % analogous to \GLStext

  \glsaddkey
  {article-long}    % new key
  {\relax}          % default value if "article-long" isn't used in \newglossaryentry
  {\glsentryArtL}   % analogous to \glsentrytext
  {\GlsentryArtL}   % analogous to \Glsentrytext
  {\glsArtL}        % analogous to \glstext
  {\GlsArtL}        % analogous to \Glstext
  {\GLSArtL}        % analogous to \GLStext


    \DeclareDocumentCommand{\aac}{o m}{%
        \IfValueTF{#1}{%
            \ifglsused{\glsentryshort{#2}}%
            {\glsdisp{#2}{#1~\ac{#2}}}%TRUE
            {\glsdisp{#2}{#1~\ac{#2}}}%FALSE
        }{%
            \ifglsused{\glsentryshort{#2}}%
            {\glsdisp{#2}{\glsArtA{#2}~\ac{#2}}}%TRUE
            {\glsdisp{#2}{\glsArtL{#2}~\ac{#2}}}%FALSE
        }%
    }

    \DeclareDocumentCommand{\Aac}{o m}{%
        \IfValueTF{#1}{%
            \ifglsused{\glsentryshort{#2}}%
            {\glsdisp{#2}{#1~\ac{#2}}}%TRUE
            {\glsdisp{#2}{#1~\ac{#2}}}%FALSE
        }{%
        \ifglsused{\glsentryshort{#2}}%
        {\glsdisp{#2}{\GlsArtA{#2}~\ac{#2}}}%TRUE
        {\glsdisp{#2}{\GlsArtL{#2}~\ac{#2}}}%FALSE
    }%
  }

  \makeglossaries

  \newacronym[article-abrv={an},article-long={a}]{sma}{sma}{SubMiniature version A}  
  \newacronym{uv}{UV}{Ultraviolet}

\begin{document}

\Aac{sma}

\Aac{sma}

Lowercase example:  \aac{sma}

Override example:  \aac[the]{sma}

%\Aac{uv}% code has no error trapping to let you use \aac as a replacement to \ac

\end{document}

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.