等高线图的问题

等高线图的问题

我试图运行以下代码

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width = 7cm, compat = 1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain = -2:2, domain y = 0:2*pi]
\addplot3[contour gnuplot = {output point meta = rawz, number=10, labels=false}, samples = 41, z filter/.code=\def\pgfmathresult{-1.6}] {exp(-x^2) * sin(deg(y))};
\addplot3[surf, samples = 25] {exp(-x^2) * sin(deg(y))};
\end{axis}
\end{tikzpicture}
\end{document}

我得到了错误

Package pgfplots Error: sorry, plot file{Utitlled_contourmp0.table} could not be opened

通过快速搜索,我发现等高线图需要 pgfplots 和 gnuplot 相互通信。理论上,让它们交互的方法是添加

!TEX option = --enable-write18

或者

%!TEX option = --enable-escape

在文件顶部。不幸的是,这对我没有用。有什么建议吗?

答案1

在测试了数十个类似问题并尝试开发一个“简单”的诊断 MWE 之后。

消息非常清楚 [原文如此]Utitlled_contourtmp0.table没有生成因此不可用,这似乎是轮廓 gnuplot 的一个非常常见的问题,并且两个解决方案相对简单。

1) -shell-execute 需要处于活动状态

2) \yourpathto\gnuplot\bin 需要位于编辑器路径上

以下测试脚本应该可以帮助 Windows 用户随意复制 mac/nix

\documentclass[border=10pt]{standalone}
\RequirePackage{ifplatform}
\usepackage{ifpdf}
%% First sanity test if pdfLaTeX is active
\ifpdf
  \errmessage{OK ! pdfLaTeX IS active PRESS ENTER TO CONTINUE}
\else
  \errmessage{plain latex and dvipdf ? PLEASE SWITCH to pdfLaTeX}
\fi
%% Second sanity test if shell escape is available
\ifshellescape
    \errmessage{OK   Shell-Escape IS active  PRESS ENTER TO CONTINUE}
\else
    \errmessage{Shell-Escape is NOT working. PLEASE ADD --shell-escape to pdfLaTeX arguments}
\fi
\begin{document}
%% if shell escape is working we can get console feedback
\def\tmpfile{w18-active-\the\year\the\month\the\day\the\time.cmd}
\immediate\write18{echo echo Shell-Escape is active > "\tmpfile"}
\ifpdf
  \immediate\write18{echo echo You are using pdflatex >> "\tmpfile"}
\else
  \immediate\write18{echo echo plain latex and dvipdf ? switch to pdflatex >> "\tmpfile"}
\fi
% Check where gnuplot is Fist check if we can get version info
\immediate\write18{echo gnuplot -e  "show version long" >> "\tmpfile"}
% these lines are under review as it should traverse editor roots, but for present we check relative and system path
\immediate\write18{echo where /r . gnuplot.exe >> "\tmpfile"}
\immediate\write18{echo where gnuplot.exe >> "\tmpfile"}

\immediate\write18{echo set path >> "\tmpfile"}
% Store latest results WILL be overwritten on each run
\immediate\write18{"\tmpfile" >results.txt}
% Add a pause in case user wants to manually edit / run cmd file
\immediate\write18{echo pause>> "\tmpfile"}
\immediate\write18{"\tmpfile"}
\immediate\write18{del "\tmpfile"}
ALL DONE SEE CONSOLE OUTPUT OR Results.txt
\end{document}

答案2

-output-dir当我设置使用 进行编译的参数时,也发生了同样的问题pdflatex

因此,我有以下结构:

.
    builds/
    texs/
        file_to_compile.tex

我想编译我的.tex并将其放入builds。在 root 中,我尝试执行此操作:
pdflatex -shell-escape -output-dir=builds texs/file_to_compile.tex
并收到此奇怪plot file could not be opened错误。没有-output-dir参数,它直接编译到 root 中,这对我来说是不必要的行为。

所以我的解决方案是:

  1. 进入texs/
  2. 执行pdflatex -shell-escape -output-dir="../builds" file_to_compile.tex

(我遇到了新问题,因为这是我尝试使用 Python 自动执行的操作,并且我不想每次编译时都更改当前目录,但那是另一回事。)

相关内容