带参数的参数化 tikzset 样式

带参数的参数化 tikzset 样式

我正在研究一种方法来定制@percusse模拟手绘线条使其参数化。

我的想法是这样的:

  \draw[penciline={jag ratio=2},decorate,thick] (-0.4cm,-0.8cm) rectangle (1.2,-2);

甚至更好(因为penciline无论如何都是装饰):

\draw[penciline={jag ratio=2},thick] (-0.4cm,-0.8cm) rectangle (1.2,-2);

我该如何将 PGF 样式的参数传递给penciline? 似乎jag ration这里无法处理。

我的 MWE 是:

\documentclass[border=1cm]{standalone}

\RequirePackage{tikz}
\usetikzlibrary{calc,patterns,decorations,plotmarks}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}


\pgfdeclaredecoration{penciline}{initial}{
  \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,
    auto corner on length=1mm,
  ]{
    \pgfpathcurveto%
        {% 1st control point
          \pgfpoint
              {\pgfdecoratedinputsegmentremainingdistance*rnd*1pt}
              {\pgfkeysvalueof{/tikz/penciline/jag ratio}*rand*\pgfdecorationsegmentamplitude}
        }
        {%% 2nd control point
          \pgfpoint
          %% Make sure random number is always between origin and target points
              {(.5pt+0.25*rand)*\pgfdecoratedinputsegmentremainingdistance}
              {\pgfkeysvalueof{/tikz/penciline/jag ratio}*rand*\pgfdecorationsegmentamplitude}
        }
        {% 2nd point (1st one is implicit)
          \pgfpointadd
              {\pgfpointdecoratedinputsegmentlast}
              {\pgfpoint{rand*1pt}{rand*1pt}}
        }
  }
  \state{final}{}
}

\tikzset{
  penciline/.code={\pgfqkeys{/tikz/penciline}{#1}},
  penciline={
    jag ratio/.initial=5,
  },
  penciline/.style = {
    decorate,
    decoration={penciline},
  },
}


\def\Grid{
  \draw[penciline,decorate,style=help lines] (-2,-2) grid[step=1cm] (4,4);
}

\begin{tikzpicture}
  \Grid{}
  \draw[penciline,decorate,thick] (-0.4cm,-0.8cm) rectangle (1.2,-2);


\end{tikzpicture}


\end{document}

答案1

最后我得到了这个该键将其他键的列表作为参数并设置它们

\tikzset{
  penciline/.code={\pgfqkeys{/tikz/penciline}{#1}},
  penciline={
    jag ratio/.initial=5,
  },
  penciline/.style = {
    decorate,
    decoration={penciline},
    penciline/.cd,
    #1,
    /tikz/.cd
  },
  decorate
}


\def\Grid{
  \draw[penciline={jag ratio=1},style=help lines] (-2,-2) grid[step=1cm] (4,4);
}

相关内容