
我正在使用 TikZ 建立一個多頁 PDF,用於動畫。這樣做時,我收到此錯誤:
! Missing number, treated as zero.
<to be read again>
這是一個顯示問題的最小工作範例(MWE):
\documentclass[tikz]{standalone}
\begin{document}
\def\timesteps{10}
\foreach \timestep in {0,1,...,\timesteps} {
\begin{tikzpicture}
\path[draw] (0, 0) circle (1);
\def\initialx{-1};
\def\initialy{0};
\def\finalx{0.9};
\def\finaly{0};
\def\length{0.1};
\def\xi{{\initialx+(\finalx-\initialx)*(\timestep/\timesteps)}};
\def\yi{{\initialy+(\finaly-\initialy)*(\timestep/\timesteps)}};
\def\xf{{\initialx+(\finalx-\initialx)*(\timestep/\timesteps)+\length}}; % This line works ...
%\def\xf{{\xi+\length}}; % ... but this line does not ...
%\def\xf{{(\xi)+\length}}; % ... unless there are parentheses
\def\yf{{\yi}};
\path[draw, ->] (\xi, \yi) -- (\xf, \yf);
\end{tikzpicture}
}
\end{document}
運行 MWE 會產生所需的輸出:一個簡單的 11 頁 PDF,其中箭頭在螢幕上移動。現在,我註解掉該行
\def\xf{{\initialx+(\finalx-\initialx)*(\timestep/\timesteps)+\length}};
並取消註解該行
\def\xf{{\xi+\length}};
因為它們在數學上是等價的。我收到前面提到的錯誤:
! Missing number, treated as zero.
<to be read again>
我在谷歌上搜尋了與 TikZ 相關的內容,所有答案都說計算時需要大括號。然而,上面的那行有花括號,所以我不認為這是問題。在嘗試了不同的事情之後,我注意到括號\xi
解決了這個問題:
\def\xf{{(\xi)+\length}};
因此,我的問題是為什麼這裡需要括號?正如我之前提到的,我認為如果計算在花括號內,則不需要其他特殊的東西。有沒有任何「官方」文件討論這個問題?最後,我是 TikZ 的新手,所以請隨意改進我的程式碼。