tikz:设置弹簧

tikz:设置弹簧

我正在尝试设置一个 spring 命令但一直收到消息:

! Package pgfkeys Error: I do not know the key
  '/pgf/decoration/\pgfkeyscurrentname ' and I am going to ignore it. Perhaps
  you misspelled it.

我的代码是:

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations}

\begin{document}
\begin{tikzpicture}[decoration = coil,
  spring/.style = {
    decoration = {
      pre length = #1,
      post length = #1}
  }]
  \fill[pattern = north east lines] (-2cm, 0) coordinate (A) rectangle
  (2cm, 0.25cm);
  \draw (A) -- (2cm, 0);
  \draw[decorate, spring = 0.15cm] (-1cm, 0) -- (-1cm, -1cm);
\end{tikzpicture}
\end{document}

所有内容的拼写都正确,所以我不确定问题是什么。

答案1

你需要\usetikzlibrary{decorations.pathmorphing}

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[decoration = coil,
  spring/.style = {
    decoration = {
      pre length = #1,
      post length = #1}
  }]
  \fill[pattern = north east lines] (-2cm, 0) coordinate (A) rectangle
  (2cm, 0.25cm);
  \draw (A) -- (2cm, 0);
  \draw[decorate, spring = 0.15cm] (-1cm, 0) -- (-1cm, -1cm);
\end{tikzpicture}
\end{document}

相关内容