[Tex/LaTex] LaTex code inside gnuplot labels

cross-referencinggnuplotgnuplottex

I would like if could I use LaTex code inside gnuplot labels or even inside graph key, I'm using gnuplottex package and I like to make cross-referencing inside this labels, this is my MWE

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[left=2cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\usepackage{graphicx,gnuplottex,amsmath}

\begin{document}
\begin{equation}
y = x^2 \label{eq:x2}
\end{equation}
\begin{figure}[htp]
    \centering
    \begin{gnuplot}[scale=1.3, terminal=epslatex, terminaloptions=color dashed]
        set samples 50000
        set grid
        unset key
        set xrange [-3:1]
        set yrange [0:20]
        set label "$y=x^2$" at -0.5,1
        set label "\eqref{eq:x2}" at -0.5,3

        p x**2
    \end{gnuplot}
    \caption{\eqref{eq:x2} parabolic curve} \label{fig:FermDistNorm}
\end{figure}
\end{document}

Maybe gnuplottex is not enough to do this. Thank you so much! 🙂

Best Answer

In gnuplot, the backslash is used as an escape character, as it is in LaTeX. So, when latex calls gnuplot, gnuplot gobbles up the backslash, and latex is asked to process the title 'eqref{eq:x2}', which just means that the braces are removed, and you end up with 'eqrefeq:x2' as your label (in a nice font).

The solution is to stop gnuplot from interpreting the backslash as an escape character, which you can do by putting an extra backslash before eqref, or using single, rather than double, quotes. See pages 24-25 of the gnuplot 5 manual for more details.

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[left=2cm,right=2cm,top=1.5cm,bottom=1.5cm]{geometry}

\usepackage{graphicx,gnuplottex,amsmath}

\begin{document}
\begin{equation}                                                                                                                                     
y = x^2 \label{eq:x2}                                                                                                                                
\end{equation}
\begin{figure}[htp]
    \centering
    \begin{gnuplot}[scale=1.3, terminal=epslatex, terminaloptions=color dashed]
        set samples 50000
        set grid
        unset key
        set xrange [-3:1]
        set yrange [0:20]
        set label "$y=x^2$" at -0.5,1
        set label "\\eqref{eq:x2}" at -0.5,3  #or use '\eqref{eq:x2}'

        p x**2
    \end{gnuplot}
    \caption{\eqref{eq:x2} parabolic curve} \label{fig:FermDistNorm}
\end{figure}
\end{document}
Related Question