# rnd minimum width of node in decoration does not have same size in repeated code

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}


• 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. Jul 30 at 15:19
• It's strange the two \pgfnode has different minimum width without explicitly setting the /pgf/minimum width Jul 30 at 15:23

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}


• I debug the \pgf\minimum width after the two \pgfnode, they are different. 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. 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
• 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. Jul 30 at 16:38