Nister
Nister

Reputation: 226

Gnuplot tikz terminal, how to add commands to the picture?

I have some gnuplot code that makes a graph using the tikz terminal. However, I want to add to the generated .tex file some tikz code.

As a MWE, consider the following gnuplot code:

set term tikz standalone
set output "out.tex"
plot sin(x)

Let's say I want to draw a circle on the plot generated, which I can do using \draw (2,2) circle [radius = 2cm]. Adding this line to the .tex file generated yields the following code:

\documentclass[10pt]{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}

\usepackage[utf8x]{inputenc}

\usepackage{gnuplot-lua-tikz}
\pagestyle{empty}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{\gpbboxborder}
\begin{document}

\begin{tikzpicture}[gnuplot]


%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
%!! ADDED BY HAND
\draw (2,2) circle [radius = 2cm];
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


%% generated with GNUPLOT 5.0p3 (Lua 5.1; terminal rev. 99, script rev. 100)
...

Is there any way to tell gnuplot that I want some tikz code on the picture, so it outputs a .tex file similar to the one I made? Reading the docs I've found that with this terminal you can set a preamble but you can't include tikz code there. Another option I was considering was using set print output_file and then print the code I want, however, either that code gets printed first in the file and gnuplot overwrites it or printing it after gnuplot has printed the output overwrites it.

Upvotes: 1

Views: 1780

Answers (1)

maij
maij

Reputation: 4218

Maybe this comes close:

set label 1 "\\tikz{\\draw [green] (2,2) circle [radius = 2cm];}" at screen 0.0, screen 0.0

set label 2 "by gnuplot" textcolor rgb "#00FF00" at 0, -0.6
set label 3 "manual" textcolor rgb "#0000FF" at 0, -0.5

set term tikz standalone
set output "out.tex"
plot sin(x)

There is a small offset compared to the circle generated by your handmade draw command, but it might be close enough.

Please note that this approach does not directly place a draw command in the generated tex file, it puts the draw command into a node. I think this causes the offset. Gnuplot generates this:

\node[gp node left] at (0.000,0.000) {\tikz{\draw [green] (2,2) circle [radius = 2cm];}};

enter image description here

Don't forget to escape the backslashes. The option screen after set label at tells gnuplot to place the label relative to the screen coordinate system, have a look at help coordinates from gnuplot commandline.

Upvotes: 1

Related Questions