# Parse numerical value from glossaries variable

Can I parse the text returned from \gls*{...} and get latex to recognize it as a numerical variable so that I can use it in a calculation? This calculation could be done using calculator, fp, calc or pgf depending what I run into concerning package conflicts. I am compiling in XeLaTeX and TeX4HT.


In the MWE below the different approaches I have tried are commented out to permit compilation to show the success of the glossaries definition and calculation functions.

\documentclass{article}
\usepackage[utf8]{inputenc}
\setlength\parindent{0pt}
\usepackage[nogroupskip,toc,acronym]{glossaries}
\usepackage{scrwfile}
\usepackage{calculator}
\usepackage{fp}
\usepackage{calc}
\usepackage{pgf}

\usepackage{siunitx}

\newglossary[datag]{data}{datat}{datan}{Data}

\newglossaryentry{d.A_Control-0}{ type={data},  name={1.1}, first={1.1}, description={1.1} }

\newglossaryentry{d.B_Sample-2}{ type={data},  name={1.08059784043081}, first={1.08059784043081}, description={1.08059784043081} }

\newcommand{\foldfp}[3]{
\FPeval{result}{round(#1/#2,#3)}
\result
}

\newcommand{\foldcalculator}[3]{
\DIVIDE{#1}{#2}{\dsol}
\dsol
%\DIVIDE{\gls*{#1}}{\gls*{#2}}{\dsol}
%\DIVIDE{{#1}}{{#2}}{\dsol}
%\ROUND[#3]{\dsol}{\rsol}
%\rsol
}

\begin{document}

\begin{itemize}
\item \gls*{d.A_Control-0}
\item \gls*{d.B_Sample-2}
\item \glsentryfirst{d.A_Control-0}
\item \foldfp{33}{2}{2} - proof that the calculation function works fine.
\item \foldcalculator{33}{2}{2} - proof that the calculation function works fine.
%\item \num{\gls*{d.A_Control-0}}
%\item \number{\gls*{d.A_Control-0}}
%\item \foldfp{\gls*{d.A_Control-0}}{\gls*{d.B_Sample-2}}{2}
%\item \foldcalculator{\gls*{d.A_Control-0}}{\gls*{d.B_Sample-2}}{2}
\end{itemize}

\end{document}

• \gls* is protected, therefore not expandable, unfortunately. Nice question, anyway – user31729 Feb 23 '15 at 18:17
• @ChristianHupfer Thanks for your comment. Are \glsentryfirst or any of the other \glsentryxxx fields also protected? Might I be able to use one of these macros to define a custom macro that would parse this constant value? I think I understand the purpose behind protecting gls calls because they are dynamic, however the glsentry equivalents are constants... Is there any potential to adapt the question with these calls as shown in \item three of the original question? – EngBIRD Feb 23 '15 at 18:28
• Quick check for your first question (in comment): \glsentryfirst isn't protected, but I am not really familiar with glossaries, so I don't know the various \glsentryxxx commands. Try yourself (in a terminal) texdef -t latex -p glossaries glsentryxxx (replace glsentryxxx with the real macro name) – user31729 Feb 23 '15 at 18:33
• The expandable commands are the non-case-changing \glsentryxxx commands listed in Using Glossary Terms Without Links. (It's also possible to use \glsletentryfield to assign to contents of a particular field to a command, which you could then use in an expandable context.) – Nicola Talbot Feb 23 '15 at 20:57
• @NicolaTalbot Much thanks for your comment, not to mention the glossaries package itself. I have solved my question using \glsentryname{#n} instead of \gls*{#n} inside the newcommand performing the ratio. Could have sworn I had tried that in the form of \glsentryfirst{#n}. I am having some trouble visualizing how \glsletentryfield would improve this because from what I understand, I still have to assign a custom function name. In macro form this would be overwritten with each call, but how is that different than using the call to the field using \glsentryxxx? – EngBIRD Feb 24 '15 at 2:49

Thanks to all who commented, here is my summary of the problem evolution and solution:

1. I was mistaken in interpreting the unexpanded \gls*{} entries as a numerical parse problem. Thanks to @ChristianHupfer
2. I can use \glsentryname{} to grab the expanded definition. Thanks to @NicolaTalbot
3. Now that I am using an expanded variable correctly, there isn't actually a problem recognizing the defined term (a number in excel - rather than use LaTeX to parse and evaluate my content, I use a formula in excel to control the digits/rounding of the raw data stored as gls entry outputs). Thanks to @cfr.
4. Lastly I couldn't continue to use the calculator package because it uses a modified division algorithm that does not return sufficiently accurate results. Instead I used the following code.

fold command:

\newcommand{\folds}[2]{%
\ifboolexpr{test {\ifdimgreater{\glsentryname{#1} pt}{\glsentryname{#2} pt}}}%
{%
\FPeval{result}{round(\glsentryname{#1}/\glsentryname{#2},\rfndp)}%
\num{\result}%
}% true
{%
\FPeval{result}{round(\glsentryname{#2}/\glsentryname{#1},\rfndp)}%
\num{\result}%
}% false
}