September 8, 2010 at 05:28 - by eelvex | one comment (Leave a comment)

*(Here is my list of very good books to learn latex.)*

For years I was mostly using gnuplot.

This is how I worked (and still am very often):

- Generate postscript plot
- Convert postscript to pdf
epstopdf out.eps
- Insert into .tex file
\documentclass{article}

\usepackage{graphicx}

\title{A gnuplot ``diagram''}

\begin{document}

\maketitle

\begin{figure}[ht]

\center

\includegraphics[width=7cm]{out}

\caption{My caption}

\end{figure}

\end{document}Sometimes, I use the options \includegraphics[type=pdf,ext=.pdf,read=.pdf,width=7cm]{out}

- Finally, compile to pdf (gnuplot.pdf)
pdflatex gnuplot.tex

Sometimes, when I repeatedly used a simple diagram, I was putting it in a picture-environment newcommand. I still think this is quite flexible for this kind of stuff:

- Create the newcommand
\newcommand{\stamp}

{

\begin{picture}(50,30)

\put(0,15){\vector(1,0){50}}

\put(25,0){\vector(0,1){30}}

\qbezier(25,15)(34, 24)(45, 24.6)

\qbezier(25,15)(16, 6)(5, 5.4)

\end{picture}

} - Use it:
\documentclass{article}

\setlength{\unitlength}{.5mm}

\newcommand{\stamp}

{

\begin{picture}(50,30)

\put(0,15){\vector(1,0){50}}

\put(25,0){\vector(0,1){30}}

\qbezier(25,15)(34, 24)(45, 24.6)

\qbezier(25,15)(16, 6)(5, 5.4)

\end{picture}

}

\begin{document}

This is a \stamp of the \stamp lorem

ipsum dolor sit amet.

\begin{figure}

\center

\stamp

\caption{As a figure}

\end{figure}

\end{document}

When the time came that I had to be more serious about my graphs, I learned some TikZ.

Pgf/TikZ is rich and powerful but you kinda have to use gnuplot for some plots (complicated functions and maybe data plotting). It has a truly nice manual and there are lots of examples available. It's basic usage is very simple:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\pagestyle{empty}

\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)$};

\end{tikzpicture}

\end{document}

\usepackage{tikz}

\begin{document}

\pagestyle{empty}

\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)$};

\end{tikzpicture}

\end{document}

To turn this into an example that uses gnuplot:

- Add some functions and labels:
\documentclass{article}

\usepackage{tikz}

\begin{document}

\pagestyle{empty}

\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}(tkz.tex)

- Run
pdflatex tkz.tex
- For some combinations of versions pgf/tikz and gnuplot, latex will complain
`Package pgf Warning: Plot data file `tkz.exp.table' not found.`

or something like that.

In that case:- There are now three gnuplot files: tkz.exp.gnuplot, tkz.sin.gnuplot and tkz.x.gnuplot

Edit them and change`set terminal table`

to`set table`

- Run
gnuplot tkz.*.gnuplot
- Run again
pdflatex tkz.tex

- There are now three gnuplot files: tkz.exp.gnuplot, tkz.sin.gnuplot and tkz.x.gnuplot
- We know have tkz.pdf:

Asymptote! Asymptote is a vector graphics **language**... Use it! There are hundreds of examples out there.

Asymptote produces .eps postscript files; I handle them the same way I handle the gnuplot-generated files:

- Write some asymptote code (oth.asy):
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29import graph3;

import palette;

size(0,300);

currentprojection=perspective(3,-2,2);

real V(real r) {return r^4-r^2;}

real V(pair pos) {return V(abs(pos));}

real R=1/sqrt(2);

real z=-0.2;

bool active(pair pos) {return abs(pos) < R;}

bool above(pair pos) {return V(pos) >= z;}

pair a=(-1.5,-1);

pair b=(0.5,1);

real f=1.2;

draw(plane(f*(b.x-a.x,0,z),(0,f*(b.y-a.y),z),(a.x,a.y,z)),

lightgrey+opacity(0.5));

surface s=surface(V,a,b,40,Spline,active);

draw(s,mean(palette(s.map(new real(triple v) {

return above((v.x,v.y)) ? 1 : 0;}),

new pen[] {lightblue,lightgreen})),black);

xaxis3(Label("$\phi^\dagger\phi$",1),red,Arrow3);

zaxis3(Label("$V(\phi^\dagger\phi)$",1),0,0.3,red,Arrow3); - Compile:
asy oth.asy
- Convert to pdf
epstopdf oth.eps
- Include in .tex
\begin{figure}[ht]

\includegraphics[width=7cm]{oth}

\end{figure}

I love gnuplot, I befriend begin{picture}, I adore TikZ, I work with Asymptote.

## Plotting in LaTeX, the options | Manos Tsagkias

on September 8, 2010 at 11:15

[...] listed in a recent post a few options for plotting in LaTeX: gnuplot, TiKZ, and Asymptote, along with their pros, cons, and a few examples from each. This [...]