2

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 :

MWE

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.

2
  • 1
    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. Jun 26, 2014 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, 2014 at 15:14

1 Answer 1

2

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:

Image of result

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

6

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .