2

I am working on a way to curtomize @percusse from Simulating hand-drawn lines to make it parametric.

My idea is to have something like:

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

or even better (because penciline is a decoration anyway):

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

How do I manage to pass PGF style arguments to penciline? It seems that jag ration is not handled from here.

My MWE is:

\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}
renard
  • 742
  • 5
  • 15
  • the {} button (or control-l or just indent by 4 spaces) is better than <code> for code sections – David Carlisle Dec 17 '14 at 21:27
  • Can you please stop using this decoration? :P I promised to fix it for arcs but still didn't finalize it, I get embarrassed everytime I see it. – percusse Dec 17 '14 at 21:45
  • Here my question is more about how to use parametrized tikzset. The decoration is just a support. I feel sorry if I embarrassed you. – renard Dec 17 '14 at 21:48
  • @renard No problem at all. It was meant to be a joke but I failed. – percusse Dec 17 '14 at 21:55
  • No offense taken ;-) – renard Dec 17 '14 at 21:56
  • Does you edit answer the question? If so, it would be better to post it as an answer. If not, perhaps you could clarify how it helps and what the remaining problem is? – cfr Dec 17 '14 at 22:16
  • @percusse see http://tex.stackexchange.com/a/218483/27327 – renard Dec 17 '14 at 23:12
  • 1
    @cfr I post the answer as you recommended. – renard Dec 17 '14 at 23:14
  • Haha, nice. But it needs a few tricks in the guts to make it hand-drawn. Right now they are a little too much prosthetic-hand-drawn. I divided the curves into random pieces to perturb but still doesn't look as good as I wished to have as lines. – percusse Dec 17 '14 at 23:18

1 Answers1

4

Finally I got this from Key that takes a list of other keys as argument and sets them

\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);
}
renard
  • 742
  • 5
  • 15