TikZ 退出并显示“不知道密钥 '/tikz/restrict y to domain”

TikZ 退出并显示“不知道密钥 '/tikz/restrict y to domain”

当我尝试编译时,出现以下错误。

Runaway argument?
samples=60, domain=0:10, xmax=10.5, restrict y to domain=0:10, axis l\ETC.
! Paragraph ended before \pgfplots@@environment@axis was complete.
<to be read again> 
                   \par 

使用输入

\begin{tikzpicture}
\begin{axis}[samples=60,
    domain=0:10, xmax=10.5,
    restrict y to domain=0:10,
    axis lines=left,
    y=0.5cm/3,
    x=0.5cm,
    grid=both,
    xtick={0,...,10},
    ytick={0,3,...,9},
    compat=newest,
    xlabel=$x$, xlabel style={at={(1,0)}, anchor=west},
    ylabel=$y$, ylabel style={rotate=-90,at={(0,1)}, anchor=south}

]
\addplot [red] {x};
\addplot [black] {x^2};
\end{axis}
\end{tikzpicture}

不起作用。错误是

! Package pgfkeys Error: I do not know the key '/tikz/restrict y to domain' and
 I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.32 \end{axis}

?

与 MWE

\documentclass{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{epsf,graphicx,tikz,graphics}
\usepackage{pgfplots}
\usepackage{url}
\begin{document}
\title{}
\author{}
\maketitle
\begin{tikzpicture}
\begin{axis}[samples=60,
    domain=0:10, xmax=10.5,
    restrict y to domain=0:10,
    axis lines=left,
    y=0.5cm/3,
    x=0.5cm,
    grid=both,
    xtick={0,...,10},
    ytick={0,3,...,9},
    compat=newest,
    xlabel=$x$, xlabel style={at={(1,0)}, anchor=west},
    ylabel=$y$, ylabel style={rotate=-90,at={(0,1)}, anchor=south}
]
\addplot [red] {x};
\addplot [black] {x^2};
\end{axis}

\end{tikzpicture}

\end{document}

答案1

选项末尾有一个空行axis,删除它就可以正常工作。

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=60,
    domain=0:10, xmax=10.5,
    restrict y to domain=0:10,
    axis lines=left,
    y=0.5cm/3,
    x=0.5cm,
    grid=both,
    xtick={0,...,10},
    ytick={0,3,...,9},
    compat=newest,
    xlabel=$x$, xlabel style={at={(1,0)}, anchor=west},
    ylabel=$y$, ylabel style={rotate=-90,at={(0,1)}, anchor=south}
]
\addplot [red] {x};
\addplot [black] {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

答案2

正如 Torbjørn T 的回答中评论的那样,问题在于空白行。删除空白行是简单的修复方法,但可以说底层键值解析器应该容忍逗号分隔列表中的空格。在加载包后将以下内容添加到序言中可以解决这个问题,至少对于它没有隐藏在其他宏中的简单情况而言是这样。

\makeatletter
\let\zz\pgfplots@environment@axis
\def\pgfplots@environment@axis{\endlinechar` \zz}
\makeatother

答案3

只需在 LaTeXit 中加载代码它就可以工作......

在此处输入图片描述

也许你的序言中遗漏了一些东西。否则你就没有跟上时代。

相关内容