如果我用
\FPeval{\one}{\csname test1\endcsname}
一切都很好。如果我使用
\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname}
在 \ifthenelse 中,我收到错误。
\documentclass[10pt,german]{article}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgffor} %foreach
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp -> FPeval
\usepackage{fp}
\usepackage{ifthen,changepage}
\expandafter\xdef\csname test1\endcsname{5}
\expandafter\xdef\csname test2\endcsname{4}
\begin{document}
%\FPeval{\one}{\csname test1\endcsname} <- this works
\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname} % <- this doesnt work
\FPeval{\two}{\csname test2\endcsname}
\ifthenelse{\one > \two}{case 1 }{case 2}
\end{document}
有人有想法吗?
答案1
\ithenelse 比较整数,因此不适合进行此类测试。请使用 fp 包本身提供的测试:
\documentclass[10pt,german]{article}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgffor} %foreach
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp -> FPeval
\expandafter\xdef\csname test1\endcsname{5}
\expandafter\xdef\csname test2\endcsname{4}
\begin{document}
%\FPeval{\one}{\csname test1\endcsname} <- this works
\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname} % <- this doesnt work
\FPeval{\two}{\csname test2\endcsname}
\FPifgt \one \two case 1 \else case 2 \fi
\end{document}
答案2
一旦你应用\FPeval
,你就会得到类似的东西9.000000000000000000
,在 TeX 看来,这不是一个整数,因此比较无效。
您可以使用\lengthtest
:
\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp -> FPeval
\usepackage{ifthen}
\expandafter\def\csname test1\endcsname{5}
\expandafter\def\csname test2\endcsname{4}
\begin{document}
\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname}
\FPeval{\two}{\csname test2\endcsname}
\ifthenelse{\lengthtest{\one pt > \two pt}}{case 1}{case 2}
\end{document}
但我认为,使用具有expl3
可扩展比较功能的功能更为有效。
\documentclass{article}
\usepackage{xparse}
% see https://tex.stackexchange.com/a/467527/4427
\ExplSyntaxOn
\NewExpandableDocumentCommand{\xifthenelse}{mmm}
{
\bool_if:nTF { #1 } { #2 } { #3 }
}
\cs_new_eq:NN \numtest \int_compare_p:n
\cs_new_eq:NN \oddtest \int_if_odd_p:n
\cs_new_eq:NN \fptest \fp_compare_p:n
\cs_new_eq:NN \dimtest \dim_compare_p:n
\cs_new_eq:NN \deftest \cs_if_exist_p:N
\cs_new_eq:NN \namedeftest \cs_if_exist_p:c
\cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN
\cs_new_eq:NN \streqtest \str_if_eq_p:ee
\cs_new_eq:NN \emptytest \tl_if_blank_p:n
\prg_new_conditional:Nnn \xxifthen_legacy_conditional:n { p,T,F,TF }
{
\use:c { if#1 } \prg_return_true: \else: \prg_return_false: \fi:
}
\cs_new_eq:NN \boolean \xxifthen_legacy_conditional_p:n
% some syntactic sugar for variables
\NewDocumentCommand{\setvar}{mm}
{% #1 = name, #2 = value
\fp_zero_new:c { l_bilal_var_#1_fp }
\fp_set:cn { l_bilal_var_#1_fp } { #2 }
}
\NewExpandableDocumentCommand{\usevar}{m}
{
\fp_use:c { l_bilal_var_#1_fp }
}
\ExplSyntaxOff
\begin{document}
\setvar{test1}{5}
\setvar{test2}{4}
\setvar{one}{\usevar{test1}+\usevar{test2}}
\xifthenelse{\fptest{\usevar{one} > \usevar{test2}}}{case 1}{case 2}
\end{document}
编辑 2021
更完整的实施方案还考虑到expl3
现在已经有\legacy_if_p:n
。
\ExplSyntaxOn
\NewExpandableDocumentCommand{\xifthenelse}{mmm}
{
\bool_if:nTF { #1 } { #2 } { #3 }
}
\NewDocumentCommand{\newboolean}{m}
{
\cs_if_exist:cTF {if#1}
{
\msg_error:nnn { xifthen } { booleanexists } { #1 }
}
{
\exp_args:Nc \newif {if#1}
}
}
\NewDocumentCommand{\provideboolean}{m}
{
\cs_if_exist:cF {if#1}
{
\exp_args:Nc \newif {if#1}
}
}
\NewDocumentCommand{\setboolean}{mm}
{
\cs_if_exist:cTF {if#1}
{
\use:c { legacy_if_set_#2:n } { #1 }
}
{
\msg_error:nnn { xifthen } { wrongboolean } { #1 }
}
}
\msg_new:nnnn { xifthen } { booleanexists }
{
Boolean~\token_to_str:N \if #1~already~exists
}
{
Your~command~was~ignored.\\
Type~<return>~to~continue~without~it.
}
\msg_new:nnnn { xifthen } { wrongboolean }
{
Boolean~\token_to_str:N \if #1~does~not~exist
}
{
Try~typing~<return>~to~proceed.\\
If~that~doesn't~work,~type~X<return>~to~quit.
}
\cs_new_eq:NN \numtest \int_compare_p:n
\cs_new_eq:NN \oddtest \int_if_odd_p:n
\cs_new_eq:NN \fptest \fp_compare_p:n
\cs_new_eq:NN \dimtest \dim_compare_p:n
\cs_new_eq:NN \deftest \cs_if_exist_p:N
\cs_new_eq:NN \namedeftest \cs_if_exist_p:c
\cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN
\cs_new_eq:NN \streqtest \str_if_eq_p:ee
\cs_new_eq:NN \emptytest \tl_if_blank_p:n
\cs_new_eq:NN \boolean \legacy_if_p:n
\ExplSyntaxOff