# tikz/gnuplot legend inside the caption

I want to add my legend inside the caption, similarly the one given in this question. But I want to use gnuplot environment as in the following MWE. Unfortunately, I am getting '??' because \label{test} is not working. Is it possible to do that using 'gnuplot' environment? Or one given by @pluton, is this the only way?

My MWE which I am working on it:

\documentclass[12pt,a4paper,english]{article}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{gnuplot-lua-tikz}
\usepackage[shell]{gnuplottex}

\begin{document}
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\begin{tikzpicture}
\draw [draw=none,use as bounding box] (0cm,0cm) rectangle (14cm,8cm);{
\begin{gnuplot}[terminal=tikz,terminaloptions={size 14cm, 8cm}]
plot sin(x) w lines t 'sin(x)',\
cos(x) w lines t 'cos(x)'
\label{test}  %gives '??' on the pdf, not working
\end{gnuplot}
};
%\addlegendentry{Legend for only desired lines} % Did not work
\node[draw=none, text width=1cm] at (-5.0,1.0) {$sin(x)$}; %this has no sample
\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: \ref{test}}
\end{figure}
\end{document}


Thanks for the comments, I am able get what I want.

MVE2 (It creates a legend in \caption)

\documentclass[12pt,a4paper,english]{article}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{gnuplot-lua-tikz}
\usepackage[shell]{gnuplottex}

\begin{document}
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\begin{tikzpicture}
\draw [draw=none,use as bounding box] (0cm,0cm) rectangle (14cm,8cm);{
\begin{gnuplot}[terminal=tikz,terminaloptions={size 14cm, 8cm}]
set style line 1  lc rgb "black"  lt 1 lw 1 pointinterval 1 pt 1
set style line 11 lc rgb "black"  lt 1 lw 5 pointinterval 5 pt 12
plot sin(x) w lines ls 1 t 'sin(x)',\
cos(x) w lines ls 11 t 'cos(x)'
\end{gnuplot}
};
\node[draw=none, text width=1cm] at (-5.0,1.0) {$sin(x)$}; %this has no sample
\end{tikzpicture}
\caption[Caption in ToC]{
\begin{tikzpicture}[baseline=-.5ex,line join=round,line cap=round,color=black]
%First legend
\pgfsetlinewidth{1\gpbaselw}
\node[draw=none, text width=1cm] at (0.0,0.0) {$sin(x)$}; %this has no sample
\draw (0.75,0) -- (1.10,0);
%Creat following legend
\pgfsetlinewidth{5\gpbaselw}
\node[draw=none, text width=1cm] at (1.8,0.0) {$cos(x)$}; %this has no sample
\draw (2.5,0) -- (2.85,0);
\end{tikzpicture}
}
\end{figure}
\end{document}


Aim of the work

• Have you run it twice? The first run always gives ?? – John Kormylo Sep 17 '14 at 12:43
• You can always use something along the lines of: \begin{tikzpicture}[baseline=-.5ex,line join=round,line cap=round,color=gnu2]\pgfsetlinewidth{2\gpbaselw}\draw (0,0) -- (0.35,0);\end{tikzpicture}) in the caption. Note that gnu2 is a custom defined colour (of course you can also choose one of the predefined colors) and the number before \gpbaselw should match the linewidth (lw) used in gnuplot. I used this method for one plot in my masters thesis, so this is quite feasible I'd guess. – John Sep 17 '14 at 13:13
• Also, all \label and \ref do is handle counters, specifically those incremented using \refstepcounter. I'm not sure what the heck the linked MWE was trying to accomplish, but they aren't going to help you put a legend into the caption. That's what the saveboxes were for. Besides, there are at least 2 fatal problems with the location of \label. – John Kormylo Sep 17 '14 at 14:29
• @JohnKormylo 1. I compile with latexmk -f -pdf -pdflatex="pdflatex -interactive=nonstopmode -shell-escape" -use-make myDoc.tex So, yes I run it twice. 2. The position of \label is not indicating anything. It is trying put legend inside the caption. I will have look saveboxes, I have no idea about them for now. @John 1. I used your advice, it is working, thanks. I can not really say that it is a clear solution, but it works nicely :) I will keep improving my Q/A if I/Someone will come up with better idea. Still, maybe the best is just using \addplot as in the linked question. – trblnc Sep 17 '14 at 15:10
• @trblnc_dsg Note that you can improve the numbers on your plot by adding \usepackage{siunitx} to the preamble and change \usepackage[shell]{gnuplottex} to \usepackage[shell,siunitx]{gnuplottex}. Also it is preferable to use $\sin(x)$ instead of $sin(x)$ (the same is true for cos(x) as well). – John Sep 17 '14 at 15:25