# Style acronyms long forms and avoid description fields for those that don't have it defined

I'm working with the glossaries package, and wanted to modify the long-short style of the glossary like this (edited, it didn't came from a .tex compilation):

For acronyms only (as the real document I'm working on needs to contain both acronyms and normal terms into a single glossary):

• Don't put any description if it doesn't have that tag defined in the optional argument, and put the short form of the acronym in bold, a line, and the long form in plain format, all of these in the same line (i.e. avoid any space for the description field).
• Otherwise, put the long form first, then the short form between parentheses, and the given description below (as in the long-short glossary style).

The following MWE shows what I've got so far:

\documentclass[12pt]{article}

\usepackage[xindy]{glossaries}
\usepackage{ifthen}

\makeglossaries

% Style the acronyms
\setacronymstyle{long-short}
\renewcommand*{\acronymsort}[2]{#1} % Order by short form
\renewcommand*{\acronymentry}[1]{%
\ifthenelse{\equal{\glsentrydesc{#1}}{\glsentrylong{#1}}}{%
\acronymfont{\glsentryshort{#1} --- \glsentrylong{#1}}%
}{%
\acronymfont{\glsentrylong{#1} (\glsentryshort{#1})}%
}%
}

\newacronym{one}{one}{The number one}
\newacronym{two}{two}{The number two}
\newacronym[description={Just a number with a meaningful description}]{three}{three}{The number three}

\begin{document}
The numbers \acrshort{one}, \acrshort{two} and \acrshort{three}...
\setglossarystyle{altlisthypergroup}
\printglossary[type=main]
\end{document}

• The glossary now shows both glossary terms and acronyms (as I'm not using the acronym option for the package).
• For acronyms, I managed to style the acronym long and short forms, according to the value of the description field (the documentation for the package states that the long-short style "copies" the acronym long form into the description field if the user didn't define it).

And the output of compiling the given code is:

Is there a way to:

• avoid the description field for the acronyms that don't have it user-defined?
• change the long form for these acronyms to plain format?

Any help will be much appreciated :)

I think you might be better off using the glossaries-extra extension package, which allows different abbreviation styles. This package internally loads glossaries, but there are some subtle differences. In particular it uses a completely different abbreviation mechanism. Each entry now has an associated category (set with the category key) and the abbreviation style is set for a particular category using:

\setabbreviationstyle[category-label]{style-name}


where category-label is the category label and style-name is the style. Abbreviations are then defined using \newabbreviation. This has the same syntax as \newacronym. To make it easier to transfer documents over from just the base glossaries package to glossaries-extra, \newacronym is redefined to internally use \newabbreviation (with category=acronym). The other difference is that the short form is obtained using \glsxtrshort instead of \acrshort. (This allows for different abbreviation fonts.)

So your MWE can be changed to:

\documentclass{article}

\usepackage[xindy]{glossaries-extra}

\makeglossaries

\setabbreviationstyle[withoutdesc]{short-long}
\setabbreviationstyle[withdesc]{long-short-desc}

\newabbreviation[category=withoutdesc]{one}{one}{The number one}
\newabbreviation[category=withoutdesc]{two}{two}{The number two}
\newabbreviation[category=withdesc,
description={Just a number with a meaningful description}]
{three}{three}{The number three}

\newglossarystyle{customlist}{%
\setglossarystyle{listhypergroup}%
\renewcommand*{\glossentry}[2]{%
\item[\glsentryitem{##1}%
\glstarget{##1}{\glossentryname{##1}}]%
\glsifcategory{##1}{withoutdesc}%
{ --- \glsentrylong{##1}}%
{%
\glossentrydesc{##1}%
}%
\glspostdescription\space ##2%
}%
}
\setglossarystyle{customlist}%

\begin{document}
The numbers \glsxtrshort{one}, \glsxtrshort{two} and \glsxtrshort{three}...

\printglossary[type=main]
\end{document}


This assigns two categories: withdesc for abbreviations with a description and withoutdesc for abbreviations without a description. This allows one style (short-long) to be applied to the abbreviations without a description and another style (long-short-desc) to be applied to the abbreviations with a description.

A new glossary style is also defined based on the listhypergroup style that checks the entry's category (using \glsifcategory) to determine whether or not to insert a paragraph break followed by the description.

I'm guessing that you've used \acrshort because you don't want expansion on first use. There are abbreviation styles that can automatically do this for you so that you can just use \gls instead.

In the first case (abbreviations with no descriptions), this can easily be achieved by replacing the short-long style with short-nolong:

\setabbreviationstyle[withoutdesc]{short-nolong}


For the withdesc category, there is a short-nolong-desc style but this would reverse the order in the name field. There isn't a nolong-short-desc style, but a style can be defined based on an existing style like this:

\newabbreviationstyle{nolong-short-desc}%
{% base it on short-nolong-desc:
\GlsXtrUseAbbrStyleSetup{short-nolong-desc}%
}%
{% base it on long-short style:
\GlsXtrUseAbbrStyleFmts{long-short}%
}


Now the style can be set for the withdesc category:

\setabbreviationstyle[withdesc]{nolong-short-desc}


The complete document is now:

\documentclass{article}

\usepackage[xindy]{glossaries-extra}

\makeglossaries

\newabbreviationstyle{nolong-short-desc}%
{% base it on short-nolong-desc:
\GlsXtrUseAbbrStyleSetup{short-nolong-desc}%
}%
{% base it on long-short style:
\GlsXtrUseAbbrStyleFmts{long-short}%
}

\setabbreviationstyle[withoutdesc]{short-nolong}
\setabbreviationstyle[withdesc]{nolong-short-desc}

\newabbreviation[category=withoutdesc]{one}{one}{The number one}
\newabbreviation[category=withoutdesc]{two}{two}{The number two}
\newabbreviation[category=withdesc,
description={Just a number with a meaningful description}]
{three}{three}{The number three}

\newglossarystyle{customlist}{%
\setglossarystyle{listhypergroup}%
\renewcommand*{\glossentry}[2]{%
\item[\glsentryitem{##1}%
\glstarget{##1}{\glossentryname{##1}}]%
\glsifcategory{##1}{withoutdesc}%
{ --- \glsentrylong{##1}}%
{%
\glossentrydesc{##1}%
}%
\glspostdescription\space ##2%
}%
}
\setglossarystyle{customlist}%

\begin{document}
The numbers \gls{one}, \gls{two} and \gls{three}...

\printglossary[type=main]
\end{document}


The resulting document looks like:

You can provide your own shortcut command so that you don't need to keep setting the category key. For example:

% syntax: \mynewabbrv[options]{label}{short}{long}{description}
\newcommand*{\mynewabbrv}[5][]{%
\ifblank{#5}%
{\newabbreviation[category=withoutdesc,#1]{#2}{#3}{#4}}%
{\newabbreviation[category=withdesc,description={#5},#1]{#2}{#3}{#4}}%
}

\mynewabbrv{one}{one}{The number one}{}
\mynewabbrv{two}{two}{The number two}{}
\mynewabbrv{three}{three}{The number three}
{Just a number with a meaningful description}


This uses etoolbox's \ifblank to determine if the final argument is empty.

Alternatively, if you want to keep your original syntax:

\makeatletter
\define@key{mynewabbrv}{description}{\def\mydesc{#1}}
\makeatother

\renewcommand*{\newacronym}[4][]{%
\def\mydesc{}%
\setkeys*{mynewabbrv}{#1}%
\ifdefempty{\mydesc}%
{\newabbreviation[category=withoutdesc,#1]{#2}{#3}{#4}}%
{\newabbreviation[category=withdesc,#1]{#2}{#3}{#4}}%
}

\newacronym{one}{one}{The number one}
\newacronym{two}{two}{The number two}
\newacronym[description={Just a number with a meaningful description}]
{three}{three}{The number three}


This allows the description key to be extracted from the options list.

• Wow. o.o I'm happy I finially could read this. It's really clear, educational, and even you showed me other ways to do the same! :D Definitely it was worth it. Thank you so much! =) – user101590 Mar 2 '17 at 9:11