![如何使用 pgfplots 生成二维坐标系图像](https://linux22.com/image/447884/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20pgfplots%20%E7%94%9F%E6%88%90%E4%BA%8C%E7%BB%B4%E5%9D%90%E6%A0%87%E7%B3%BB%E5%9B%BE%E5%83%8F.png)
我尝试运行 pgfplots 给出的示例:
\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
% We will externalize the figures
\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = \(x\),
ylabel = {\(f(x)\)},
]
%Below the red parabola is defined
\addplot [
domain=-10:10,
samples=100,
color=red,
]
{x^2 - 2*x - 1};
\addlegendentry{\(x^2 - 2x - 1\)}
%Here the blue parabola is defined
\addplot [
domain=-10:10,
samples=100,
color=blue,
]
{x^2 + 2*x + 1};
\addlegendentry{\(x^2 + 2x + 1\)}
\end{axis}
\end{tikzpicture}
\end{document}
我使用xelatex来编译:
xelatex ./demo.tex
但是 tikz 给了我这个错误信息:
(/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd)
*geometry* driver: auto-detecting
*geometry* detected driver: xetex
Package pgfplots notification 'compat/show suggested version=true': you might b
enefit from \pgfplotsset{compat=1.18} (current compat level: 1.9).
===== 'mode=convert with system call': Invoking 'xelatex -halt-on-error -intera
ction=batchmode -jobname "demo-figure0" "\def\tikzexternalrealjob{demo}\input{d
emo}"' ========
! Package tikz Error: Sorry, the system call 'xelatex -halt-on-error -interacti
on=batchmode -jobname "demo-figure0" "\def\tikzexternalrealjob{demo}\input{demo
}"' did NOT result in a usable output file 'demo-figure0' (expected one of .pdf
:.jpg:.jpeg:.png:.bmp:). Please verify that you have enabled system calls. For
pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is also named 'write 1
8' or something like that. Or maybe the command simply failed? Error messages c
an be found in 'demo-figure0.log'. If you continue now, I'll try to typeset the
picture.
See the tikz package documentation for explanation.
Type H <return> for immediate help.
...
l.36 \end{tikzpicture}
这是为什么?我该如何处理?
答案1
如果您想要做的只是在文档中生成一个图表,请删除以下行:
\usepgfplotslibrary{external}
\tikzexternalize
如果您想要外部化 tikz 绘图,则需要进行 shell escape。如果您不知道这意味着什么,只需删除 \tikzexternalize,您的文档就可以正常工作(尽管编译速度会稍慢)。
如果你想启用 shell 转义,请参阅我如何启用 shell-escape?