1

I'm trying to create a new style to display my acronyms in the text with the glossaries-extra package and the bib2gls tool (for I need to have other lists with different sorting options).

I need to create a new style to display them in the text, because for acronyms that aren't in my mother language must be shown in their original long form in the list, such as

FFT Fast Fourier Transform

But in the text, the translated text must be shown outside the parentheses and the original text inside them in the fist call of \gls{label}, like

... a Transformada Rápida de Fourier (FFT, Fast Fourier Transform) ...

An abbreviation style such as long-short-user would almost fulfill my need, except for the fact that it would put the user1 key inside the parentheses.

So I tried to create a new abbreviation style (based on the glossaries-extra manual) and a new parenthetical content command \glsxtruserpareninverted like

\documentclass{memoir}
\usepackage{ifluatex} % Verificação do compilador utilizado
\ifluatex
\usepackage{fontspec}
\else
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\fi

\usepackage[english]{babel}

\usepackage[record,style=long,nonumberlist,acronyms]{glossaries-extra}

\newcommand*{\glsxtruserpareninverted}[2]{%
    \glsxtrfullsep{#2}%
    \glsxtrparen
    {#1\ifglshasfield{\glsxtruserfield}{#2}{, \glsaccesslong{#2}}{}}
}

\newabbreviationstyle{user-short-long}
{%
    \renewcommand*{\CustomAbbreviationFields}{%
        name={\glsxtrlongshortname},
        sort={\the\glsshorttok},
        plural={\protect\glsabbrvuserfont{\the\glsshortpltok}},%
        first={\protect\glsfirstlongfont{\the\glslongtok}%
            \protect\glsxtrfullsep{\the\glslabeltok}%
            \glsxtrparen{\protect\glsfirstabbrvfont    {\the\glsshorttok}}},%
        firstplural={\protect\glsfirstlongfont{\the\glslongpltok}%
            \protect\glsxtrfullsep{\the\glslabeltok}%
            \glsxtrparen{\protect\glsfirstabbrvfont    {\the\glsshortpltok}}},%
        description={\protect\glsuserdescription{\the\glslongtok}%
            {\the\glslabeltok}}}%
    \renewcommand*{\GlsXtrPostNewAbbreviation}{%
        \glshasattribute{\the\glslabeltok}{regular}%
        {%
            \glssetattribute{\the\glslabeltok}{regular}{false}%
        }%
        {}%
    }%
}{%
    \renewcommand*{\abbrvpluralsuffix}{\glsxtrusersuffix}%
    \renewcommand*{\glsabbrvfont}[1]{\glsabbrvuserfont{##1}}%
    \renewcommand*{\glsfirstabbrvfont}[1]{\glsfirstabbrvuserfont{##1}}%
    \renewcommand*{\glsfirstlongfont}[1]{\glsfirstlonguserfont{##1}}%
    \renewcommand*{\glslongfont}[1]{\glslonguserfont{##1}}%
    \renewcommand*{\glsxtrfullformat}[2]{%
        \ifglshasfield{\glsxtruserfield}{##1}{
            \glsfirstlonguserfont{\glsentryuseri{##1}    \ifglsxtrinsertinside##2\fi}%
            \ifglsxtrinsertinside\else##2\fi
            \glsxtruserpareninverted{\glsabbrevuserfont    {\glsaccessshort{##1}}}{##1}%
        }
        % else
        {
            \glsfirstlonguserfont{\glsaccesslong{##1}    \ifglsxtrinsertinside##2\fi}%
            \ifglsxtrinsertinside\else##2\fi
            \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshort    {##1}}}{##1}%
        }
    }%
    \renewcommand*{\glsxtrfullplformat}[2]{%
        \glsfirstlonguserfont{\glsaccesslongpl{##1}    \ifglsxtrinsertinside##2\fi}%
        \ifglsxtrinsertinside\else##2\fi
        \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshortpl{##1}}}    {##1}%
    }%
    \renewcommand*{\Glsxtrfullformat}[2]{%
        \glsfirstlonguserfont{\Glsaccesslong{##1}\ifglsxtrinsertinside##2\fi}%
            \ifglsxtrinsertinside\else##2\fi
            \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshort    {##1}}}{##1}%
    }%
    \renewcommand*{\Glsxtrfullplformat}[2]{%
        \glsfirstlonguserfont{\Glsaccesslongpl{##1}    \ifglsxtrinsertinside##2\fi}%
        \ifglsxtrinsertinside\else##2\fi
        \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshortpl{##1}}}    {##1}%
    }%
}

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

\GlsXtrLoadResources[
    src={Acronyms},
    sort={pt-BR},
    type={acronym},
]

\begin{document}
    \printunsrtglossary[type=acronym]
    \clearpage

    First time mentioning the \gls{sig:FFT} and the \gls{sig:DFT}.

    Now I'm mentioning \gls{sig:FFT} and \gls{sig:DFT} once again.
\end{document}

but it always formats my entries based on the first key defined in the setup argument, event if the regular attribute is set to false.

I don't have much experience on LaTeX, so I don't know where is my mistake.

Aditionally, the content of the Acronyms.bib file loaded in the code is:

@acronym{sig:DFT,
    short={DFT},
    long={\textit{Discrete Fourier Transform}},
    user1={Transformada Discreta de Fourier}
}

@acronym{sig:FFT,
    short={FFT},
    long={\textit{Fast Fourier Transform}},
    user1={Transformada R\'{a}pida de Fourier}
}
  • 1
    I think it would be simpler to have the Portuguese version in the long field and the English version in the user1 field, and then it's just a matter of modifying the glossary style. That way if you need \glsxtrlong in the text it will use the Portuguese version. – Nicola Talbot May 9 '18 at 21:27
  • @NicolaTalbot by modifying the glossary style you mean the way it is displayed in the list? Because my problem on using the Portuguese text in long is that this version is also displayed in the list, instead of the English text. – Henrique Baron May 10 '18 at 3:51
3

I recommend a different approach that uses a custom entry type and fields. This makes it more flexible so you can use the same .bib file across different documents, and with a different set of aliases you can get different results according to particular document requirements. For comparison, I've also added a Brazilian abbreviation:

@acronym{ABNT,
  short = {ABNT},
  long = {Associa\c{c}\~ao Brasileira de Normas T\'ecnicas}
}

@foreignabbreviation{sig:DFT,
  short = {DFT},
  foreignlong = {Discrete Fourier Transform},
  nativelong = {Transformada Discreta de Fourier}
}

@foreignabbreviation{sig:FFT,
  short = {FFT},
  foreignlong = {Fast Fourier Transform},
  nativelong = {Transformada R\'{a}pida de Fourier}
}

The aim here is to make bib2gls treat the @foreignabbreviation entries as though they had been written as:

@abbreviation{sig:FFT,
  short = {FFT},
  user1 = {Fast Fourier Transform},
  long = {Transformada R\'{a}pida de Fourier},
  category = {foreignabbreviation}
}

This can be achieved with:

  entry-type-aliases={% make @foreignabbreviation act like @abbreviation
    foreignabbreviation=abbreviation
  },
  field-aliases={
    foreignlong=user1,
    nativelong=long
  },
  category={same as original entry}% requires bib2gls v1.4+

The @acronym term will have the category field set to acronym, so the abbreviation style for that can be set with:

\setabbreviationstyle[acronym]{long-short}

The @foreignabbreviation entry will have the category field set to foreignabbreviation, so that can be set with:

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

This allows the two different types of abbreviation to be formatted differently.

The English term is in the user1 field, and the font change for use in the main text can be set with:

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

Now to deal with only the English long form showing in the glossary. The simplest method is to redefine \glsuserdescription. This was only introduced to glossaries-extra in version 1.30, so this method isn't available for older versions:

\renewcommand{\glsuserdescription}[2]{%
 \ifglshasfield{\glsxtruserfield}{#2}{\emph{\glscurrentfieldvalue}}{#1}%
}

The first argument is the long value and the second argument is the label, so this checks if the user1 field has been set and, if it is, uses that instead (so the long-short-user style will also work for the ABNT term in this case).

Here's the complete document:

\documentclass{memoir}

\usepackage{ifluatex}
\ifluatex
\usepackage{fontspec}
\else
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\fi

\usepackage[english]{babel}

\usepackage[record,style=long,nonumberlist,acronyms]{glossaries-extra}

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

\renewcommand{\glsxtruserparen}[2]{%
 \glsxtrfullsep{#2}%
 \glsxtrparen
  {#1\ifglshasfield{\glsxtruserfield}{#2}{, \emph{\glscurrentfieldvalue}}{}}%
}
\renewcommand{\glsuserdescription}[2]{%
 \ifglshasfield{\glsxtruserfield}{#2}{\emph{\glscurrentfieldvalue}}{#1}%
}

\GlsXtrLoadResources[
  src  = {Acronyms},
  sort = {pt-BR},
  type = {acronym},
  entry-type-aliases={% make @foreignabbreviation act like @abbreviation
    foreignabbreviation=abbreviation
  },
  field-aliases={
    foreignlong=user1,
    nativelong=long
  },
  category={same as original entry}% requires bib2gls v1.4+
]

\begin{document}
    \printunsrtglossary[type=acronym]
    \clearpage

    First time mentioning the \gls{sig:FFT} and the \gls{sig:DFT}.
    And \gls{ABNT}.

    Now I'm mentioning \gls{sig:FFT} and \gls{sig:DFT} once again.
    And \gls{ABNT} again.

    Explicit long form: \glsxtrlong{sig:FFT}.
    Explicit full form: \glsxtrfull{sig:FFT}.
\end{document}

This produces:

Acronyms ABNT Associação Brasileira de Normas Técnicas DFT Discrete Fourier Transform FFT Fast Fourier Transform

for the glossary and in the main text:

First time mentioning the Transformada Rápida de Fourier (FFT, Fast Fourier Transform) and the Transformada Discreta de Fourier (DFT, Discrete Fourier Transform). And Associação Brasileira de Normas Técnicas (ABNT). Now I’m mentioning FFT and DFT once again. And ABNT again. Explicit long form: Transformada Rápida de Fourier. Explicit full form: Transformada Rápida de Fourier (FFT, Fast Fourier Transform).

With an older version of glossaries-extra, you can adjust the glossary style to check for the user1 field:

\newglossarystyle{longuser}{%
 \setglossarystyle{long}%
  \renewcommand{\glossentry}[2]{%
    \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
    \ifglshasfield{\glsxtruserfield}{##1}%
    {\emph{\glscurrentfieldvalue}}{\glossentrydesc{##1}}%
    \glspostdescription\space ##2\tabularnewline
  }%
}
\setglossarystyle{longuser}

The newer method is better as it makes it easier to switch glossary styles.

  • This worked much better and simpler than what I was trying to! Thank you a lot! – Henrique Baron May 10 '18 at 22:05

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.