3

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

\documentclass{article}
\usepackage{pgfplots}
\usepackage{sagetex}


\begin{document}

\begin{sagesilent}
  f(x) = 2*sin(x^2)
\end{sagesilent}

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

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

\end{document}

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

student
  • 29,003
  • 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 03 '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 03 '15 at 14:53
  • Can't you run the sage output with sagesilent within the function declaration? – percusse Jan 03 '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 http://tex.stackexchange.com/a/197349/3235 – percusse Jan 03 '15 at 15:46
  • @percusse Can you post a minimal working example to make more clear what you mean? – student Jan 03 '15 at 16:34
  • I ran into this problem some time back and I don't think there is a simple answer. I've worked along the lines that percusse suggests; i.e., getting the raw output. I've posted several examples on this site: Riemann zeta Cantor function, Fourier and Weierstrass (not enough characters for that link). – DJP May 06 '15 at 14:10
  • @percusse You don't need Sage to test. Just open a free Sagemath Cloud account to get access to Sage, LaTeX, and a lot more. Extremely powerful--think you'd find it useful! – DJP May 06 '15 at 14:18
  • @DJP Quite true. I'm not too familiar with the hip tools of CAS systems. But I actually wrote a control systems toolbox on Python with some influence of these free tools. I actually selected Python after hearing it from my Sage-junkie colleagues. Can you maybe write an answer for this? – percusse May 06 '15 at 15:27
  • @percusse 3 comments above I mentioned 4 examples that I posted on this site. The method looks different than the link you posted but the idea of getting the data and plotting is essentially the same. – DJP May 06 '15 at 15:35

1 Answers1

2

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

\documentclass{scrartcl}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-fct}
\pagestyle{empty}
\begin{document}
\begin{sagesilent}
x=var('x')
y=var('y')
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])
\end{sagesilent}
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmin=-5,xmax=5,ymax=2]
\tkzGrid
\tkzAxeXY
\sagestr{output}
\end{tikzpicture}
\end{document}

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...

student
  • 29,003