fp和宏的问题

fp和宏的问题

我尝试将执行 FP 计算的宏输入到执行相同操作的其他宏中,但遇到了宏扩展问题。常用的“保护”技巧对我不起作用。有什么提示吗?

下面是一个例子。

\documentclass{article}

\usepackage{fp}
\newcommand{\calculation}[2]{%
\FPeval\X{round(#1 + #2,1)}%
\X%
}

% shall return result of some expression if argument is "XXX"
\makeatletter
\newcommand{\foo}[1]{%
  \ifnum\pdfstrcmp{#1}{XXX}=\z@\FPeval\result{round(1+2+3,1)}\result\fi%      
}
\makeatother

\begin{document}
% writes 6.0
0: \foo{XXX}

% works fine: gives 30.0 
1: \calculation{10}{20}

% now try to insert expression from 0 into calculation: doesn't work
2: \calculation{\foo{XXX}}{20}

% store result of foo in Y: works: gives 6.0
\FPset\Y{\protect\foo{XXX}}
3: \FPprint\Y

% inserting Y into calculation
4: \calculation{\Y}{20}
\end{document}

答案1

我不认为它会起作用,\FPeval因为它不可扩展。但你可以尝试xfp以下方法fp

\documentclass{article}

\usepackage{xfp}
\newcommand{\calculation}[2]{%
\fpeval{round(#1 + #2,1)}%
}

% shall return result of some expression if argument is "XXX"
\makeatletter
\newcommand{\foo}[1]{%
  \ifnum\pdfstrcmp{#1}{XXX}=\z@\fpeval{round(1+2+3,1)}\fi%
}
\makeatother

\begin{document}
% writes 6.0
0: \foo{XXX}

% works fine: gives 30.0
1: \calculation{10}{20}

% now try to insert expression from 0 into calculation: doesn't work
2: \calculation{\foo{XXX}}{20}

\end{document}

相关内容