18

Gnuplot is power for plotting graph in scientific progress.

I have met this problem. I want to add some commands like "$\ddot{x}$".

or $\tau$ on the legend.

I see some people suggest that

plot [-5:5] [-1.5:1.5] sin(x+pi) title "$\\sin(x+\\pi)$"

But it seems it does not work.

1
  • 1
    Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – bersanri
    Jun 17, 2013 at 11:25

2 Answers 2

24

To get LaTeX in labels or legends in Gnuplot, you need to use one of the terminals that produce LaTeX code.

For example, you could use the latex terminal:

set terminal latex
set out 'plot.tex'
plot [-5:5] [-1.5:1.5] sin(x+pi) title "$\\sin(x+\\pi)$"
set out

That will produce a plot.tex file that includes the plot using basic TeX statements. You can include that file in your main document using \input{plot.tex}:

\documentclass{article}
\begin{document}
\input{plot.tex}
\end{document}

Which will give you

(notice that the quality isn't overwhelming when using the latex terminal).

You can also use the epslatex terminal:

set terminal epslatex color
set out 'plot.tex'
plot [-5:5] [-1.5:1.5] sin(x+pi) title "$\\sin(x+\\pi)$"
set out

This will include a plot.tex file with all the labels, and a separate plot.eps file including the graphical elements. You can include the output using

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\input{plot.tex}
\end{document}


Alternatively, you could create the plots directly within LaTeX, using a package like PGFPlots or pst-plot:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    enlarge x limits=false,
    legend entries=$\sin{x+\pi}$
]
\addplot [domain=-1.5*pi:1.5*pi, smooth] {sin(deg(x+pi))};
\end{axis} 
\end{tikzpicture}
\end{document}

5
  • Method 1 is working, but I here got is a pdf document,then take a screenshot of pdf? Method 2, I got a .eps document but losing the title.
    – Jimmy Wang
    Jun 17, 2013 at 12:28
  • Method 2 is also working, but what I got is pdf document.
    – Jimmy Wang
    Jun 17, 2013 at 12:30
  • @JimmyWang: PDF is the output format of pdflatex. What format do you want?
    – Jake
    Jun 17, 2013 at 12:31
  • I see. If I wanna put this graph in other document: one way is to get a screenshot, or directly put plot.tex and plot.eps in that main latex document. Is that right?
    – Jimmy Wang
    Jun 17, 2013 at 12:42
  • @JimmyWang: You just put \input{plot.tex} wherever you want the plot to appear.
    – Jake
    Jun 17, 2013 at 12:44
7

You can use the epslatex terminal that comes with gnuplot. (There are some others like cairolatex that can do the same, but not all of them are included in a standard install.)

First you switch to the epslatex terminal, if it's not the standard terminal.

sett epslatex

Then you define the name of your output.

set output 'name.tex'

This will generate and empty name.tex file and once you plot your figure you'll get a name.eps file as well. You can also specify where the file should come by giving a path like set output /users/yourname/desktop/name.tex. Now you can add LaTeX code to the axis labels. Once you're done you just plot with the standard plot command (and all the options you need). Type set output a last time to make sure everything it's written to the file you specified earlier.

Now you've ended up with a .tex file containing the all the numbers and labels for you axis and an .eps file that contains the actual plot. Put both of these files in the same folder as the .tex file in which you would like to put the plot. You can include these with the following code

\begin{figure}[t]
\centering
\input{name}
\end{figure}

This relies on the graphicx package and possibly on the epstopdf package if you're not using MacTex.

The beauty of this solution is that TeX renders all the labels and you can edit them by editing the name.tex file in which they're stored.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .