3

Is it possible to let TikZ save and in subsequent compilation runs use the plot points generated with the PGF math library?

A (simple) example of such a function plotted that way is:

\begin{tikzpicture}
  \draw plot (\x,{sin(2*pi*\x r)});
\end{tikzpicture}

Here, the PGF math library is used to calculate sin(2*pi*\x r). For complex expressions, this is quite time-intensive. I assume this time could be saved if the plot points were available as is the case when using a gnuplot ‘function’ plot. Then (if the plot is given an id) the plot points generated by gnuplot are saved to a .table file, so that they need not be regenerated unless the plot parameters have changed.

  • The big problem is that TeX doesn't really support arrays. A Tikz array is just a comma delimited text string. – John Kormylo Dec 9 '13 at 2:15
  • 3
    May be this answer helps you: tex.stackexchange.com/a/60370/1952. You can create a data file computed with pgfplotstable and use it afterwards to plot it. – Ignasi Dec 9 '13 at 9:09
5

You can use \pgfplotstablenew command from pgfplotstable (which comes with pgfplots) to create a file with your plot points and some conditional to create it when you want. Next example creates the file when doesn't exist.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

\IfFileExists{myplot.dat}{}{
\pgfplotstablenew[
    create on use/plot1/.style={
        create col/expr={sin (2*pi*\pgfplotstablerow)}
    },
    columns={plot1}
]
{700}
\loadedtable
\pgfplotstablesave{\loadedtable}{myplot.dat}
}


  \begin{tikzpicture}
   \begin{axis}[
        line join=bevel,
        no markers,
        table/x expr={\coordindex},
        xmin=0,
        enlarge x limits=false,
        ymin=-1.5,
        ymax=1.5,
    ]
   \addplot[red] table [y expr={\thisrow{plot1}}] {myplot.dat};
\end{axis}

\end{tikzpicture}

\end{document} 

enter image description here

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.