# How to get gnuplot error messages displayed when using gnuplottex?

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.

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.

• Thank you, this looks like a nice solution! The log file looks awesome and exactly as needed, however Texmaker seems to ignore leading whitespaces in the "view log window", which removes the indication of position with the ^ character and shifts it to the beginning of the line. Any ideas on how to fix that? I've searched through the settings of Texmaker and have not found anything. Another (tiny) issue is that in the log, everything is crammed into a single line without any whitespaces in between. If multiline is not an option, then some spacing would be nice. Nevertheless terrific work! – Victor van Santen Nov 15 '15 at 23:16
• @VictorvanSanten Sorry, but I don't use Texmaker (I find it clumsy). In the summary the warning text is on a single line, but when the log is shown it appears on multiple lines; I don't know why the program takes the liberty of removing leading blank spaces; the “real” log file has the ^ at the right position. I tried on TeXStudio and it's shown correctly. – egreg Nov 15 '15 at 23:26
• @egreg Thanks for this -- I've integrated it into gnuplottex version 0.9, which should be available shortly. – Lars Kotthoff Dec 9 '15 at 0:56