# Consistently specify function to be graphed with or without gnuplot

I would like to be able to specify a function and then graph that with with pgfplot only, or use gnuplot to compute the coordinates. As far as I know there are two main differences in how the functions are parsed:

1. pgf uses ^ and gnuplot uses ** for exponentiation. This is easily fixed with the translate gnuplot=true, so is not a problem.

2. Another is that pgf uses degrees for trigonometric functions and gnuplot uses radians. Is there a way to adjust this also?

But, ignoring the possible problem with #2 above, is there a way to specify the function once and use pgfplots to graph it with or without gnuplot? So far, I have been specifying the pgfplots function as follows:

\pgfmathdeclarefunction{FunctionF}{1}{\pgfmathparse{(#1)^(3)}}%


But, when I want to gnuplot I have been defining it as follows:

\newcommand*{\FunctionFGnuplot}{(x)^3}%


Is there not a way I can specify the function once and use it both cases? Just to be clear I don't have an issue with the fact that the format of the addplot command is different between the two, just the specification of what the function is.

Here is a MWE illustrating this:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-2.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}

\pgfkeys{/pgfplots/Axis Style/.style={
clip=false,% so that we can see labels placed outside
xmin=\XAxisMin, xmax=\XAxisMax,
ymin=\YAxisMin, ymax=\YAxisMax,
domain=\DomainMinF:\DomainMaxF,
width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
translate gnuplot=true,% can use ‘^’ instead of ‘**’
id=foo,
mark=none,%
domain=\DomainMinF:\DomainMaxF,%
samples=50,%
ultra thick,
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%
%-----------------------------

% I would like to only have to specify the function here once
\pgfmathdeclarefunction{FunctionF}{1}{\pgfmathparse{(#1)^(3)}}%
\newcommand*{\FunctionFGnuplot}{(x)^3}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[Axis Style]
({x},{FunctionF(x)});%
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
gnuplot% Use Gnuplot for Graph 2
{\FunctionFGnuplot};%
\end{axis}
\end{tikzpicture}
\end{document}

-
At least in this case, you can say either \addplot {\FunctionFGnuplot}; or \addplot gnuplot {\FunctionFGnuplot}, both will give you the same result. Can you give an example where this does not work? –  Jake Oct 26 '11 at 11:26
I would agree with Jake's comment: the approach to define a macro which contains the math expression should work in most cases. There are most certainly cases where this will fail (like boolean arithmetics). But the most important case where it fails is (as already noted as point 2): trigonometric functions. Perhaps it would be a good idea to introduce some sort of switch for tikz/pgfplots to use radians (at least inside of plot coordinates). –  Christian Feuersänger Oct 29 '11 at 18:39
As of Nov 3, 2011, the \addplot {<function>} method also supports '==, !=, <=, >=, ?. See tex.stackexchange.com/questions/33584/… –  Christian Feuersänger Nov 3 '11 at 21:26

An answer to question 2: You can tell gnuplot to use degrees instead of radians by issuing a set angles degrees; in the gnuplot code.

You can define a new style to add that line into the gnuplot code generated by PGFplots:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\makeatletter
\pgfplotsset{
use degrees in gnuplot/.code={
\xdef\pgfplots@gnuplot@format{\pgfplots@gnuplot@format set angles degrees;}
}
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:360, samples=50, enlarge x limits=false, clip=false]
{sin(x)}
node [anchor=north west] {PGFplots};

@ChristianFeuersänger, Jake: This is excellent and pretty much makes the two consistent. Posted a follow up question: consistently specify a function and use it for computation and plotting which is why I had been using pgfmathdeclarefunction to define function and now am having questions as I also want to be able to use gnuplot`. –  Peter Grill Nov 3 '11 at 22:16