如何在 PGFPlots 坐标内使用数学运算符?
当我写类似的内容时\draw (axis cs: 0,0) -- (axis cs: sqrt(3),1)
,出现以下错误:
¡ \pgfplots@evalute@tikz@coord@system@interface@ 的参数有一个额外的}。
但是,\draw (0,0) -> ({sqrt(3)},1)
只要sqrt(3)
将其写在括号内,在 Tikz 中就可以正常工作{}
。
如果我将括号添加到 PGFPlots 代码中,会出现以下错误:
¡ 软件包 PGF 数学错误:无法将输入 '-{sqrt(3)}/2' 解析为浮点数,抱歉。无法读取的部分位于 'sqrt(3)/2' 附近。
梅威瑟:
\documentclass{memoir}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.8,
standard/.style={
axis x line=center,
axis y line=center,
axis z line=center,
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
axis line style=help lines,
every axis/.append style={font=\tiny},
}
}
\begin{document}
{\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[
standard,
hide z axis,
xmin=-1.25, xmax=1.25,
ymin=-1.25, ymax=1.25,
zmin=0, zmax=2.5,
xtick={-1,1},
xticklabels={$-2r$,$2r$},
ytick=\empty,
]
% Draw Circle
\draw[blue] (axis cs: 0,0,0)
ellipse [
x radius=1, y radius=1];
% Draw Square
\draw[green] (axis cs: -0.86602540378,0.5,0) --
(axis cs: -0.86602540378,-0.5,0) --
(axis cs: -0.86602540378,-0.5,1) --
(axis cs: -0.86602540378,0.5,1) --
(axis cs: -0.86602540378,0.5,0);
% I can't use math operators within PGFPlots apparently?
% \draw[green] (axis cs: -sqrt(3)/2,1/2,0) --
% (axis cs: -sqrt(3)/2,-1/2,0) --
% (axis cs: -sqrt(3)/2,-1/2,1) --
% (axis cs: -sqrt(3)/2,1/2,1) --
% (axis cs: -sqrt(3)/2,1/2,0);
\end{axis}
% However, I am able to do so in regular Tikz, as long as I put functions like sqrt() in braces...
\draw[green] (-{sqrt(3)/2},1/2,0) ->
(-{sqrt(3)/2},-1/2,0) ->
(-{sqrt(3)/2},-1/2,1) ->
(-{sqrt(3)/2},1/2,1) ->
(-{sqrt(3)/2},1/2,0);
\end{tikzpicture}
}
\end{document}
答案1
看来您无法在 PGFPlots 函数中编写任意表达式。解析器正在寻找单个浮点数,而不是必须求值才能获得单个浮点数的表达式。您可以做的是将结果保存在宏中,然后每次调用该宏。您必须对and表达式-sqrt(3)/2
执行相同的操作,否则只需将其替换为and 。1/2
-1/2
0.5
-0.5
固定代码:
\documentclass{memoir}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.8,
standard/.style={
axis x line=center,
axis y line=center,
axis z line=center,
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
axis line style=help lines,
every axis/.append style={font=\tiny},
}
}
\begin{document}
{\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[
standard,
hide z axis,
xmin=-1.25, xmax=1.25,
ymin=-1.25, ymax=1.25,
zmin=0, zmax=2.5,
xtick={-1,1},
xticklabels={$-2r$,$2r$},
ytick=\empty,
]
% Draw Circle
\draw[blue] (axis cs: 0,0,0)
ellipse [
x radius=1, y radius=1];
% Draw Square
\draw[green] (axis cs: -0.86602540378,0.5,0) --
(axis cs: -0.86602540378,-0.5,0) --
(axis cs: -0.86602540378,-0.5,1) --
(axis cs: -0.86602540378,0.5,1) --
(axis cs: -0.86602540378,0.5,0);
% I can't use math operators within PGFPlots apparently?
\pgfmathsetmacro\foo{-sqrt(3)/2}
\draw[green] (axis cs: \foo,0.5,0) --
(axis cs: \foo,-0.5,0) --
(axis cs:\foo,-0.5,1) --
(axis cs:\foo,0.5,1) --
(axis cs:\foo,0.5,0);
\end{axis}
% However, I am able to do so in regular Tikz, as long as I put functions like sqrt() in braces...
\draw[green] (-{sqrt(3)/2},1/2,0) ->
(-{sqrt(3)/2},-1/2,0) ->
(-{sqrt(3)/2},-1/2,1) ->
(-{sqrt(3)/2},1/2,1) ->
(-{sqrt(3)/2},1/2,0);
\end{tikzpicture}
}
\end{document}