# How to use GNU Plot to plot diagrams in TexShop

hope anyone can help with this... I have a very basic knowledge of TeXShop, but I need to plot some functions on it, say y=x. I learnt that this can be done by installing GNU Plot and writing in TeXShop

\begin{tikzpicture}[domain=0:4]
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=black] plot[id=x] function{x}
node[right] {$f(x) =x$};
\end{tikzpicture}


So I have downloaded GNU Plot and dragged it from the disk image to applications. I don't know how to use it but it's there. However when I run the above in TeXShop only the axis and labels pop up, but no function!

I have read many sites now, but it all seems too complicated for me - can anyone help?

-
I think you need X11 wich apple calls xquartz and you will need aquaterm i dont know why. thats what i found on google but apparently it does not help me either :( i can open gnuplot from the terminal and plot with aquaterm but i still cant see the plot in texshop anybody knows what i have to do ? –  dori Mar 17 '13 at 15:25
If you want to plot functions using LaTeX/tikz, you may be interested in pgfplots, compare pgfplots.sourceforge.net –  Christian Feuersänger Mar 17 '13 at 17:13

Here is what I did to get gnuplot working on a Mac. (You need to use the Terminal for this.)

Goto GNU Plot on Sourceforge and download the source code. This should unzip in a folder gnuplot-4.6.0 in your Downloads folder.

Open a Terminal window and issue the following commands: (hit the return key after each; the last three commands will produce quite a bit of terminal output and you'll need to wait for them to be done before issuing the next command.)

cd ~/Downloads/gnuplot-4.6.0
./configure
make
sudo make install


This will correctly install GNUPlot on your machine.

Now in TeXShop, you can make a simple document:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=black] plot[id=x] function{x} node[right] {$f(x) =x$};
\end{tikzpicture}
\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot[id=x] function{x} node[right] {$f(x) =x$};
\draw[color=blue] plot[id=sin] function{sin(x)} node[right] {$f(x) = \sin x$};
\draw[color=orange]
plot[id=exp] function{0.05*exp(x)} node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
\end{document}


Run this with TeXShop. You should get the following output:

-
Does this code actually use gnuplot? –  Peter Grill Sep 11 '12 at 17:44
@PeterGrill Yes. If you try to compile the document without gnuplot installed, you get garbage. –  Alan Munn Sep 11 '12 at 18:30
Ok, but for some reason I thought you had to explicitly specify to use gnuplot, otherwise the pgf math engine would be used. –  Peter Grill Sep 11 '12 at 18:33
@PeterGrill That's the difference between plot and plot function. –  Alan Munn Sep 11 '12 at 18:37
Ahhh, yes I was thinking of pgfplots where you use \addplot gnuplot. I think I had decided a long time ago to do plots only in pgfplots, so totally forgot about this.. Thanks for the clarification. –  Peter Grill Sep 11 '12 at 18:39

Alan's answer is pretty much how I did it: I'd like to share some additional details.

It took me a while to work this one out. I had latex talk to gnuplot on my linux machine without doing anything, but struggled on Mac OS X. So for the record (most likely my future self), I'd like to summarize the various pieces of information I gathered from numerous searches.

2. Unpack in a temporary directory

3. cd into the directory

4. At the terminal command line:

./configure --with-readline=builtin


4b. If you get the following error:

configure: error: in /opt/gnuplot-4.6.4':
configure: error: no acceptable C compiler found in \$PATH


Get command_line_tools_os_x_mountain_lion_for_xcode__october_2013.dmg from:

This requires an appleid for download. Setting up an appleid is a great pain (I found a copy for direct download by searching pastebin).

The command line tools are much smaller than XCode and should be sufficient to install and run gnuplot.

After 4b go back to 4. and try again.

4c. At some point you may need XQuartz. XQuartz is the new X11, a window emulator in which gnuplot can run and plot. Download from the XQuartz project: http://xquartz.macosforge.org. Because older applications still look for XQuartz by the name of X11. Make a link from XQuartz to X11 by running:

sudo ln -sf /Applications/Utilities/XQuartz.app/ /Applications/Utilities/X11.app


To get gnuplot to automatically run in XQuartz:

echo "export GNUTERM='x11'" >> .bash_profile


After 4. is completed, got to 5.

5. Command line:

make

6. command line:

sudo make install

7. Add gnuplot to the PATH:

sudo nano /etc/paths


add the following line to the paths and save: /usr/local/libexec/gnuplot

8. Test

gnuplot
plot sin(x) with lines

9. Make LaTeX talk to gnuplot

Create a config file in your texmf-local folder, named texmf.cnf, with the following:

shell_escape_commands = \
bibtex,bibtex8,\
kpsewhich,\
makeindex,\
mpost,\
repstopdf,\
gnuplot,\


The file must end with a blank line. Save or copy this file into the following directory:

/usr/local/texlive/texmf-local/web2c


Then update the TeX database. You should now be able to run the following code, for instance:

Recreating a guilloche with TikZ

10. To call gnuplot, you need to allow shell-escape. Test with latexmk:

latexmk --synctex=1 -interaction=nonstopmode --shell-escape


or with latex -> dvips -> ps2pdf:

latex -synctex=1 -interaction=nonstopmode --shell-escape %.tex: dvips -o %.ps %.dvi: ps2pdf %.ps
`

There are several hints and explanations here:

Setting up LaTeX -> DVIPS -> PS2PDF with Shell-Escape (in TeXStudio)

-

Using OSX Mountain Lion you have to install aquaterm and X11 or Xquartz first.

-
Welcome to TeX.sx! Can you please give a little bit more details? What are aquaterm and Xquartz? –  Kurt Mar 17 '13 at 14:30
SOLUTION: first you have to install X11 xquartz.macosforge.org/landing Than Aquaterm sourceforge.net/projects/aquaterm After the installation you have to go to preferences (cmd ,) in TEXSHOP and add after: => Latex: pdflatex --shell-escape Good luck and have fun, now it is even working for me =) Concerning your question, i do not know what these programms do but it is working for me, when somebody elso knows more details i would be happy to hear about them too –  dori Mar 17 '13 at 15:40