# Context-sensitive braces with glossaries

I use the glossaries package for handling my acronyms. On first use, I want the acronyms to be printed in the form "long (short)". However, it may well be that the text where an acronym is used for the first time is already embedded in parentheses, such as item 2. in the following example.

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}             % Umlaute
\usepackage[T1]{fontenc}

\usepackage[nomain,acronym,nonumberlist]{glossaries}

\makeglossaries
\newacronym{nemesis}{NEMESIS}{Netherlands Mental Health Survey and Incidence Study}

\begin{document}
\printglossaries

\section*{Dummy text}
\noindent \textbf{Everything is fine in a normal textenvironment:}\\
The \gls{nemesis} indicates something.\vspace{1cm}

\glsresetall

\noindent\textbf{If used first inside parentheses, the result looks ugly:}\\
Some studies' names (e.g. the \gls{nemesis}) make interesting acronyms.\vspace{1cm}

\noindent\textbf{This is what it should look like:}\\
Some studies' names (e.g. the Netherlands Mental Health Survey and Incidence Study [NEMESIS]) make interesting acronyms.

\end{document}


Is there any way to get glossaries to set the parentheses correctly?

– user31729
Apr 30, 2016 at 9:26
• Thanks, I have inserted an MWE into the original posting. Apr 30, 2016 at 13:05

First you need a command that will toggle between ( ... ) and [ ... ]. This is a bit like the automated quoting system provided by packages like csquotes. In general I think csquotes is much better than quotmark, but here we could hijack quotmark and use it for parenthetical material. That way it shouldn't interfere if you happen to need csquotes. For example:

\documentclass{article}

\usepackage{quotmark}

\setquotemarks{{(}{)},{[}{]}}
\newcommand{\paren}[1]{\tqt{#1}}

\begin{document}
\paren{Some \paren{parenthetical} text}
\end{document}


This produces:

Now instead of explicitly using (...), you can use \paren{...}. There may be other neater ways of defining \param, but once you've settled on your preferred definition, you need an acronym style uses this command.

The simplest approach is to use the extension package glossaries-extra, but make sure you have up-to-date versions of both glossaries (at least v4.23) and glossaries-extra (at least v1.04):

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{quotmark}
\setquotemarks{{(}{)},{[}{]}}
\newcommand{\paren}[1]{\tqt{#1}}

\usepackage[nomain,acronym,nonumberlist,nopostdot=false]{glossaries-extra}

\makeglossaries

\setabbreviationstyle[acronym]{long-short-user}

\renewcommand*{\glsxtruserparen}[2]{%
\glsxtrfullsep{#2}%
\paren{#1\ifglshasfield{\glsxtruserfield}{#2}{, \glscurrentfieldvalue}{}}%
}

\newacronym{nemesis}{NEMESIS}{Netherlands Mental Health Survey and
Incidence Study}

\begin{document}

\printglossaries

\section*{Dummy text}
\noindent \textbf{Everything is fine in a normal textenvironment:}\\
The \gls{nemesis} indicates something.\vspace{1cm}

\glsresetall

\noindent\textbf{First use inside parentheses:}\\
Some studies' names \paren{e.g. the \gls{nemesis}} make interesting
acronyms.\vspace{1cm}

\noindent\textbf{Compare with:}\\
Some studies' names (e.g. the Netherlands Mental Health Survey and
Incidence Study [NEMESIS]) make interesting acronyms.

\end{document}


This produces:

If you want to just use glossaries without the extension package, then you need to define a custom style:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{quotmark}
\setquotemarks{{(}{)},{[}{]}}
\newcommand{\paren}[1]{\tqt{#1}}

\usepackage[nomain,acronym,nonumberlist]{glossaries}

\makeglossaries

\newacronymstyle{long-paren-short}%
{%
\ifglshaslong{\glslabel}{\glsgenacfmt}{\glsgenentryfmt}%
}%
{%
\renewcommand*{\GenericAcronymFields}{description={\the\glslongtok}}%
\renewcommand*{\genacrfullformat}[2]{%
\glsentrylong{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshort{##1}}}%
}%
\renewcommand*{\Genacrfullformat}[2]{%
\Glsentrylong{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshort{##1}}}%
}%
\renewcommand*{\genplacrfullformat}[2]{%
\glsentrylongpl{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshortpl{##1}}}%
}%
\renewcommand*{\Genplacrfullformat}[2]{%
\Glsentrylongpl{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshortpl{##1}}}%
}%
\renewcommand*{\acronymentry}[1]{\acronymfont{\glsentryshort{##1}}}%
\renewcommand*{\acronymsort}[2]{##1}%
\renewcommand*{\acronymfont}[1]{##1}%
\renewcommand*{\firstacronymfont}[1]{\acronymfont{##1}}%
\renewcommand*{\acrpluralsuffix}{\glspluralsuffix}%
}

\setacronymstyle{long-paren-short}

\newacronym{nemesis}{NEMESIS}{Netherlands Mental Health Survey and
Incidence Study}

\begin{document}

\printglossaries

\section*{Dummy text}
\noindent \textbf{Everything is fine in a normal textenvironment:}\\
The \gls{nemesis} indicates something.\vspace{1cm}

\glsresetall

\noindent\textbf{First use inside parentheses:}\\
Some studies' names \paren{e.g. the \gls{nemesis}} make interesting
acronyms.\vspace{1cm}

\noindent\textbf{Compare with:}\\
Some studies' names (e.g. the Netherlands Mental Health Survey and
Incidence Study [NEMESIS]) make interesting acronyms.

\end{document}


This produces the same result.

Edit:

When using \newacronymstyle (or \newabbreviationstyle with glossaries-extra) there can be a difference between using \glsfirst and the first use of \gls. This is due to the line:

  \ifglshaslong{\glslabel}{\glsgenacfmt}{\glsgenentryfmt}%


in the acronym style (with glossaries or due to the redefinition of \glsentryfmt in glossaries-extra). This essentially determines if the entry is an abbreviation (has a long value) or if it's a normal (or regular) entry.

Both glossaries and glossaries-extra use \glsgenentryfmt for regular entries. This uses the value of the first field on first use (or the firstplural field on first use if \glspl has been used) and the value of the text field on subsequent use (or the plural field for \glspl). Since \glsfirst displays the value of the first field, it's the same as \gls on first use for regular entries.

The default definition of \newacronym also behaves in this way, where the first field is set to \acrfullformat{long}{short}, but this can mess up \Gls (if \acrfullformat is redefined to show the short form first) and the insert from the final optional argument of commands like \gls can't be treated properly.

This is why the newer ways of defining abbreviations (using \setacronymstyle in glossaries or \newabbreviation in glossaries-extra) don't use the first key when using \gls on first use. This means that commands like \Gls can now function correctly, but it means that \glsfirst is no longer appropriate for these entries. Instead you can use commands like \acrfull (with glossaries) or \glsxtrfull (with glossaries-extra) to show the full form.

If you really want to, you can modify the style to set the first key, as in the modified example below:

\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{quotmark}
\setquotemarks{{(}{)},{[}{]}}
\newcommand{\paren}[1]{\tqt{#1}}

\usepackage[nomain,acronym,nonumberlist]{glossaries}

\makeglossaries

\newacronymstyle{long-paren-short}%
{%
\ifglshaslong{\glslabel}{\glsgenacfmt}{\glsgenentryfmt}%
}%
{%
\renewcommand*{\GenericAcronymFields}{%
first={\the\glslongtok\space\protect\paren{\the\glsshorttok}},
description={\the\glslongtok}}%
\renewcommand*{\genacrfullformat}[2]{%
\glsentrylong{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshort{##1}}}%
}%
\renewcommand*{\Genacrfullformat}[2]{%
\Glsentrylong{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshort{##1}}}%
}%
\renewcommand*{\genplacrfullformat}[2]{%
\glsentrylongpl{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshortpl{##1}}}%
}%
\renewcommand*{\Genplacrfullformat}[2]{%
\Glsentrylongpl{##1}##2\space
\paren{\protect\firstacronymfont{\glsentryshortpl{##1}}}%
}%
\renewcommand*{\acronymentry}[1]{\acronymfont{\glsentryshort{##1}}}%
\renewcommand*{\acronymsort}[2]{##1}%
\renewcommand*{\acronymfont}[1]{##1}%
\renewcommand*{\firstacronymfont}[1]{\acronymfont{##1}}%
\renewcommand*{\acrpluralsuffix}{\glspluralsuffix}%
}

\setacronymstyle{long-paren-short}

\newacronym{nemesis}{NEMESIS}{Netherlands Mental Health Survey and
Incidence Study}

\begin{document}

\printglossaries

The \glsfirst{nemesis}['s] findings show that\ldots

The \acrfull{nemesis}['s] findings show that\ldots

The \gls{nemesis}['s] findings show that\ldots

\end{document}


Note the difference in the treatment of the optional argument.

In the case of \glsfirst, the 's is just appended after the parentheses, which looks weird, whereas with the other two cases, the 's has been inserted after the long form.

• Sorry that I'm replying so late -- the notification mail was falsely recognizes as spam, so I didn't see it immediately. The solution is perfect -- in fact, as user of the csquotes-package, I had already thought of doing the nesting in a csquotes-like manner, i just didn't know how to get this done. May 14, 2016 at 16:30
• Just for the record: In case you write your own macros and finish them with \xspace, consider to insert \qt@endquote in \xspaceaddexceptions: \makeatletter\xspaceaddexceptions{\qt@endquote}\makeatother May 27, 2016 at 14:48
• Using \setquotemarks{{(}{)},{[}{]}} and the \paren-construct works fine, but do you know how to make these changes persistent? Every time \selectlanguage{languagename} from babel is used in the document, \setquotemarks{{(}{)},{[}{]}} has to be called again, otherwise the default quotation marks instead of the parentheses will be used further on. May 27, 2016 at 15:16
• @Andreas Try loading quotmark before babel. May 27, 2016 at 15:30
• Great, that did the trick! May 27, 2016 at 15:48