Is there any way to pass a function defined in sagtex to pgfplots for plotting? See the following example.



  f(x) = 2*sin(x^2)

$f(x) = \sage{f(x)}$

    \begin{axis}[no markers, samples=100]
%      \addplot gnuplot {\sageraw{f(x)}};
      %should be the same as
      \addplot gnuplot {2*sin(x^2)};


Edit: I also tried \pgfmathdeclarefunction{function}{\sagestr{print(f(x))}} and then \addplot gnuplot {function(x)}; but it didn't work.

Edit 2: I have just opened a feature request for sagetex: https://github.com/dandrake/sagetex/issues/1 I hope there will be a solution for this issue soon

  • Declare a math function, \pgfmathdeclarefunction{generic sage} with one argument. Assign the output to the raw sage output. I don't have Sage so I can't test it. – percusse Jan 3 '15 at 14:43
  • How can I get the "raw sage output"? The command \sageraw above doesn't exist, it was only a fake command. – student Jan 3 '15 at 14:53
  • Can't you run the sage output with sagesilent within the function declaration? – percusse Jan 3 '15 at 15:07
  • You don't need gnuplot there, the function should only call sagetex retrieve a value and assign it. For example here, I'm calling table macros to retrieve values etc. and assigning to the output as if it was a math function tex.stackexchange.com/a/197349/3235 – percusse Jan 3 '15 at 15:46
  • @percusse Can you post a minimal working example to make more clear what you mean? – student Jan 3 '15 at 16:34

I do not have sage installed, but give it a try...

x_coords = [x for x in srange(0,10)]
y_coords = [2*sin(x*x) for x in srange(0,10)]
output = ""
for i in range(0,len(x_coords)-1):
    output+=r"\draw[red, thick] (%f cm ,%f cm)--(%f cm ,%f cm);"%(x_coords[i],y_coords[i],x_coords[i+1],y_coords[i+1])

maybe you have to adapt the example which was used from http://www.sagemath.org/doc/tutorial/sagetex.html

But I cannot test it at the moment...

  • Is this code working? – Peter Ebelsberger May 7 '15 at 7:03
  • Yes it works after I fixed some minor mistakes. But it seems to complicated, it is not exactly what I want. – student May 7 '15 at 7:12
  • Sorry for that, I only tried to solve the problem without the possibility to test the code. – Peter Ebelsberger May 7 '15 at 14:45

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.