9

The commented rows of the following code give me errors. It seems that functions declared with \pgfmathdeclarefunction do not work with tikz commands...

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfmathdeclarefunction{myfunct}{3}{\pgfmathparse{#1+#2*#3}}

\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot {myfunct(1,2,x)};
%   \node at (axis cs:2,{myfunct(1,2,2)}) {aaa};
%   \draw (axis cs:2,{myfunct(1,2,2)}) -- (axis cs:0,5);
\end{axis}
\end{tikzpicture}
\end{document} 

A way to fix this would be to define a macro like:

\pgfmathsetmacro\mymacro{myfunct(1,2,2)}

and use \mymacro instead of myfunct(1,2,2) but I would like to know if there is a way for using directly myfunct(1,2,2) avoiding the macro definition.

2
  • 1
    An unrelated tip: You can also use \tikzset{declare function={myfunc{\a,\b,\c}=\a+\b+\c;}} which has the advantage that you can use descriptive macro names and that \a, \b and \c are already evaluated so you do not worry about adding parentheses around #1 and so on (in your case mathematically irrelevant, but still). Oct 5, 2013 at 19:31
  • 1
    Should it be parentheses after the function handle, ie, \tikzset{declare function={myfunc(\a,\b,\c)=\a+\b+\c;}}?
    – Matt S
    Apr 14, 2019 at 12:14

1 Answer 1

11

What you experience is caused by the fact that pgfplots up to and including version 1.8 applies no expression parsing to axis cs. In other words: it expect(ed) pure numbers like 42, but not 2^2.

This means that your math function is correct, and it would work in any pure tikz operations (axis cs is an add-on of pgfplots).

This has been improved for the current pgfplots stable 1.9: now, axis cs accepts math expressions. Note that version 1.9 is not one week old at the time of this writing, so it may take some time for MikTeX etc to pick it up. Upgrading pgfplots will solve the problem.

1
  • Thanks, so no way of doing it with older versions of pgfplots.. I will update it
    – Red
    Oct 6, 2013 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.