# A dynamic variable as content of a glossary entry

Based on the macro defined here, I would like to use it to create a glossary with a variable in the description. Here is my MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{glossaries}
\makeglossaries
\newcommand\referenceSente{}
\newcommand{\defReferenceSente}[1]
{\gdef\referenceSente{#1}}

% MACRO FOR DEFINITION
\newcommand{\keyDef}[2]{%
%CREATE THE GLOSSARY ENTRY
\newglossaryentry{#1}{name={#1},description={%
#2\\(\referenceSente)
}}
% PRINT THE DEFINITION
\textbf{\gls{#1}} : \textit{#2}\par\vspace{.3cm}
}
\newenvironment{myEnvironment}[1]{\defReferenceSente{#1}}{\par}

\begin{document}

\begin{myEnvironment}{firstReference}
\keyDef{first term}{first definition}
\end{myEnvironment}

\begin{myEnvironment}{secondReference}
\keyDef{second term}{second definition}
\end{myEnvironment}

\printglossaries
\end{document}


With the result :

The command \keyDef{<term>}{<definition>} serves to create a definition which is 1) printed in the document, and 2) added to the glossary, followed with the current value of the variable referenceSente, in parentheses.

The problem is that for the first entry of the glossary, the content inside the parentheses should (firstReference) instead of (secondReference). It seems that when the code is compiled, the glossary uses the last very value of the variable for all the items, instead of the value it had when the definition was declared.

I have no idea how to "ask the glossary" to use the correct value, instead of the last recorded in the document.

• What version of glossaries are you using? If it's recent (v4.0 onwards), you need to adjust the expansion setting when defining the entry. See the section called "Expansion" in the user manual. – Nicola Talbot Jun 26 '14 at 15:04
• Mhmm. I tried, but it didn't seem to work. I don't really understand what the expansion is about, but I tried to add \glssetexpandfield{description} as well as \glssetnoexpandfield{description} just before the creation of my entry (so in the \keyDef macro) but it didn't change anything. – BonyHoax Jun 26 '14 at 15:14

By default the description key (and others like the name key) don't get expanded when the entry is defined. This is done to protect any fragile commands that may be used in the entry. To switch on the expansion you need to use \glssetexpandfield where the argument is the internal field used when storing the value. In most cases this is the same as the name of the key, but there are a few exceptions that are listed in the "Key to Field Mappings" table in the "Expansion" section of the manual. In the case of the description, the field is called desc.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{glossaries}
\makeglossaries

\glssetexpandfield{desc}

\newcommand\referenceSente{}
\newcommand{\defReferenceSente}[1]
{\gdef\referenceSente{#1}}

% MACRO FOR DEFINITION
\newcommand{\keyDef}[2]{%
%CREATE THE GLOSSARY ENTRY
\newglossaryentry{#1}{name={#1},description={%
#2\\(\referenceSente)
}}
% PRINT THE DEFINITION
\textbf{\gls{#1}} : \textit{#2}\par\vspace{.3cm}
}
\newenvironment{myEnvironment}[1]{\defReferenceSente{#1}}{\par}

\begin{document}

\begin{myEnvironment}{firstReference}
\keyDef{first term}{first definition}
\end{myEnvironment}

\begin{myEnvironment}{secondReference}
\keyDef{second term}{second definition}
\end{myEnvironment}

\printglossaries
\end{document}


This produces:

If you do switch on the expansion, you need to remember to protect any fragile commands when you define the glossary entries.

• Thank you, it seems perfect. But I don't really understand this thing about protecting fragile commands. What kind of risk is it? Should I add something to my code to protect it? – BonyHoax Jun 26 '14 at 15:55
• @BonyHoax See Moving Arguments And Fragile Commands or What’s the reason for ‘protection’? for information about fragile commands. – Nicola Talbot Jun 26 '14 at 16:18
• – Nicola Talbot Jun 26 '14 at 16:19
• Ok, I'll check it out. By the way, do you know why I can't put any accentuated character inside the 'name' field? When I do it, I get a huge amount of errors. If it's too complicated, I'll open a new topic... – BonyHoax Jun 26 '14 at 16:23
• @BonyHoax I think it's best to create a new question with a MWE showing the problem. – Nicola Talbot Jun 26 '14 at 16:24