[Tex/LaTex] Gnuplot and TiKZ: centering plots

gnuplotpdftextikz-pgf

I'm trying to plot an implicit function in a LaTeX document, and I'm using TiKZ for all my diagrams. For the implicit function, I'm calling gnuplot, and I've found that the best solution, which seems to be portable across my different systems, is to use the gnuplottex package, and set the gnuplot terminal to be epslatex. I've tried using \draw[raw gnuplot] in TiKZ, and I've experimented with pgfplots; but none of these gives me a correct plot. (Well, in fact raw gnuplot gives a wonderful result on one of my systems, and a messy job with crazy lines everywhere on another.)

However! even though the gnuplot output is in itself correct, I actually want it in a larger TIKZ diagram, so I need to somehow ensure that gnuplot's (0,0) coincides with TiKZ's, when I treat the gnuplot picture as a TiKZ node:

\begin{tikzpicture}
  \draw[color=lightgray] (-4,0) -- (4,0);
  \draw[color=lightgray] (0,-4) -- (0,4);
  \draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
  \node[scale=1.2] at (0,0) {
    \begin{gnuplot}[terminal=epslatex]
       set contour
       set cntrparam levels incremental -0.02,0.01,0.02
       set view map
       set view equal
       unset surface
       unset key
       unset tics
       unset border
       set isosamples 1000,1000
       set xrange [-3.5:3.5]
       set yrange [-3.5:3.5]
       set view 0,0
       set cont base    
       splot x**3 + y**3 - 6*x*y
    \end{gnuplot}
    };
\end{tikzpicture}

At least I'd like the axes and a few other things drawn with TiKZ, and then I can position the gnuplot graph correctly on the diagram.

However, currently the gnuplot (0,0) is somewhat to the right and above TiKZ's (0,0). I know I can use the TiKZ grid command and reposition the axes so that they are in the right place, but that seems a bit fiddly.

Is there a simple, elegant, portable way of lining up TiKZ and a gnuplot plot?

Note: For a complete example, see epslatextest.tex and epslatextest.pdf.

\documentclass{article}

\usepackage{tikz}
\usepackage[shell]{gnuplottex}

\begin{document}
The difficulty is shown in the figure.

\begin{figure}[h]%
  \centering%
  \begin{tikzpicture}
    \begin{scope}
      \draw[color=lightgray] (-4,0) -- (4,0);
      \draw[color=lightgray] (0,-4) -- (0,4);
      \draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
    \end{scope}

    \node[scale=1.2] at (0,0) {
\begin{gnuplot}[terminal=epslatex]
   set contour
   set cntrparam levels incremental -0.02,0.01,0.02
   set view map
   set view equal
   unset surface
   unset key
   unset tics
   unset border
   set isosamples 1000,1000
   set xrange [-3.5:3.5]
   set yrange [-3.5:3.5]
   set view 0,0
   set cont base
   splot x**3 + y**3 - 6*x*y
 \end{gnuplot}
};
  \end{tikzpicture}

  \caption{A test of gnuplottex with epslatex}
  \label{fig:gnuplot}
\end{figure}
\end{document}

Best Answer

I don't know if this is the best(tm) way, but it works :)

You can use the tikz terminal, and give it the size you want. For this you must set the plot size equal to the canvas size (set lmargin ... etc.). (I haven't tried with the plotsize option).

To overlap your picture with the gnuplot tikzpicture environment, you can shift the scope.

\documentclass{article}

\usepackage{tikz}
\usepackage{gnuplot-lua-tikz}
\usepackage[shell]{gnuplottex}
\thispagestyle{empty}

\begin{document}
The difficulty is shown in the figure.

\begin{figure}[h]%
  \centering%
  \begin{tikzpicture}
    \begin{scope}[shift={(4,4)}]
      \draw[color=lightgray] (-4,0) -- (4,0);
      \draw[color=lightgray] (0,-4) -- (0,4);
      \draw[dashed,color=lightgray] (-1,-1) rectangle (1,1);
    \end{scope}
    \begin{gnuplot}[terminal=tikz,terminaloptions={size 8,8}]
      set contour
      set cntrparam levels incremental -0.02,0.01,0.02
      set view map
      set view equal
      unset surface
      unset key
      unset tics
      unset border
      set lmargin at screen 0
      set rmargin at screen 1
      set bmargin at screen 0
      set tmargin at screen 1
      set isosamples 1000,1000
      set xrange [-3.5:3.5]
      set yrange [-3.5:3.5]
      set view 0,0
      set cont base
      splot x**3 + y**3 - 6*x*y
    \end{gnuplot}
  \end{tikzpicture}

  \caption{A test of gnuplottex with epslatex}
  \label{fig:gnuplot}
\end{figure}
\end{document}

enter image description here