如何使用 decorations.pathreplacing 附加样式?

如何使用 decorations.pathreplacing 附加样式?
\documentclass[tikz,margin=3pt]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc}
\usepackage{pgf}

\tikzset{alamain/.style={%
    decoration={show path construction,%
    lineto code={
        \draw let
        \p1 = ($(\tikzinputsegmentlast) -
                             (\tikzinputsegmentfirst)$),
        \n1 = {atan2(\y1,\x1)},
        \n2 = {(1.5+#1*abs(rnd))*{1,-1}[random(0,1)]}
        in
        (\tikzinputsegmentfirst) 
        to [out=\n1 + \n2 , in=\n1 + 180 + \n2]
        (\tikzinputsegmentlast) ;
        }
    },decorate
    },
alamain/.default=2
}

\begin{document}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{tikzpicture}
\draw [help lines] grid (3,2);

% here a try to append the style
\tikzset{alamain/.append style = {blue} }

\foreach \i in {0,...,10} {%
    \draw [alamain] (0,.2*\i) -- (3,.2*\i) ;
}

\end{tikzpicture}

\end{document}

答案1

假设您希望lineto代码产生蓝线,那么我认为解决方案是在代码lineto中的路径中添加样式(或任何其他合适的名称)lineto

\documentclass[tikz,margin=3pt]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc}
\usepackage{pgf}

\tikzset{alamain/.style={%
    decoration={show path construction,%
    lineto code={
        \draw let
        \p1 = ($(\tikzinputsegmentlast) -
                             (\tikzinputsegmentfirst)$),
        \n1 = {atan2(\y1,\x1)},
        \n2 = {(1.5+#1*abs(rnd))*{1,-1}[random(0,1)]}
        in [lineto/.try]
        (\tikzinputsegmentfirst) 
        to [out=\n1 + \n2 , in=\n1 + 180 + \n2]
        (\tikzinputsegmentlast) ;
        }
    },decorate
    },
alamain/.default=2
}

\begin{document}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{tikzpicture}
\draw [help lines] grid (3,2);

% here a try to append the style
\tikzset{alamain/.append style = {lineto/.style={blue}} }

\foreach \i in {0,...,10} {%
    \draw [alamain] (0,.2*\i) -- (3,.2*\i) ;
}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容