[\directlua]:1: 预计附近有 ')'

[\directlua]:1: 预计附近有 ')'

在大多数情况下,我会收到此错误:[\directlua]:1: ')' expected near 。

我想计算 \Abs{((\bx-\ax)*(\cx-\ax)+(\by-\ay)*(\cy-\ay))/(809.55841)}然后四舍五入

\documentclass[margin=1cm]{standalone} 
\usepackage{tkz-euclide}

\makeatletter
\def\Abs#1{\directlua{tex.sprint(math.abs(#1))}}
\def\pDec#1{\directlua{tex.print(string.format('\@percentchar.12f',#1))}}
\def\Round#1#2{\directlua{tex.sprint(tostring(round(#1,#2)))}}

\def\DotProd(#1,#2,#3){%
\begingroup
\pgfextractx{\pgf@x}{\pgfpointanchor{#1}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#1}{center}}% 
\def\ax{\strip@pt\pgf@x}%
\def\ay{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#2}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#2}{center}}% 
\def\bx{\strip@pt\pgf@x}%
\def\by{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#3}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#3}{center}}% 
\def\cx{\strip@pt\pgf@x}%
\def\cy{\strip@pt\pgf@y}%
% the following line gives the error: 
% [\directlua]:1: ')' expected near <eof>.
% \Abs #1->\directlua {tex.sprint(math.abs(#1))}
% \edef\tmp{\Abs{((\bx-\ax)*(\cx-\ax)+(\by-\ay)*(\cy-\ay))/(809.55841)}}

% the following line gives a result
\edef\tmpa{\directlua{tex.sprint(tostring(((\bx-\ax)*(\cx-\ax))/809.55841))}}

% the next line gives the same error
%\edef\tmpb{\directlua{tex.sprint(tostring(((\by-\ay)*(\cy-\ay))/809.55841))}}
 
% \edef\dotprod{\Round{\tmp}{5}}%
% \global\let\MathResult\dotprod
\endgroup
}
\makeatother

\begin{document}   

\begin{tikzpicture}
  \tkzDefPoints{1/-2/A,5/0/B}
  \tkzDefCircle[diameter](A,B) \tkzGetPoint{O}
  \tkzDrawCircle(O,A)
  \tkzDrawPoints(A,B,O)
  \tkzLabelPoints(A,B,O)
  \DotProd(O,A,B)
\end{tikzpicture}
 
\end{document}

答案1

... 在这种情况下,问题在于间接层混淆了真正的问题。

我建议执行以下操作:

\documentclass[margin=1cm]{standalone} 
\usepackage{tkz-euclide}



% ======== add this part
\ExplSyntaxOn
\def\directluadebug#1{
  \expandafter\directluadebugii\expandafter{\detokenize\expandafter{\expanded{#1}}}
}
\def\directluadebugii#1{
  \directlua{print() print() print("Going~to~execute:") print(token.scan_string()) print()}{#1}
  \directlua{#1}
}
\ExplSyntaxOff
% ======== end add this part


\makeatletter
\def\Abs#1{\directluadebug{tex.sprint(math.abs(#1))}}
\def\pDec#1{\directluadebug{tex.print(string.format('\@percentchar.12f',#1))}}
\def\Round#1#2{\directluadebug{tex.sprint(tostring(math.round(#1,#2)))}}

\def\DotProd(#1,#2,#3){%
\begingroup
\pgfextractx{\pgf@x}{\pgfpointanchor{#1}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#1}{center}}% 
\def\ax{\strip@pt\pgf@x}%
\def\ay{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#2}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#2}{center}}% 
\def\bx{\strip@pt\pgf@x}%
\def\by{\strip@pt\pgf@y}%
\pgfextractx{\pgf@x}{\pgfpointanchor{#3}{center}}%
\pgfextracty{\pgf@y}{\pgfpointanchor{#3}{center}}% 
\def\cx{\strip@pt\pgf@x}%
\def\cy{\strip@pt\pgf@y}%

\edef\tmp{\Abs{((\bx-\ax)*(\cx-\ax)+(\by-\ay)*(\cy-\ay))/(809.55841)}}

\edef\tmpa{\directluadebug{tex.sprint(tostring(((\bx-\ax)*(\cx-\ax))/809.55841))}}

\edef\tmpb{\directluadebug{tex.sprint(tostring(((\by-\ay)*(\cy-\ay))/809.55841))}}
 
\edef\dotprod{\Round{\tmp}{5}}%
\global\let\MathResult\dotprod
\endgroup
}
\makeatother

\begin{document}   

\begin{tikzpicture}
  \tkzDefPoints{-1/-2/A,-5/-7/B}
  \tkzDefCircle[diameter](A,B) \tkzGetPoint{O}
  \tkzDrawCircle(O,A)
  \tkzDrawPoints(A,B,O)
  \tkzLabelPoints(A,B,O)
  \DotProd(O,A,B)
\end{tikzpicture}
 
\end{document}

如果您添加在“添加此部分”和“结束添加此部分”之间标记的部分,然后将每个替换\directlua\directluadebug,那么您将能够查看正在执行的代码。

(存在一些细微的差别,其中\directlua只需一步即可扩展为“结果”,而 则\directluadebug需要多个扩展步骤 - 此问题可以使用 修复\luadef,但在这种情况下为了简单起见我省略了它)

反正终端上打印的内容是……

Going to execute:
tex.sprint(math.abs(((-142.26372--142.26372)*(-142.26372--142.26372)+(-199.1692--199.1692)*(-199.1692--199.1692))/(809.55841)))

一个简单的 Lua 语法高亮器就可以轻松指出错误的位置。

相关内容