[Tex/LaTex] Using the glossaries package for English acronyms in German documents

glossaries

This is a repost of the original message I posted to comp.text.tex in August, which is still unanswered there. I'm sorry for reposting, but I want to spread this to a wider readership.

I found couple of similar questions (along with helpful answers) on StackExchange, however, they are more focused on how to deal with the variety of cases the German language has. This is not my actual problem, since the suggested workarounds for this issue are sufficient. They mostly incorporate the use of \glslink and \glsdisp.

Below is the text of my original post.

I would like to use the glossaries package for German documents
including many English based acronyms. For better understanding, let's
have a look at a simple example:

  • Acronym: PSK
  • English long: Phase Shift Keying
  • German long: Phasenumtastung

Im most cases, there is not corresponding acronym in German, therefore
I would like to use the English acronym in my document. Upon first
use, the inserted text shall look like

Phasenumtastung (PSK, Phase Shift Keying)

Upon further use, only

PSK

shall be used.

Finally, in the list of acronyms, it shall look like:

PSK – Phasenumtastung (Phase shift Keying)

I tried to to setup something customized by renewing
\CustomAcronymFields, \SetCustomDisplayStyle and \glossaryentryfield following the user documentation. I'm using the current version 3.01 of glossaries.

My approach is basically to put the string ", Phase Shift Keying" into
the description field while defining with \newacronym and then use it
with customized \defglsdisplayfirst and \glossaryentryfield.

This is to demonstrate how it should look like:

% compile this with
%    pdflatex example.tex
%    makeindex  -s "example.ist" -t "example.alg" -o "example.acr" "example.acn"
%    pdflatex example.tex

\documentclass{scrartcl}

\usepackage[ngerman]{babel}

\usepackage[acronym,nomain,sanitize={description=false,symbol=false}]{glossaries}
\makeglossaries

%% Desired style:
% first: "German long (acronym[, English long])"
% following: "acronym"
\renewcommand*{\CustomAcronymFields}{%
    name={\the\glsshorttok},%
    symbol={\the\glslongtok},%
    text={\the\glsshorttok},%
    plural={\the\glsshorttok\noexpand\acrpluralsuffix}%
}

\renewcommand*{\SetCustomDisplayStyle}[1]{%
  % ##1 corresponds to the 'first' key
  % ##2 corresponds to the 'description' key
  % ##3 corresponds to the 'symbol' key
  % ##4 is the inserted text given by the final optional argument to
  % commands like \gls
  % The short form can be obtained via \glsentryshort{\glslabel}
  % The plural short form can be obtained via
  %\glsentryshortpl{\glslabel}
  % The long form can be obtained via \glsentrylong{\glslabel}
  % The plural long form can be obtained via
  %\glsentrylongpl{\glslabel}
  \defglsdisplayfirst[#1]{##3 (##1##4##2##4)}%
  % ##1 corresponds to the 'text' key
  % the rest as above
  \defglsdisplay[#1]{##1##4}%
}
\SetCustomStyle

% start the glossary entries with capitals
\newglossarystyle{superuc}{%
  \glossarystyle{super}%
  \renewcommand*\glossaryentryfield[5]{%
    \glstarget{##1}{##2} & \makefirstuc{##4}##3\glspostdescription\space##5\\
  }
}



%% acronym definitions
% italic typeset foreign language terms
\newcommand{\foreignlang}[1]{\emph{#1}}
% define acronym with both German and English long
\newcommand{\newacropair}[4]{\newacronym[description={, \foreignlang{#4}}]{#1}{#2}{#3}}
% define acronym with only German long
\newcommand{\newacrogerman}[3]{\newacronym[description={}]{#1}{#2}{#3}}
% define acronym with only English long
\newcommand{\newacroenglish}[3]{\newacronym[description={}]{#1}{#2}{\foreignlang{#3}}}

% sample acronyms
% English acronym along with its common German counterpart
\newacropair{acr:adc}{ADC}{Analog-Digital-Umsetzer}{Analog to Digital Converter}
% English acronym without a common German counterpart
\newacroenglish{acr:dect}{DECT}{Digital Enhanced Cordless Telecommunications}
% German acronym
\newacrogerman{acr:din}{DIN}{Deutsches Institut fuer Normung}


% Alternative first - to be used if the first use is not nominative singular.
% Problem: I do not find a way to use glossaries' integrated 'first use' flag.
% This macro is my current dirty implementation. It always displays the full form
% and unsets the 'first use' flag afterwards. Bad things happen if at a later point
% it time, I decide to mention the same acronym earlier. I'll be using \glsalt again
% and end up in a double definition of the acronym, which I would like to avoid.
\newcommand{\glsalt}[2]{\glslink{#1}{#2 (\acrshort*{#1}\glsdesc*{#1})}\glsunset{#1}}


\begin{document}

\section{Problems}

% For the first mention, I'll be using \glsalt, since I need to display the 
% German text in genitive.
Die Aufloesung des \glsalt{acr:adc}{Analog-Digital-Umsetzers} beeinflusst\dots

% In the example below, I would need a plural form. That means an 's' would
% have to be attached to both the English acronym as well as the English long form.
% I have no clue how to implement that cleanly, due to my changes in
% \SetCustomDisplayStyle in the preamble.
Die Aufloesung der \glsalt{acr:adc}{Analog-Digital-Umsetzer} beeinflusst\dots


\section{Normal Cases}

\glsreset{acr:adc}

% These are only to show how it should look like for the 'normal' cases.
\gls{acr:adc} -- \gls{acr:adc}

\gls{acr:dect} -- \gls{acr:dect}

\gls{acr:din} -- \gls{acr:din}

\printglossary[type=\acronymtype,style=superuc,nonumberlist=false]

\end{document}

This works for me unless I don't have to use the plural form upon the
first occurence. From then on, the concept behind glossaries, so far I
can understand, does not really allow my tweak.

The main reason, to my understanding, is the fact that when defining a
new acronym by \newacronym, besides the label, only one short form and
one long form can be passed and accessed in \CustomAcronymFields.
These two can be accessed by \glsshorttok and \glslongtok for storing
them under the user-defined keys, such as name, symbol, text,
etc. Especially the description key itself cannot be accessed at
this stage.

From the conceptual point of view, it would be helpful if all three
forms (short, English long, German long) could be passed to
\newacronym separately and then concatenated in a customized fashion
to be finally stored with their corresponding keys, including
firstplural and longplural. Note that for my example, I would e.g.
have to append an "s" for the plural forms of the short form as well
as to the English long form and provide a used-defined plural form for
the German long form in most cases.

For now, my only question is whether you think that my problem can
still be solved with what glossaries offers so far. If not, would it
make sense to try to redefine a couple of commands? I've had the
impression that e.g. \glsentryfull has to be redefined. However, one
major problem I fear is that most of these commands only take a subset
of keys, such as only short and long (not description) in case
of \glsentryfull.

Or is there even another package which would do my task without modifications?

Best Answer

The simplest method is to use the glossaries-extra package. The translation is stored in the user1 key. The long-short-user abbreviation style is designed to include additional information in the parenthetical material on first use. By default it picks up the information from the user1 field.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[colorlinks]{hyperref}
\usepackage[acronym,
            nomain,% don't create "main" glossary
            postdot% put a period after the description
           ]{glossaries-extra}

\makeglossaries

% \glscurrentfieldvalue only works with glossaries v4.23 (and above)
\renewcommand{\glsxtrpostdescacronym}{%
 \ifglshasfield{\glsxtruserfield}{\glscurrententrylabel}%
 { (\glscurrentfieldvalue)}%
 {}%
}

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

\newacronym[user1=Phase Shift Keying]{psk}{PSK}{Phasenumtastung}
\newacronym{aa}{AA}{Another Acronym}

\begin{document}    

First use: \gls{psk}. Subsequent use: \gls{psk}.

First use: \gls{aa}. Subsequent use: \gls{aa}.

\printglossaries

\end{document}

First use: Phasenumtastung (PSK, Phase Shift Keying). Subsequent use: PSK. First use: Another Acronym (AA). Subsequent use: AA. Acronyms AA Another Acronym. 1 PSK Phasenumtastung (Phase Shift Keying). 1

The post-description hook (\glsxtrpostdesccategory) appends information after the description in the glossary. This is a convenient way of making a minor adjustment without having to define a new glossary style.

If you prefer to use \newabbreviation instead of \newacronym, you need to redefine \glsxtrpostdescabbreviation instead of \glsxtrpostdescacronym and use \setabbreviationstyle{long-short-user} instead of \setabbreviationstyle[acronym]{long-short-user}. (\newacronym implicitly sets category=acronym and \newabbreviation implicitly sets category=abbreviation.)

If you want to stick with just the base glossaries package, it's slightly more complicated. The translation is still provided in the user1 field:

\documentclass{article}

\usepackage{glossaries}

\makeglossaries

% define an acronym style for the acronyms with a translation:

\newacronymstyle{acronymslang}%
{%
  \GlsUseAcrEntryDispStyle{long-short}%
}
{%
  \GlsUseAcrStyleDefs{short-long}%
  \renewcommand*{\genacrfullformat}[2]{%
   \glsentrylong{##1}##2\space
   (\protect\firstacronymfont{\glsentryshort{##1}}%
    \ifglshasfield{useri}{##1}%
    {, \glsentryuseri{##1}}{})%
  }%
  \renewcommand*{\Genacrfullformat}[2]{%
   \Glsentrylong{##1}##2\space
   (\protect\firstacronymfont{\glsentryshort{##1}}%
    \ifglshasfield{useri}{##1}%
    {, \glsentryuseri{##1}}{})%
  }%
  \renewcommand*{\genplacrfullformat}[2]{%
   \glsentrylongpl{##1}##2\space
   (\protect\firstacronymfont{\glsentryshortpl{##1}}%
    \ifglshasfield{useri}{##1}%
    {, \glsentryuseri{##1}}{})%
  }%
  \renewcommand*{\Genplacrfullformat}[2]{%
   \Glsentrylongpl{##1}##2\space
   (\protect\firstacronymfont{\glsentryshortpl{##1}}%
    \ifglshasfield{useri}{##1}%
    {, \glsentryuseri{##1}}{})%
  }%
}

\setacronymstyle{acronymslang}

\newacronym[user1=Phase Shift Keying]{psk}{PSK}{Phasenumtastung}
\newacronym{aa}{AA}{Another Acronym}

% glossary style for acronyms with a translation

\newglossarystyle{acronymslang}
{%
  \setglossarystyle{long}%
  \renewcommand*{\glsgroupskip}{}%
  \renewcommand{\glossentry}[2]{%
    \glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
    \glossentrydesc{##1}\glspostdescription
    \ifglshasfield{useri}{##1}{ (\glsentryuseri{##1})}{}%
    \space ##2\tabularnewline
  }%
}

\setglossarystyle{acronymslang}

\begin{document}

First use: \gls{psk}. Subsequent use: \gls{psk}.

First use: \gls{aa}. Subsequent use: \gls{aa}.

\printglossaries
\end{document}

image of document

This doesn't have the benefit of the post-description hook so a custom glossary style is required. The difference between the two methods can be seen from the position of the period (full-stop) inserted after the description through the postdot or postpunc=dot (glossaries-extra only) or nopostdot=false package options. With the glossaries-extra post-description hook the terminating punctuation is placed after the hook.

My original answer used an old version of glossaries and had to use internal commands, which isn't desirable. It's better to update to a newer version and use one of the above methods instead. There's no custom glossary style as it uses a hack to append the user1 contents to the description field.

\documentclass{article}

\usepackage{glossaries}
\usepackage{etoolbox}

\newtoks\customtok

\renewcommand*{\newacronymhook}{%
 \edef\dosetkeys{\noexpand\setkeys{glossentry}{user1={},\the\glskeylisttok}}%
 \dosetkeys
 \ifcsempty{@glo@useri}%
 {%
   \expandafter\customtok\expandafter{\the\glsshorttok}%
 }%
 {%
   \edef\custom{\the\glsshorttok, \csexpandonce{@glo@useri}}%
   \expandafter\customtok\expandafter{\custom}%
 }%
}

\newcommand*{\custompostdesc}[1]{%
  \ifcsempty{glo@#1@useri}{}{ (\glsentryuseri{#1})}%
}

\renewcommand*{\CustomAcronymFields}{%
  user1={},%
  name={\the\glsshorttok},%
  description={\the\glslongtok\noexpand\custompostdesc{\the\glslabeltok}},%
  first={\the\glslongtok\space(\the\customtok)},%
  firstplural={\the\glslongtok\noexpand\acrpluralsuffix
    \space (\the\customtok)}%
  text={\the\glsshorttok},%
  plural={\the\glsshorttok\noexpand\acrpluralsuffix}%
}

\SetCustomStyle

\makeglossaries

\newacronym[user1=Phase Shift Keying]{psk}{PSK}{Phasenumtastung}
\newacronym{aa}{AA}{Another Acronym}

\begin{document}

First use: \gls{psk}. Subsequent use: \gls{psk}.

First use: \gls{aa}. Subsequent use: \gls{aa}.

\printglossaries
\end{document}

image of document