我正在使用这个宏
\newcommand{\ymax}[0]{\pgfkeysvalueof{/pgfplots/ymax}}
做如下的事情:
\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax);
而且效果很好。
但是,如果我想执行类似\ymax / 2
或 的操作0.5 * \ymax
,则此方法无效。我收到错误“无法将输入的‘ 0.5 * 1.09763666e6’解析为浮点数,抱歉。”
我需要使用类似的东西吗\pgfmathparse
?对这个值进行算术运算的正确方法是什么?
当我将表达方式改为
\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \pgfmathparse{0.5*\ymax});
然后我的 TeX Live 冻结了,我不得不^C
退出。
平均能量损失
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false]
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\addplot [smooth, domain=0:6] (x, x); % to generate an axis cs
% this works fine
\draw[dashed] (axis cs:2, \ymin) -- (axis cs:2, \ymax);
% none of these work
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax / 2);
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, (\ymax / 2));
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, ({\ymax / 2}));
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, {(\ymax / 2)});
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, (0.5*\ymax));
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, (0.5*{\ymax}));
%\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, {0.5*(\ymax});
\end{axis}
\end{tikzpicture}
\end{document}
我收到的错误是:
! Package PGF Math Error: Could not parse input ' {0.5*6.0e0}' as a floating po
int number, sorry. The unreadable part was near '*6.0e0'..
See the PGF Math package documentation for explanation.
Type H <return> for immediate help.
答案1
不要使用多余的括号;例如,不要:
\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, (\ymax / 2));
简单使用
\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax / 2);
这可以使用pgfplots 2013/10/03 v1.9
。代码:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false]
\newcommand{\getpgfkey}[1]{\pgfkeysvalueof{/pgfplots/#1}}
\newcommand{\ymax}[0]{\getpgfkey{ymax}}
\newcommand{\ymin}[0]{\getpgfkey{ymin}}
\addplot [smooth, domain=0:6] (x, x); % to generate an axis cs
% this works fine
\draw[dashed] (axis cs:2, \ymin) -- (axis cs:2, \ymax);
% these work
\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax / 2);
\draw[dashed] (axis cs:4.5, \ymin) -- (axis cs:4.5, {\ymax / 4});
\draw[dashed] (axis cs:5, \ymin) -- (axis cs:5, {3*\ymax / 4});
\draw[dashed] (axis cs:3.5, \ymin) -- (axis cs:3.5, 0.666*\ymax);
\draw[dashed] (axis cs:5.5, \ymin) -- (axis cs:5.5, {0.333*\ymax});
\end{axis}
\end{tikzpicture}
\end{document}