2

I am using this macro

\newcommand{\ymax}[0]{\pgfkeysvalueof{/pgfplots/ymax}}

to do stuff like the following:

\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax);

and this works fine.

However, if I want to do something like \ymax / 2 or 0.5 * \ymax, this doesn't work. I get the error, "Could not parse input ' 0.5 * 1.09763666e6' as a floating point number, sorry."

Do I need to use something like \pgfmathparse? What is the correct way to do arithmetic on this value?

When I change the expression to

\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \pgfmathparse{0.5*\ymax});

then my TeX Live freezes and I have to ^C out.

MWE

\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}

The error I get is:

! 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.
wchargin
  • 3,139
  • Can you draw a mockup of what you want? – Superbest Jan 29 '14 at 04:03
  • Didn't test it but try putting them first in extra parentheses and then braces. that should signal tikz to parse it as math. Emphasis on should :) – percusse Jan 29 '14 at 07:01
  • @percusse Using at (axis cs:4, ({\ymax / 2})) I get Could not parse input ' ({3.3075714e6/ 2}' ; is the lack of space between e6 and / contributing to the problem? – wchargin Jan 30 '14 at 00:03
  • @WChargin The other way around. parentheses inside braces – percusse Jan 30 '14 at 00:05
  • @percusse oh... but at (axis cs:4,{(\ymax / 2)}) still yields Could not parse input '(3.3075714e6/ 2)' as a floating point number... am I missing something? – wchargin Jan 30 '14 at 00:14
  • I don't think so. I have to test what I'm talking about before I claim anything. I'll check and fix an answer if I can soon. – percusse Jan 30 '14 at 00:23
  • I can't reproduce this. When I create a test document, everything works fine. What version of PGFPlots are you using? – Jake Jan 30 '14 at 08:40
  • I have \pgfplotsset{compat=1.8}; could you share your test document? – wchargin Feb 02 '14 at 18:41
  • @WChargin: I think it would be a better idea if you could edit your question to include a minimal example document that reproduces the error you encounter. Maybe I'm misunderstanding your question and my test document isn't actually relevant. – Jake Feb 03 '14 at 09:19
  • @WChargin could you post a minimal working example (MWE) as Jake requested? – Paul Gessler Mar 02 '14 at 02:59

1 Answers1

1

Don't use the extra parentheses; for example, instead of :

\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, (\ymax / 2));

use simply

\draw[dashed] (axis cs:4, \ymin) -- (axis cs:4, \ymax / 2);

This works using pgfplots 2013/10/03 v1.9. The code:

\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}

enter image description here

Gonzalo Medina
  • 505,128
  • ...I don't see what you've changed, and your example doesn't work for me. Could you please elaborate? I get the error: "Could not parse input ' 6.0e0/ 2' as a floating point number, sorry. The unreadable part was near '/ 2 '.." – wchargin Mar 02 '14 at 04:08
  • @WChargin I removed some extra pair of parentheses. As my image shows, the code I posted works OK. If it is not working for you, perhaps you are using an old version of pgfplots? – Gonzalo Medina Mar 02 '14 at 04:10
  • I'm using "Package: pgfplots 2013/03/17 v1.8 Data Visualization (1.8-3-gaf58a29)" (as reported by \listfiles); is this out of date? – wchargin Mar 02 '14 at 04:14
  • (not using parentheses was the first thing I tried; I included parentheses in the MWE because of the comments on my question) – wchargin Mar 02 '14 at 04:14
  • @WChargin It's outdated. I'm using pgfplots 2013/10/03 v1.9 Data Visualization (1.9). Try updating and it should work without the parentheses. – Gonzalo Medina Mar 02 '14 at 04:16
  • Thanks. (It might be helpful to add something to your answer along those lines.) Unfortunately I can't update because I'm running my system off a flash drive with casper-rw and the package manager is flaky; if I apt-get update it nukes my system and I have to create a new image. But thanks for your help. – wchargin Mar 02 '14 at 04:18
  • @WChargin Yes, I've added some remark about this. – Gonzalo Medina Mar 02 '14 at 04:26