1

I'd like to decorate path with double line of stars and expect the upper line of stars duplicate the lower ones. But why the stars in the state{initial} part are still random size.

I try to set mininum width, but it gives all same stars.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations,shapes.geometric,decorations.shapes,decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\pgfdeclaredecoration{stars}{initial}{
    \state{initial}[width=15pt ] {
        \pgfmathparse{round(rnd*100)}
        \pgfsetfillcolor{yellow!\pgfmathresult!orange}
        \pgfsetstrokecolor{yellow!\pgfmathresult!red}
        % set node mininum width
        %\pgfmathparse{\pgfkeysvalueof{/pgf/minimum width} }
        %\pgfset{minimum width=\pgfmathresult pt }

        \pgfnode{star}{center}{}{}{\pgfusepath{stroke,fill}} % line lower stars
        \pgftransformshift{\pgfpoint{0pt}{12pt}}  
        \pgfnode{star}{center}{}{}{\pgfusepath{stroke,fill}} % line upper stars
    }
    \state{final} {
        \pgfpathmoveto{\pgfpointdecoratedpathlast}
    }
}
\draw[decorate, decoration={stars}, star point ratio=2,  star points=5, inner sep=0, minimum width=rnd*10pt+2pt] (0,0) -- +(5,0);

\end{tikzpicture}
\end{document}

stars

4
  • Welcome to tex.sx. Jul 30 at 13:52
  • Welcome to TeX.SX! Did you try \pgfmathtruncatemacro{\myresult}{round(rnd*100)} instead of using \pgfmathresult multiple times? Jul 30 at 14:02
  • @JasperHabicht Yeah, it works. \pgfmathtruncatemacro{\myresult}{\pgfkeysvalueof{/pgf/minimum width}} \pgfset{minimum width=\myresult } . Thanks.
    – asepsiswu
    Jul 30 at 15:19
  • It's strange the two \pgfnode has different minimum width without explicitly setting the /pgf/minimum width
    – asepsiswu
    Jul 30 at 15:23

1 Answer 1

1

If you need to access the result of a PGF calculation multiple times, it is better to store it in a macro using \pgfmathtruncatemacro. This is also the right way to store \pgfkeysvalueof{/pgf/minimum width} in a macro.

These two lines are needed to make the upper and the lower stars have the same size:

\pgfmathtruncatemacro{\myresultB}{\pgfkeysvalueof{/pgf/minimum width}}
\pgfset{minimum width=\myresultB}

Since you want to set minumum width to some random value, you need to prevent TikZ to calculate this value more than once. This is done with the two lines of code above where minimum width is internally set to the calculated random value, which will keep this value fixed when it is applied to the two stars.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, decorations.shapes}

\pgfdeclaredecoration{stars}{initial}{
    \state{initial}[width=15pt] {
        \pgfmathtruncatemacro{\myresultA}{round(rnd*100)}
        \pgfsetfillcolor{yellow!\myresultA!orange}
        \pgfsetstrokecolor{yellow!\myresultA!red}
        % set node mininum width
        \pgfmathtruncatemacro{\myresultB}{\pgfkeysvalueof{/pgf/minimum width}}
        \pgfset{minimum width=\myresultB}
        \pgfnode{star}{center}{}{}{\pgfusepath{stroke,fill}} % line lower stars
        \pgftransformshift{\pgfpoint{0pt}{12pt}}  
        \pgfnode{star}{center}{}{}{\pgfusepath{stroke,fill}} % line upper stars
    }
    \state{final} {
        \pgfpathmoveto{\pgfpointdecoratedpathlast}
    }
}

\begin{document}
\begin{tikzpicture}

\draw[decorate, decoration={stars}, star point ratio=2,  star points=5, inner sep=0, minimum width={rnd*10pt+2pt}] (0,0) -- +(5.5,0);

\end{tikzpicture}
\end{document}

enter image description here

11
  • I debug the \pgf\minimum width after the two \pgfnode, they are different.
    – asepsiswu
    Jul 30 at 15:44
  • Sorry, I don't understand what you mean. Is this not what you want to get? Jul 30 at 15:46
  • there are two \pgfnode, every time the \pgf\minimum width of \pgfnode of lower star and upper star are different. That's not what I want, the solution is to explictly set \pgf\minimum width.
    – asepsiswu
    Jul 30 at 15:58
  • Then, don't use minimum width={rnd*10pt+2pt} but instead minimum width=10pt (or another fixed value). Jul 30 at 16:00
  • 1
    when explictly setting the minimum width with \pgfset{minimum width=\myresultB},then both paired stars(upper and lower) get the same minimum width. that fix the problem.
    – asepsiswu
    Jul 30 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.