1

I know how to annotate a picture using \node and includegraphics, see here. But I need to include a pic createed with gnuplot and epslatex terminal. The resulting *.tex looks something like this (shortened):

% GNUPLOT: LaTeX picture with Postscript
\begingroup
  \makeatletter
  \providecommand\color[2][]{%
    \GenericError{(gnuplot) \space\space\space\@spaces}{%
      Package color not loaded in conjunction with
      terminal option `colourtext'%
    }{See the gnuplot documentation for explanation.%
    }{Either use 'blacktext' in gnuplot or load the package
      color.sty in LaTeX.}%
    \renewcommand\color[2][]{}%
  }%
  \providecommand\includegraphics[2][]{%
    \GenericError{(gnuplot) \space\space\space\@spaces}{%
      Package graphicx or graphics not loaded%
    }{See the gnuplot documentation for explanation.%
    }{The gnuplot epslatex terminal needs graphicx.sty or graphics.sty.}%
    \renewcommand\includegraphics[2][]{}%
  }%
  \providecommand\rotatebox[2]{#2}%
  \@ifundefined{ifGPcolor}{%
    \newif\ifGPcolor
    \GPcolortrue
  }{}%
  \@ifundefined{ifGPblacktext}{%
    \newif\ifGPblacktext
    \GPblacktexttrue
  }{}%
  % define a \g@addto@macro without @ in the name:
  \let\gplgaddtomacro\g@addto@macro
  % define empty templates for all commands taking text:
  \gdef\gplbacktext{}%
  \gdef\gplfronttext{}%
  \makeatother
  \ifGPblacktext
....
\begin{picture}(7200.00,5040.00)%
    \gplgaddtomacro\gplbacktext{%
      \csname LTb\endcsname%%
      \put(2034,4534){\makebox(0,0){\strut{}(a)}}%
      \put(983,3955){\makebox(0,0)[l]{\strut{}(a)}}%
    }%
    \gplgaddtomacro\gplfronttext{%
      \csname LTb\endcsname%%
...
    \gplbacktext
    \put(0,0){\includegraphics{mypath/mypic.eps}}%
    \gplfronttext
  \end{picture}%
\endgroup

This needs to be included via "input" . So how can I embed this kind of picture within the tikzpicture environment and annotate it?

OD IUM
  • 140

1 Answers1

2

Prologue

I guess you won't find many users here who use gnuplot with latex regularly.

If you want to create plots that integrate well into your document you find alternatives which follow a more tex'ish approach with a larger user base like pgfplots.

However it's not impossible to create high quality graphs with gnuplot and latex.

I don't think that either the epslatex, the cairolatex or the pdf terminal of gnuplot is qualified for this task though. Since all of these terminals create self-contained "images" from gnuplot files. This approach doesn't contribute to a well typeset document.

In contrast gnuplot's latex and tikz terminals don't create self-contained images but latex code from which latex compiles the final graphs. The resulting output is of far higher typographical quality.

The following example shows how to integrate graphs created with gnuplot's tikz terminal into your document and how to annotate the graph.

It uses siunitx to typeset numbers, gnuplot-lua-tikz which is provided by gnuplot and gnuplottex which supports to create graphs with gnuplot from a single tex-file.

Simple Way to Annotate Gnuplot's Output (tikz-Terminal)

The annotation takes place within the tikzpicture environment. All nodes of the plot are annotated after the gnuplot environment.

I don't know exactly what you want to achieve but for merely annotating the graph this approach should do it.

I hope the code is as self-explanatory as possible for you.

But for motivational purposes the compiled output first. As you can see the font is consistent over the whole page.

enter image description here

\documentclass[10pt]{article}

\usepackage{tikz}
\usepackage{siunitx}
\usepackage[siunitx]{gnuplottex}
\usepackage{gnuplot-lua-tikz}
\usepackage{lipsum}

\tikzset{
    tick style/.style = {
        black, inner sep=1ex, rounded corners=0.4ex, font=\small, 
    },
    gp node right/.append style = {
        tick style, fill=red!20, font=\footnotesize
    },
    gp node center/.append style = {
        tick style, fill=green!20, yshift=-3ex
    }
}

\begin{document}

\lipsum
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{gnuplot}[
            terminal=tikz, 
            terminaloptions={
                color size 10,8 nopicenvironment originreset tightboundingbox createstyle
            }
        ]
            set yrange [-2:2]
            set ytics 1.0
            set samples 500
            set key spacing 2.5
            set zeroaxis
            set grid xtics
            set grid ytics
            plot [-10:10] 10**-2*sin(4*x)*x**2 lw 4 t '$10^{-2}x^2\sin{4x}$',\
            3/(1+exp(-x))-1.5 ls 2  lt 3 t '$\displaystyle \frac{3}{1+\exp{-x}}-1.5$'

        \end{gnuplot}

        % Create all labels
        \foreach \i/\j in {%
            {west}/{left}, {north west}/{above left},
            {north}/{above}, {north east}/{above right},
            {east}/{right}, {south east}/{below right},
            {south}/{below}, {south west}/{below left},
            {center}/{above}%
        } {
            \fill[red] (gp plot 1.\i) circle[radius=1mm] node[\j, text=black, fill=white, fill opacity=0.6, text opacity=1, inner sep=2ex]{\i};
        }

    \end{tikzpicture}

    \caption{Gnuplot tikz terminal in action}
\end{figure}

\end{document}

Smart Way to Annotate Gnuplot's Output (tikz-Terminal)

Unfortunately i don't have much time today. But i would like to point out that it's possible to define reference nodes in gnuplot's coordinate system which can later be referred to from your latex document.

The feature is poorly documented in the official reference manual. It says:

The string may contain any valid TeX/LaTeX/ConTeXt font commands like e.g. ’\small’. It is passed directly as a node parameter in form of "font={}". This can be ’misused’ to add further code to a node, e.g. ’\small,yshift=1ex’ or ’,yshift=1ex’ are also valid while the latter does notchange the current font settings.

The crucial information is in gnuplot-lua-tikz-common.tex:

to add an empty label with the referenceable name "my node" to the plot, just add the following line to your gnuplot file:

set label "" at 1,1 font ",gp refnode,name=my node"

enter image description here

\documentclass[10pt]{article}

\usepackage[hmargin=3.5cm]{geometry}

\usepackage{tikz}
\usepackage{siunitx}
\usepackage[siunitx]{gnuplottex}
\usepackage{gnuplot-lua-tikz}
\usepackage{lipsum}
\usepackage{xsavebox}

\usetikzlibrary{ducks}

\xsavebox{duck}{%
    \tikz{\draw (0,0) pic[duck/body=black!20!magenta] {duck};}%
}

\begin{document}

\lipsum
\begin{figure}
    \centering
    \begin{tikzpicture}[
        aux line style/.style = {
            ->, thick, shorten >=4pt, >=stealth, 
        },
        circled number style/.style = {
           circle, thick, fill=orange!20, draw=black
        }
    ]
        \begin{gnuplot}[
            terminal=tikz, 
            terminaloptions={
                color size 14,11 nopicenvironment originreset tightboundingbox createstyle
            }
        ]
            set yrange [-2:2]
            set ytics 1.0
            set samples 750
            set zeroaxis
            set grid xtics
            set grid ytics

            f1(x) = 10**-2*sin(2*pi*x/(2))*x**2

            set label "" at 2,1.6 font ",gp refnode, name=annotate"
            set label "" at -9.5,f1(-9.5) font ",gp refnode, name=p1"

            set label "" at 0,-1.5 font ",gp refnode, name=p2"

            set for [j=0:8] label "" at 2*j-8.5,f1(2*j-8.5) font sprintf(",gp refnode, name=n%d", j)
            set for [j=0:8] label "" at 2*j-7.5,f1(2*j-7.5) font sprintf(",gp refnode, name=d%d", j)

            plot [-10:10]   f1(x) lw 4 t ''

        \end{gnuplot}

        \draw (annotate) node[circled number style]{1} edge[aux line style, out=-180, in=90] (p1);

        \begin{scope}[overlay]
            \node[circled number style] (2) at ([shift={(6,-5)}]gp plot 1.south) {2};
            \foreach \i in {0,1,...,8}{
                \draw[aux line style, green!40!black]  (2) edge[in=-90, out=90] (n\i);
                \fill[red, draw=black] (n\i) circle[radius=1mm];
            }
        \end{scope}

        \foreach \i in {0,1,...,8}{
            \node[
                scale=0.4,
                anchor=south, 
                inner sep=-2pt, 
                rotate={30*mod(\i,3)-30}
            ] at (d\i) {\xusebox{duck}}; 
        }

    \end{tikzpicture}\medskip

    \caption{Gnuplot tikz terminal in action}
\end{figure}

\end{document}
user1146332
  • 2,166
  • Thank you very much for your time. I really appreciate it. My reasoning behind using gnuplot is simply performance. Especially when I need to annotate a surface plot with arrows and labels it is really annoying to wait 1 min. and more for a single plot. Gnuplot is several orders of magnitude faster and the quality is good enough for me. – OD IUM Apr 07 '20 at 12:43
  • @ODIUM I Updated my answer! I have discovered how to define nodes in gnuplot to which you can later refer to from your latex document. Would be interesting to know if you can reproduce my example. – user1146332 Apr 07 '20 at 15:48