Graphics, Figures & Tablesgnuplottex | New Environment for Gnuplot Script

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
AahzChi
Posts: 3
Joined: Fri Aug 10, 2012 3:14 pm

gnuplottex | New Environment for Gnuplot Script

Post by AahzChi »

I try to create a new environment for gnuplot, where the figure-command and captions and so on are included.

Code: Select all

\newenvironment{gnu}[1]{
  \begin{figure}[!ht] 
  \caption{#1}
  \begin{gnuplot}[terminal=epslatex,terminaloptions=color solid]
}{
  \end{gnuplot}
  \end{figure}
}


\begin{gnu}{1}
  plot cos(x)
\end{gnu}
But this is not working. It does not recognize the \end{gnuplot}. If I write it explicitly in the command, it works:

Code: Select all

\newenvironment{gnu}[1]{
  \begin{figure}[!ht] 
  \caption{#1}
  \begin{gnuplot}[terminal=epslatex,terminaloptions=color solid]
}{
  \end{figure}
}


\begin{gnu}{1}
  plot cos(x)
  \end{gnuplot}
\end{gnu}
Is there any workaround?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

gnuplottex | New Environment for Gnuplot Script

Post by Stefan Kottwitz »

Hi AahzChi,

welcome to the board!

Perhaps post a complete example, so it can be tested. Perhaps try a different syntax, \gnuplot and \endgnuplot:

Code: Select all

\newenvironment{gnu}[1]{
  \begin{figure}[!ht]
  \caption{#1}
  \gnuplot[terminal=epslatex,terminaloptions=color solid]
}{
  \endgnuplot
  \end{figure}
}
Stefan
LaTeX.org admin
AahzChi
Posts: 3
Joined: Fri Aug 10, 2012 3:14 pm

gnuplottex | New Environment for Gnuplot Script

Post by AahzChi »

Hi,
thanks for the reply and the welcome!
Here is the complete example:

Code: Select all

\documentclass{article}

\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{gnuplottex}

\begin{document}

\newenvironment{gnu}[1]{
  \begin{figure}[!ht] 
  \caption{#1}
  \begin{gnuplot}[terminal=epslatex,terminaloptions=color solid]
}{
  \end{gnuplot}
  \end{figure}
}

\begin{gnu}{caption}
  plot cos(x)
\end{gnu}

\end{document}
If I take \begin/\end{gnuplot} out of the newenvironment, it works.
Your suggestion with \gnuplot/\endgnuplot does not work for me.

I have heard about commands that expand an argument in a certain order. Could this be useful here?
AahzChi
Posts: 3
Joined: Fri Aug 10, 2012 3:14 pm

Re: gnuplottex | New Environment for Gnuplot Script

Post by AahzChi »

No ideas anybody?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

gnuplottex | New Environment for Gnuplot Script

Post by Stefan Kottwitz »

How about using pgfplots? This is a very capable modern package, which works with LaTeX in dvi mode as well as with pdfLaTeX, XeLaTeX, LuaLaTeX, ConTeXt, and plain TeX, without need to call an external program such as gnuplot.

Stefan
LaTeX.org admin
lechi_jb
Posts: 3
Joined: Sat May 11, 2013 1:05 pm

Re: gnuplottex | New Environment for Gnuplot Script

Post by lechi_jb »

Hey AahzChi!

Did you find a solution for this problem?

Christian
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

gnuplottex | New Environment for Gnuplot Script

Post by cgnieder »

The {gnuplot} environment is no usual environment but similar to a {verbatim} environment. It needs to grab the environment body verbatim and write it to a file which then is processed by gnuplot. The best way around is to copy the definition of the {gnuplot} environment (it is not very long) and modify it to give the wanted result:

Code: Select all

\documentclass{article}

\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{gnuplottex}

\newenvironment{gnu}[1]{%
  \def\gnufigurecaption{#1}%
  \stepcounter{fignum}%
  \def\gnuplotterminal{latex}%
  \def\gnuplotterminaloptions{}%
  \def\gnuplotscale{1}%
  \setkeys{pic}{terminal=epslatex,terminaloptions=color solid}%
  \xdef\gnuplotCutFile{\figname.gnuplot}%
  \gnuplotverbatimwrite{\gnuplotCutFile}%
}{%
  \endgnuplotverbatimwrite
  \gnuplotgraphicsprocess
  \begin{figure}[!ht]
  \centering
  \expandafter\caption\expandafter{\gnufigurecaption}%
  \gnuplotgraphicsinclude
  \end{figure}
}

\begin{document}

\begin{gnu}{this is the caption}
  plot cos(x)
\end{gnu}

\end{document}
Regards
site moderator & package author
lechi_jb
Posts: 3
Joined: Sat May 11, 2013 1:05 pm

gnuplottex | New Environment for Gnuplot Script

Post by lechi_jb »

Thank you very much!!
:)

It works perfekt!

Here is my code:

Code: Select all

\newenvironment{plot}[2]
{
	\stepcounter{fignum}
	\def\gnuplotterminal{latex}
	\def\gnuplotterminaloptions{}
	\def\gnuplotscale{#2}
	\def\gnuplotcaption{#1}
	\setkeys{pic}{terminal=epslatex,terminaloptions=color}
	\xdef\gnuplotCutFile{\figname.gnuplot}
	\gnuplotverbatimwrite{\gnuplotCutFile}
}
{
	\begin{figure}[htbp]
	\begin{center}
	\endgnuplotverbatimwrite
	\gnuplotgraphicsprocess
	\gnuplotgraphicsinclude
	\caption{\gnuplotcaption}
	\end{center}
	\end{figure}
}




\begin{plot}{Test2}{0.5}
	set multiplot
	set key outside;
	f(x)=sin(x);
	plot f(x);
	unset multiplot
\end{plot}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

gnuplottex | New Environment for Gnuplot Script

Post by cgnieder »

lechi_jb wrote:

Code: Select all

{
	\begin{figure}[htbp]
	\begin{center}
	\endgnuplotverbatimwrite
	\gnuplotgraphicsprocess
	\gnuplotgraphicsinclude
	\caption{\gnuplotcaption}
	\end{center}
	\end{figure}
}
You should change this into

Code: Select all

{
	\endgnuplotverbatimwrite
	\gnuplotgraphicsprocess
	\begin{figure}[htbp]
	\centering
	\gnuplotgraphicsinclude
        \expandafter\caption\expandafter{\gnuplotcaption}
	\end{figure}
}
(1) You don't want the \begin{figure} before \endgnuplotverbatimwrite. I'm surprised this isn't giving you errors?
(2) The {center} environment adds additional vertical space which I believe is unwanted. Using \centering suffices and is what's commonly recommended.
(3) It's probably unnecessary but just to be on the safe side I would expand \gnuplotcaption once before feeding it to \caption{}.

Regards
site moderator & package author
lechi_jb
Posts: 3
Joined: Sat May 11, 2013 1:05 pm

Re: gnuplottex | New Environment for Gnuplot Script

Post by lechi_jb »

Ok!

I'm new on latex and so its important for me to get useful tips and tricks :idea:

So thank you again!
Post Reply