# LaTex variable inside gnuplot code

I just started using gnuplot code inside my latex document and I just ran into a problem I could not solve after some googling (hit me if I did not google hard enough). I use the gnuplottex package.

The thing I want to do is multiple plots in my latex document with gnuplot. As some plots treat of related data, I want to use the same colors/linestyle for related data (in this case color). As I am lazy, I just want to declare once the color I want to use in hexacode in LaTex and then use it in every gnuplot command.

For instance:

\documentclass[12pt, a4]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[url=false, doi=false, isbn=false, sorting=none,style=numeric,maxbibnames=3,maxcitenames=3,backend=bibtex]{biblatex}
\usepackage{gnuplottex}
\newcommand{\myredcolor}{\#FF6666}
\begin{document}


And then :

 \begin{gnuplot}
set terminal epslatex
plot   "mydata"  title "\\myredcolor" with linespoints linestyle 1 lt rgb "\\myredcolor" lw 3
\end{gnuplot}


Thing is, obviously, it does not work. In this case, if the plot would appear (ie when putting "red" instead of "\myredcolor" for instance), the title of my plot would correctly be "#FF6666", meaning that I managed to pass my variable gnuplot for the "title" value but not for the rgb value, as the plot won't appear if I leave it like this.

Is there any way to solve this, or will I have to hardcode the hexa value in each of my plot command for this related data ?

Just to try, I also checked for linestyle (in case something was amiss with the # sign) and this wouldn't work either :

\newcommand{\mylinestyle}{1}


And then :

 \begin{gnuplot}
set terminal epslatex
plot   "mydata"  title "MyData \\mylinestyle" with linespoints linestyle \\mylinestyle lt rgb "red" lw 3
\end{gnuplot}
\end{document}


Thanks a lot

• I would define the colours and styles at the gnuplot end without injecting TeX macros. I would create a style common to all plots with settings like encoding, datafile separator, etc. and the line styles. Include this file at the beginning of every plot and you have all definitions in one place. – Marco May 24 '13 at 10:30

\documentclass{article}
\usepackage{gnuplottex}
\usepackage{letltxmacro}

\LetLtxMacro{\thegnuplot}{\gnuplot}         % save original gnuplot env
\LetLtxMacro{\endthegnuplot}{\endgnuplot}   %

\def\linecolor#1{%
\renewenvironment{gnuplot}[1][]
{\thegnuplot[##1] s="#1";}          % gnuplot string variable "s" gets the color value
{\endthegnuplot}
}

\begin{document}

\linecolor{\#FF0000}

\begin{gnuplot}[scale=0.5]
set terminal epslatex
plot sin(x)  title '\'.s with linespoints linestyle 1 lt rgb s lw 10
\end{gnuplot}

\linecolor{\#008800}

\begin{gnuplot}[scale=0.5]
set terminal epslatex
plot cos(x)*exp(-0.1*x)  title '\'.s with linespoints linestyle 1 lt rgb s lw 10
\end{gnuplot}

\end{document}

• It worked thanks a lot ! Here is the final command I used to pass multiple variables : \def\linecolor#1#2{% \renewenvironment{gnuplot}[2][] {\thegnuplot[##1] s="#1"; g="#2";} {\endthegnuplot} } – Cashalow May 27 '13 at 8:53
• Nice, I was just about to edit the answer, when you got it. Just one remark: don't confuse parameters of the macro \linecolor (now two, #1#2) with parameters of the gnuplot environment (still one, optional), so it should be the same as before \renewenvironment{gnuplot}[1][]. – g.kov May 27 '13 at 9:15