# How to prevent hyphenation of abbreviations when glossary has the choice to use the long form?

I followed the advice given by @NicolaTalbot (by the way thanks for creating the great glossaries package!) in hyphenation of abbreviation using the glossaries package to avoid hyphenation of abbreviations.

However I have an example where the abbreviation still breaks, because if you use the \gls command and let glossaries select whether it should take the long form, or the short, the \acronymfont is not used.

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[]{glossaries}

\renewcommand*{\acronymfont}[1]{\mbox{#1}}
\newacronym[sort=BayHStA]{BayHStA}{BayHStA}{Bayerisches Hauptstaatsarchiv}

\begin{document}
\gls{BayHStA} and this is to show us that \gls{BayHStA} can break, even though it has been put into a mbox.
\end{document}


A modified solution based on @Georg's idea however can be applied and leads to the expected behaviour:

\newcommand{\nonbreakableacronym}[4][]{\hyphenation{#3}\newacronym[#1]{#2}{#3}{#4}}
\nonbreakableacronym[sort=BayHStA]{BayHStA}{BayHStA}{Bayerisches Hauptstaatsarchiv}


But I liked the neatness of Nicolas version, can something similar be achieved by just using another font command of glossaries?

There is an additional problem with the approach when you want to apply it. That is, that \hyphenation does not like words ending with a full stop. This is why:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[]{glossaries}

\renewcommand*{\acronymfont}[1]{\mbox{#1}}
\newcommand{\nonbreakableacronym}[4][]{\hyphenation{#3}\newacronym[#1]{#2}{#3}{#4}}
%\nonbreakableacronym[sort=BayHStA]{BayHStA}{BayHStA}{Bayerisches Hauptstaatsarchiv}
\newacronym[sort=BayHStA]{BayHStA}{bayHStA}{Bayerisches Hauptstaatsarchiv}
\nonbreakableacronym[sort=Abt.]{Abt.}{Abt.}{Abteilung}

\begin{document}
\gls{BayHStA} and this is to show us that \gls{BayHStA} can break, even though it has been put into a mbox. \Gls{BayHStA} works as well.

But I cannot use \gls{Abt.} without getting an error, because of the full stop.
\end{document}


produces an error:

! Not a letter.
<argument> Abt.

l.9 ...leacronym[sort=Abt.]{Abt.}{Abt.}{Abteilung}

Letters in \hyphenation words must have \lccode>0.
Proceed; I'll ignore the character I just read.


If you try with

\renewcommand{\acronymfont}[1]{\asillyundefinedmacro\mbox{#1}}


you will receive no error message about \asillyundefinedmacro being undefined.

You need to choose a style.

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[]{glossaries}

\setacronymstyle{long-short}

\renewcommand*{\acronymfont}[1]{\mbox{#1}}
\newacronym[sort=BayHStA]{BayHStA}{BayHStA}{Bayerisches Hauptstaatsarchiv}

\begin{document}

\gls{BayHStA} and this is to show us that \gls{BayHStA} can break, even though
it has been put into a mbox.

\end{document}


Compile and you'll get an “overfull \hbox" warning and no break in the short form of the acronym.

• Thanks egreg, what is happening when I do not set the \acronymstyle? I cannot find a reference in the manual for this. – TobiBS Jun 23 at 11:13
• @TobiBS Neither could I. I tried with that method, to realize that \acronymfont never got used. – egreg Jun 23 at 11:58