fillbetween 给出未定义序列错误

fillbetween 给出未定义序列错误

我希望在阶跃函数和曲线之间创建一个阴影区域。以下代码给出了下图。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{fillbetween}
\begin{document}
\newcommand\myN{5}
\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    legend style={at={(1,1)},anchor=north east,fill=none},
    xlabel={$t$},
    ylabel={$y$},
    xmin=0,
    xmax={\the\numexpr\myN+2},
    ymin=0,
    ymax=1.3,
    xtick=\empty,
    ytick=\empty,
    extra x ticks={1, \myN},
    extra x tick labels={$1$, $n$},
    extra y ticks={1},
    title={sum of hatched region converges to Euler's constant}
]
\addplot[name path=A,domain=1:\myN+1,samples=501,smooth] {1/x};
\addlegendentry{$y = \frac{1}{t}$};
\addplot[name path=B,domain=1:\myN+1,samples=\myN+1,const plot,dashed] {1/x};
\addlegendentry{$y = \frac{1}{\lfloor t \rfloor}$};
% \addplot fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}

tikz 图

我尝试在曲线(路径 A)和阶跃函数(路径 B)之间引入阴影

\addplot fill between [of=A and B];

但系统抱怨“未定义的控制序列”。

VS 代码错误

答案1

使用\usepgfplotslibrary{fillbetween}而不是\usetikzlibrary{fillbetween}

完成 MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\begin{document}
\newcommand\myN{5}
\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    legend style={at={(1,1)},anchor=north east,fill=none},
    xlabel={$t$},
    ylabel={$y$},
    xmin=0,
    xmax={\the\numexpr\myN+2},
    ymin=0,
    ymax=1.3,
    xtick=\empty,
    ytick=\empty,
    extra x ticks={1, \myN},
    extra x tick labels={$1$, $n$},
    extra y ticks={1},
    title={sum of hatched region converges to Euler's constant}
]
\addplot[name path=A,domain=1:\myN+1,samples=501,smooth] {1/x};
\addlegendentry{$y = \frac{1}{t}$};
\addplot[name path=B,domain=1:\myN+1,samples=\myN+1,const plot,dashed] {1/x};
\addlegendentry{$y = \frac{1}{\lfloor t \rfloor}$};
\addplot fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}

生成的 PDF

相关内容