如何检索和使用“fp”包中的词汇表条目值进行计算?

如何检索和使用“fp”包中的词汇表条目值进行计算?

590.98我打算利用存储在词汇表条目中的数值PClabel进行后续计算。我的方法是使用检索值\glsuseri{PClabel}并将其合并到fp名为的变量中\PricePC\FPset\PricePC{ \glsuseri{PClabel} }。不幸的是,这种方法似乎不起作用。有人有其他解决方案或关于如何实现这一点的建议吗?请参考下面的代码片段:

代码

\documentclass{article}

%% Include packages:
\usepackage{glossaries}
\usepackage{fp}

%% Define glossary entry:
\newglossaryentry{PClabel}{                     % label
    name={Personal Computer},                   % name
    description={designed for individual use},  % description
    user1={590.98},                             % price
    user2={CHF},                                % currency 
}

\begin{document}
    
    The price of the \glsentryname{PClabel}
    is \glsuseri{PClabel} \glsuserii{PClabel}.
    
    %% Using the price for further calculations using the 'fp' package
    %% How can I use \glsuseri{PClabel} as an argument for \FPset\PricePC{...}?
    
    \FPset\PricePC{590.98}  % replacing 590.98 by \glsuseri{PClabel} does not work                  
    
    The price of the \glsentryname{PClabel} 
    stored \texttt{PricePC}
    is \PricePC\ \glsuserii{PClabel}.
        
\end{document}

结果

在此处输入图片描述

答案1

您可以定义命令的可扩展版本。您需要确保字段的内容具有正确的类型(并且存在)。

\documentclass{article}

%% Include packages:
\usepackage{glossaries}
\usepackage{fp}

%% Define glossary entry:
\newglossaryentry{PClabel}{                     % label
    name={Personal Computer},                   % name
    description={designed for individual use},  % description
    user1={590.98},                             % price
    user2={CHF},                                % currency
}

\newcommand{\expandgls}[2]{\UseName{glo@\detokenize{#1}@#2}}

\begin{document}

The price of the \glsentryname{PClabel}
is \glsuseri{PClabel} \glsuserii{PClabel}.

%% Using the price for further calculations using the 'fp' package
%% How can I use \glsuseri{PClabel} as an argument for \FPset\PricePC{...}?

\FPset\PricePC{\expandgls{PClabel}{useri}}

The price of the \glsentryname{PClabel}
stored \texttt{PricePC}
is \PricePC\ \glsuserii{PClabel}.

\end{document}

在此处输入图片描述

您也可以避免fp

\documentclass{article}

%% Include packages:
\usepackage{glossaries}

%% Define glossary entry:
\newglossaryentry{PClabel}{                     % label
    name={Personal Computer},                   % name
    description={designed for individual use},  % description
    user1={590.98},                             % price
    user2={CHF},                                % currency
}

\newcommand{\expandgls}[2]{\UseName{glo@\detokenize{#1}@#2}}

\begin{document}

The price of the \glsentryname{PClabel}
is \glsuseri{PClabel} \glsuserii{PClabel}.

The 20\% discounted price of the \glsentryname{PClabel} 
is \fpeval{round(\expandgls{PClabel}{useri}*0.80,2)} \glsuserii{PClabel}.

\end{document}

在此处输入图片描述

foo说明:条目字段包含的值baz存储在宏中\glo@baz@foo;条目名称被去标记化,以防其中包含“危险”字符。

相关内容