如何在使用 gnuplottex 时显示 gnuplot 错误消息?

如何在使用 gnuplottex 时显示 gnuplot 错误消息?

我经常在 LaTeX 文档中使用 gnuplottex,因为我喜欢将 gnuplot 脚本直接嵌入到 LaTeX 文档中。但是,gnuplottex 的主要问题是,gnuplot 错误消息隐藏在编译中,您只能看到一个空图,而不知道为什么。

这是一个最小的例子:

\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}

“工作图形”图形具有功能性,而“损坏图形”在其范围中添加了“a” plot [-100a:100] tan(sin(x))

如果你在 gnuplot 中执行损坏的图形代码,你会收到一条错误消息:

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

但是,在 TexLive 或 MikTex 上使用 Texmaker 时,您看不到 gnuplot 错误消息。即使在日志中也看不到任何内容。

但是,当使用 Texmaker 执行 PDFLatex 时-shell-escape,您会短暂地看到 gnuplot 错误消息,因为 gnuplot 会在 shell 中打印该错误消息。

现在来谈谈实际问题:
有没有办法将这个 shell 错误消息放入 LaTeX 日志中,以便 Texmaker 在“查看日志”窗口中显示它?
(查看日志是列出所有 LaTeX 警告和错误的窗口。因此,如果 gnuplot 错误输出以 LaTeX 错误的格式重定向到 LaTeX 日志,它应该会自动正确显示)

这将极大地帮助使用 gnuplottex 并简化复杂 gnuplot 脚本的调试,因为我们可以使用来自 gnuplot 的通常有用的错误消息的提示。

感谢您的帮助。

答案1

对于 Unix 系统来说,存在错误重定向的可能性:

\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}

这会在命令行中添加对文件gnuplot的重定向调用。此文件在作业开始时被删除(如果已经存在),则会在结束文档中加载;如果它只包含一个空行,则表示未发现任何错误;否则,它会以 Texmaker 应该能够看到的警告形式打印在终端和日志文件上。STDERR\jobname.gnuploterrors

就我保存为的示例文件而言santen.tex,我收到警告

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

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

如果我\end{document}在第二个数字之前移动,则不会有任何警告。我也用两个正确的数字进行了测试。

相关内容