Search code examples
latexglossaries

How to generate output depending on whether or not a userkey has been defined


I would like to modify the glossaryentryfield so that it creates output depending on whether or not user1 (key provided by the package has been defined/set or not. I tried the following:

\renewcommand*{\glossaryentryfield}[5]{\bfseries{\glstarget{##1}{##2}} & ##3 \\
\ifempty{\glsentryuseri{##1}}
    it & is very empty \\
\else
    & \glsentryuseri{##1} \\%
\fi}

which however results in several errormessages like this:

! Undefined control sequence. \ifempty l.49 \setentrycounter{page}\glsnumberformat{a}}} % The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

(One for every Glossary entry it seems) obviously followed by an unmatched \else and unmatched \fi

As far as i can see, i have to do something with the expansion order, however i am a complete novice to latex. I can not simply set all user1-keys, since i want to only display that extra row if user1 is defined, the other row is just for debugging purposes.

I also tried to add \expandafter directly before the \if ( \expandafter\ifempty{\glsentryuseri{##1}} ) (resulting in the following output:)

user1value it & is very empty \\ & user1value

since nobody could awnser this i crossposted it at the latex community and added a minimal example there. As soon as it gets awnsered on one site i will crosspost the solution


Solution

  • Nicola Talbot awnsered my question on the latex community, she wrote:

    \ifempty isn't defined in standard LaTeX, which is what's causing the error message. You can use the ifmtarg package, which provides the internal command \@ifmtarg. For example:

    \documentclass{article}
    
    \usepackage{ifmtarg}
    \usepackage[style=long]{glossaries}
    
    \makeglossaries
    
    \makeatletter
    \newcommand*{\ifuseriempty}[3]{%
      \protected@edef\tmp{\glsentryuseri{#1}}%
      \expandafter\@ifmtarg\expandafter{\tmp}{#2}{#3}%
    }
    \makeatother
    
    \renewcommand*{\glossaryentryfield}[5]{\bfseries{\glstarget{#1}{#2}}
    & #3 \\
    \ifuseriempty{#1}%
      {it &is very empty}%
      { &\glsentryuseri{#1}}%
    \\%
    }
    
    \newglossaryentry{sample}{name=sample,%
      description={sample without user 1 key}}
    
    \newglossaryentry{sample2}{name=sample2,%
      description={sample with user 1 key},
      user1={User1 Value}}
    
    \begin{document}
    \gls{sample}, \gls{sample2}.
    
    \printglossaries
    \end{document}
    

    Regards Nicola Talbot

    For my purposes the glossaryentryfield has to look like this:

    \renewcommand*{\glossaryentryfield}[5]{\bfseries{\glstarget{#1}{#2}} & #3 \ \ifuseriempty{#1}% {}% { &\glsentryuseri{#1}\}% }

    Remember to use ## instead of # infront of the parameter numbers if you want to use this in a \newglossarystyle enviornment