为什么计算值不被识别为数字?

为什么计算值不被识别为数字?

在下面的代码中,我想继续计算结果(来自 Lualatex)。但计算值(四舍五入)未被识别为数字。谁知道这个问题的解决方案?

\documentclass{article}
\usepackage{luacode}

\begin{luacode}
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    if num >= 0 then return math.floor(num * mult + 0.5) / mult
    else return math.ceil(num * mult - 0.5) / mult end
end

sqrt = math.sqrt
function treq (t, s, p, k, w, a) 
    return k *s * sqrt(p / w) + a 
end

function PROCEED (a, b) 
    return a / f 
end
\end{luacode}

\newcommand\round[2][0]{% default value of opt. param.: 0
   \directlua{tex.sprint(round(#2,#1))}}

\begin{document}
\obeylines
Let's try macro 'round':
$ \sqrt{2} \approx \round[1]{2^(1/2)} $ OK.
$ \sqrt[5]{32} = \round{32^(1/5)} $ OK.
$ 1.699999 \approx \round[5]{1.699999} $ OK.
$ -1.699999 \approx \round[1]{-1.699999} $ Also OK.
$ -1.9 \approx \round{-1.9} $ OK.
$ 8 \approx \round[-1]{8} $ OK.\\

Let's try macro 'round' in calculations:
\def\t{3}
\def\s{454}
\def\p{4.51}
\def\k{0.0039413}
\def\w{7.04}
\def\a{1.5}
\def\treq{\round[3]{\luadirect{tex.print(treq(\t,\s,\p,\k,\w,\a))}}} 

$ k = \round[4]{\k} $. OK.
$ t_{req} =\treq $ OK there is an answer (and rounded) \\

Let's try macro 'round' in proceeding calculations:
\def\A{\round[3]{\luadirect{tex.print(PROCEED(\t,2))}}} 
\def\B{\round[3]{\luadirect{tex.print(PROCEED(\treq,2))}}}

$ A =\A $ macro 'PROCEED' works with $t=\t$
$ B =\B $ macro 'PROCEED' is NOT working  with $t_{req}=\treq!\\$

Why is rounded value 'treq' NOT applicable in further calculations?

\end{document}

答案1

您的命令\treq使用了\round不可扩展的命令,因为它有一个可选参数,因此当传递给 Lua 时它不会扩展为数值

使用强制参数:

\documentclass{article}
\usepackage{luacode}

\begin{luacode}
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    if num >= 0 then return math.floor(num * mult + 0.5) / mult
    else return math.ceil(num * mult - 0.5) / mult end
end

sqrt = math.sqrt
function treq (t, s, p, k, w, a) 
    return k *s * sqrt(p / w) + a 
end

function PROCEED (a, b) 
    return a / b 
end
\end{luacode}

\newcommand\round[2]{% default value of opt. param.: 0
   \directlua{tex.sprint(round(#2,#1))}}

\begin{document}
\obeylines
Let's try macro 'round':
$ \sqrt{2} \approx \round{1}{2^(1/2)} $ OK.
$ \sqrt[5]{32} = \round0{32^(1/5)} $ OK.
$ 1.699999 \approx \round{5}{1.699999} $ OK.
$ -1.699999 \approx \round{1}{-1.699999} $ Also OK.
$ -1.9 \approx \round0{-1.9} $ OK.
$ 8 \approx \round{-1}{8} $ OK.\\

Let's try macro 'round' in calculations:
\def\t{3}
\def\s{454}
\def\p{4.51}
\def\k{0.0039413}
\def\w{7.04}
\def\a{1.5}
\def\treq{\round{3}{\luadirect{tex.print(treq(\t,\s,\p,\k,\w,\a))}}} 

$ k = \round{4}{\k} $. OK.
$ t_{req} =\treq $ OK there is an answer (and rounded) 

Let's try macro 'round' in proceeding calculations:
\def\A{\round{3}{\luadirect{tex.print(PROCEED(\t,2))}}} 
\def\B{\round{3}{\luadirect{tex.print(PROCEED(\treq,2))}}}

$ A =\A $ macro 'PROCEED' works with $t=\t$
$ B =\B $ macro 'PROCEED' is NOT working  with $t_{req}=\treq!\\$

Why is rounded value 'treq' NOT applicable in further calculations?

\end{document}

您可以使用可选参数\NewExpandableDocumentCommand来定义可扩展版本\round,但我认为我不会在这里这样做。

相关内容