Graphics, Figures & TablesMath formatting in Gnuplot Graph

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
breakfast_jimmy
Posts: 2
Joined: Sat May 28, 2011 2:09 am

Math formatting in Gnuplot Graph

Post by breakfast_jimmy »

Hello all,

Firstly, forgive me if my question is an idiotic one; I'm new to the world of LaTeX. I'm also hoping this is indeed a latex problem and not a gnuplot one :?.
Well, I want to generate a graph in gnuplot and include it in a latex document; thankfully the graph I need is a simple one. I'm following the tutorial given on http://chuyandmac.blogspot.com/2007/05/ ... using.html ; thus far, everything works - save the mathematical formatting on the y axis. The superscript works, but the \frac line simply shows up as frac, with no division (pardon me, that was phrased somewhat awkwardly)
I'm using mactex and doing all my editing in aquamacs, if that's relevant. I have the feeling that this is a simple problem to fix, but I cannot for the life of me figure it out.
Thanks in advance.
Last edited by breakfast_jimmy on Sun May 29, 2011 9:32 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Math formatting in Gnuplot Graph

Post by localghost »

I would try another approach with special LaTeX packages for generating plots. Then you wouldn't have to worry about font consistency in your LaTeX document. Furthermore you could evade the problems you described.

At first there would be pgfplots (based on pgf/tikZ).

Code: Select all

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[%
      xlabel={x},
      ylabel={$\left(\frac{\sin(x)}{x}\right)^2$},
      xmin=-10,xmax=10,ymin=0,ymax=1,
      xtick={-10,-5,...,10},
      ytick={0,0.2,...,1}
    ]
      \addplot[domain=-10:10,samples=100] gnuplot {(sin(x)/x)**2};
    \end{axis}
  \end{tikzpicture}
\end{document}
The package uses Gnuplot as back-end. This version can be compiled directly with PDFLaTeX.

Another one would be pst-plot (based on PSTricks).

Code: Select all

\documentclass{article}
\usepackage{pst-plot,pstricks-add}

\begin{document}
  \psset{%
    llx=-1.5cm,lly=-1cm,
    xAxisLabel={$x$},
    xAxisLabelPos={0,-0.1},
    yAxisLabel={$\left(\frac{\sin(x)}{x}\right)^2$},
    yAxisLabelPos={-2.5,0.5}
  }
%  \psframebox[linestyle=dashed,linewidth=0.4pt]{%
  \begin{psgraph}[axesstyle=none,ticks=none](-10,0)(10,1){10cm}{7cm}
    \psplot[algebraic,plotpoints=100]{-10}{10}{(sin(x)/x)^2}
    \psaxes[axesstyle=frame,ticksize=5pt,tickstyle=inner,Dx=5,Dy=0.2,Ox=-10](-10,0)(10,1)
  \end{psgraph}
%  }
\end{document}
This version needs to be compiled with LaTeX to DVI and then converted via PS to PDF (XeLaTeX or PDFLaTeX with the additional auto-pst-pdf package didn't work for me).

Further adjustments are left to the interested reader. Study the manuals of the involved packages and follow their instructions. The code for the actual plots can easily be embedded into a float environment. In case of upcoming questions, feel free to ask. If you insist doing your plots with Gnuplot, have a look at the gnuplottex package.


Best regards and welcome to the board
Thorsten
breakfast_jimmy
Posts: 2
Joined: Sat May 28, 2011 2:09 am

Re: Math formatting in Gnuplot Graph

Post by breakfast_jimmy »

Oh, thank you! Much appreciated.
User avatar
shadgrind
Posts: 140
Joined: Thu Jul 16, 2009 12:39 am

Math formatting in Gnuplot Graph

Post by shadgrind »

breakfast_jimmy wrote:I'm following the tutorial given on http://chuyandmac.blogspot.com/2007/05/ ... using.html ; thus far, everything works - save the mathematical formatting on the y axis. The superscript works, but the \frac line simply shows up as frac, with no division
That's because that guy's example is all wrong. He used double quotes instead of single quotes for math mode, which he then messed up in another way in a few spots. Here's what you do in gnuplot:

Code: Select all

unset key
set xlabel '$x$'
set ylabel '$\left(\frac{\sin(x)}{x}\right)^{2}$'
plot (sin(x)/x)**2
set terminal epslatex color
set output "myplot.tex"
replot
This will actually create two files: myplot.tex and myplot.eps. You could then use those in LaTeX like this:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\begin{document}
Here is a graph:
\begin{center}
 \input{myplot}
\end{center}
\end{document}
Here is what it will look like after compiling (note the use of color, which the original example left out):
Attachments
jimmytest.png
jimmytest.png (14.55 KiB) Viewed 10734 times
System: TeX Live 2012, Fedora 18 x86_64, GNU Emacs 24.2
Post Reply