\tikzmath
這裡兩個函數定義function的語法有問題嗎?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\tikzmath{function f(\x) { return sin(\x);
}
\begin{document}
\begin{tikzpicture}
\tikzmath{function g(\x) { return cos(\x);
};
\end{tikzpicture}
\end{document}
(請溫柔點:我還是新手TikZ
!)
答案1
這是一個小用法範例。請注意,我在和\tikzmath
之間使用,而不是在序言中使用。\begin{document}
\end{document}
關於半柱的放置:手冊指出每個(tikzmath)語句都應該以分號結尾。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
function f(\x) {
return sin(\x);
};
real \f;
\f = f(90);
print \f;
print \newline; % new line; the next line begins with no indent
print f(45); % prints "f(45)" verbatim
% No blank line allowed
print \par; % new paragraph; the next line begins with an indent
\f = f(-45);
print \f;
}
Foo
\begin{tikzpicture}
\tikzmath{
function g(\x) {
return cos(\x);
};
real \g;
\g = g(180);
};
\node at (0,0) {\g}; % use \g outside of \tikzmath, in a tikz node
\end{tikzpicture}
\end{document}
編輯:更多關於什麼是聲明的資訊。
根據 pgf/TikZ手冊中,指令中有不同類型的語句\tikzmath
:
- 賦值(例如
\a = 3 + 4;
或let \x = 2*3;
) - 「類型聲明」(
int \i;
,real \z;
或coordinate \c;
) - 迭代(例如
for \n in {1,...,5} {⟨other statements⟩};
) - 條件句(例如
if ⟨condition⟩ then ⟨statements⟩ else ⟨statements⟩;
) - 函數宣告(eg
function product(\x,\y) {return \x*\y;};
,注意這裡return \x*\y;
也是一個語句,;
第一個程式碼中的第二個是函數宣告語句)
如果語句以大括號開頭{
,則右大括號後面}
必須跟著一個半列。
這裡我以pgf/Ti為例kZ 手冊,附附加註釋(第 58.7 節在解析器外部執行程式碼,目前手冊第 708 頁):
\begin{tikzpicture}
\draw [help lines] grid (3,2); % a common `\draw` TikZ command,
% which ends with a ";"
\tikzmath{ % begin of the \tikzmath command. Not a statemnt
coordinate \c; % a statement
for \x in {0,10,...,360} { % begin of a statement, no ";" here
\c = (1.5cm, 1cm) + (\x:1cm and 0.5cm); % a statement inside
% the for statement
{ \fill (\c) circle [radius=1pt]; }; % a second statement
% inside the for statement.
% In it, a common TikZ command
% (`\draw`) which ends as usual
% by a semi-column.
}; % the end of the for statement. Ended by a semi-column
} % end of \tikzmath command. It's not a statement
\end{tikzpicture}
\draw
請注意,裡面的Tikz指令\tikzmath
是用大括號括起來的,因此在;
for 結尾之後,右大括號後面\draw
還有一個。;