帶參數的參數化 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);
}

相關內容