这里两个\tikzmath
函数定义的语法有什么问题?
\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
between\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/Ti钾Z 手册中,命令中有不同类型的语句\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⟩;
) - 函数声明(例如
function product(\x,\y) {return \x*\y;};
,注意这里return \x*\y;
也是一条语句,;
第一行代码中的第二行是函数声明语句)
如果语句以括号开头{
,则右括号后面}
必须跟一个分号。
这里我以 pgf/Ti 为例钾Z 手册,附加注释(第 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
被括号括起来,因此在;
结束后,在右括号后面\draw
也有一个。;