[Tex/LaTex] How to get gnuplot error messages displayed when using gnuplottex

errorsgnuplotpdftextexmaker

I use gnuplottex in LaTeX documents all the time, as I enjoy having the gnuplot scripts directly embedded in the LaTeX document. The main problem with gnuplottex is however, that the gnuplot error messages are hidden in the compilation and you only see an empty figure, without knowing why.

Here is a minimum example:

\documentclass{article}
\usepackage{gnuplottex}

\begin{document}
\begin{figure}[h]
\begin{gnuplot}
set terminal cairolatex
plot [-100:100] tan(sin(x)) 
\end{gnuplot}
\caption{Working Figure}
\end{figure}

\begin{figure}[h]
\begin{gnuplot}
set terminal cairolatex
plot [-100a:100] tan(sin(x)) 
\end{gnuplot}
\caption{Broken Figure}
\end{figure}

Some sample text
\end{document}

The figure "working figure" is functional, while the "broken figure" has an "a" added to its range plot [-100a:100] tan(sin(x)).

If you execute the broken figure code in gnuplot, you get an error message:

gnuplot> plot [-100a:100] tan(sin(x))
                   ^
         ':' or keyword 'to' expected

However, when using Texmaker on TexLive or MikTex, you don't see the gnuplot error messages. Even in the log nothing is apparent.

However, when executing PDFLatex in Texmaker with -shell-escape, you briefly see the gnuplot error message, as gnuplot prints the error message in the shell.

Now to the actual question:
Is there a way to get this shell error message into the LaTeX log, so that Texmaker will display this in the "view log" window?
(View log is the window, where all LaTeX warnings and errors are listed. So if the gnuplot error output is redirected to the LaTeX log in the format of LaTeX errors it should automatically appear correctly)

This would greatly help using gnuplottex and simplify debugging of complex gnuplot scripts, as we can then use the hints from the generally helpful error messages from gnuplot.

Thanks for your help.

Best Answer

Here's a possibility for a Unix systems, with error redirection:

\documentclass{article}
\usepackage{gnuplottex}
\usepackage{etoolbox,catchfile}

\patchcmd{\gnuplotgraphicsprocess}
 {\gnuplotexe\space \subfolder\figname.gnuplot}
 {\gnuplotexe\space \subfolder\figname.gnuplot\space 2>>\jobname.gnuploterrors}
 {}{}
\AtBeginDocument{\immediate\write18{rm -f \jobname.gnuploterrors}}

\makeatletter
\long\def\gnuploterrors@eatpar#1#2\@nil{\def\gnuploterrors@{#2}}
\AtEndDocument{%
  \CatchFileDef\gnuploterrors@{\jobname.gnuploterrors}{\endlinechar=`^^J \catcode`\ =12 }%
  \expandafter\gnuploterrors@eatpar\gnuploterrors@\@nil
  \ifx\gnuploterrors@\@empty\else
    \PackageWarningNoLine{gnuplottex}{There were gnuplot errors^^J%
      *************^^J%
      \detokenize\expandafter{\gnuploterrors@}%
      *************^^J}%
  \fi
}
\makeatother

\begin{document}

\begin{figure}[h]
\begin{gnuplot}
set terminal cairolatex
plot [-100:100] tan(sin(x)) 
\end{gnuplot}
\caption{Working Figure}
\end{figure}

\begin{figure}[h]
\begin{gnuplot}
set terminal cairolatex
plot [-100a:100] tan(sin(x)) 
\end{gnuplot}
\caption{Broken Figure}
\end{figure}

Some sample text
\end{document}

This adds to the command line calling gnuplot the redirection of STDERR to a file \jobname.gnuploterrors. This file, removed at the start of the job, if already existing, is loaded at end document; if it contains just an empty line, no error has been found; otherwise, it is printed on the terminal and the log file in the form of a warning that Texmaker should be able to see.

In the case of your example file, that I saved as santen.tex, I get the warning

Package gnuplottex Warning: There were gnuplot errors
*************
plot [-100a:100] tan(sin(x))
          ^
"santen-gnuplottex-fig2.gnuplot", line 4: ':' or keyword 'to' expected

*************

If I move \end{document} before the second figure, there's no warning. I tested it also with two correct figures.