1

I would like to plot sin(x) on a LaTeX document (an article) by using gnuplot commands programmatically inside the document.

I am aware that you can save the plot as a .tex file and then insert it in the document using \input{}. However this is not what I am asking for. I would like to learn how to generate the plot inside the document source code.

This is the plot that I get when I use the gnuplot console, so this is the result I am expecting to get on my document too.

gnuplot sin(x)

I am using MiKTeX and TeXstudio, though this last is probably not relevant.

  • You might benefit from searching for, and looking at some of the other questions regarding gnuplot. For instance tex.stackexchange.com/questions/6744/gnuplot-epslatex – A Feldman Feb 16 '16 at 23:48
  • What about the below answer is not an answer to your question? To me, your problem seems to have been solved by erik. – hooy Feb 17 '16 at 13:31
  • @hooy When I run the code provided in erik's answer I get an error (you can read about it on my comment), so I won't mark the question as answered until I get some working code. Did you run the code? – codeaviator Feb 17 '16 at 19:55
2

The pgfplots package has an \addplot gnuplot command, which uses gnuplot to do the calculation and then import the data for plotting by pgfplots. In the following example, the first function is drawn in blue, the second in red.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
  \begin{axis}
  \addplot gnuplot {sin(x)};
  \addplot {sin(x)};
  \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thanks for your contribution. What is the red line supposed to be? Why do you emphasize the fact that there are two functions? Can you tell me about them? – codeaviator Feb 17 '16 at 0:55
  • 1
    @Cebri I think the second line illustrates that tikz sin(x) expects x in degrees, not radians. Hence sin(deg(x)) produces same red as gnuplot blue. – user4686 Feb 17 '16 at 12:52
  • @jfbu Thanks for your inputs, the red line indeed corresponds to the sin(x) in degrees. – codeaviator Feb 17 '16 at 14:50
  • When I run erik's code on my editor and compile, I get the following error: line 7: Package pgfplots Error: Sorry, the gnuplot-result file 'plotTest.pgf-plot.tab<file>.gnuplot' manually on the respective gnuplot file.. \addplot gnuplot {sin(x)}; : running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.13} into your preamble. The output image that I get only shows the red sine but not the blue one. Anybody knows what's wrong? – codeaviator Feb 17 '16 at 14:53
  • @Cebri Did you try adding \pgfplotsset{compat=1.13} to your preamble? If that didn't work, perhaps this question will be helpful tex.stackexchange.com/q/120218/24974 – erik Feb 17 '16 at 15:36

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.