![装饰时,“每条路径”样式会影响 \node“路径”](https://linux22.com/image/380866/%E8%A3%85%E9%A5%B0%E6%97%B6%EF%BC%8C%E2%80%9C%E6%AF%8F%E6%9D%A1%E8%B7%AF%E5%BE%84%E2%80%9D%E6%A0%B7%E5%BC%8F%E4%BC%9A%E5%BD%B1%E5%93%8D%20%5Cnode%E2%80%9C%E8%B7%AF%E5%BE%84%E2%80%9D.png)
以下是 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\draw[decorate, decoration=random steps] (0,0) -- (10,0);
\node[decorate, decoration=random steps, draw, fill=yellow, inner sep=0.5cm] at (5,-2) {};
\end{tikzpicture}
\end{document}
对于更复杂的图片,我想对所有线条和节点应用相同的装饰,因此我使用了“每条路径”:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[every path/.style={decorate, decoration=random steps}]
\draw (0,0) -- (10,0);
\node[draw, fill=yellow, inner sep=0.5cm] at (5,-2) {};
\end{tikzpicture}
\end{document}
但是,这会导致错误:“我无法修饰空路径”。我想我明白问题是什么了:\node ...
被替换为\path node ...
,因此我们有一个空路径。
但是我怎样才能实现我想要的,即将相同的装饰应用于所有线和节点(假设有很多),而不必为每个命令明确指定\draw
它\node
?
答案1
我认为,这个问题不久前已经被 Jake 解决了,出色的答案。这和您所建议的正好相反:如果您使用\path (<coordinates>) node...
,它就会起作用。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[every path/.style={decorate,decoration=random steps}]
\draw (0,0) -- (10,0);
\path (5,-2) node[decorate,draw, fill=yellow, inner sep=0.5cm] {};
\end{tikzpicture}
\end{document}
编辑:图片看起来有点像节点的左边缘是直的。然而,这只是一个意外,从下面的动画中可以看出,随机种子是变化的。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\foreach \X in {1,...,50}
{\begin{tikzpicture}[every path/.style={decorate,decoration=random steps}]
\pgfmathsetseed{\X}
\draw (0,0) -- (10,0);
\path (5,-2) node[decorate,draw, fill=yellow, inner sep=0.5cm] {};
\end{tikzpicture}}
\end{document}
编辑:如果您不想draw,decorate
一遍又一遍地写,您可以将其附加到节点样式中。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[every path/.style={decorate,decoration=random steps},
every node/.append style={draw,decorate}]
\draw (0,0) -- (10,0);
\path (5,-2) node[fill=yellow, inner sep=0.5cm] {};
\end{tikzpicture}
\end{document}
答案2
显然every path
没有按你预期的那样工作。你仍然需要明确地说出哪些线条、形状具有装饰路径:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[every path/.style={decoration=random steps}]
\draw[decorate] (0,0) -- (10,0);
\node[decorate, draw, fill=yellow, inner sep=0.5cm] at (5,-2) {};
\end{tikzpicture}
\end{document}
因此,定义路径样式可能更方便(为了写得更短),例如
DP/.style={%decorated path
decorate, decoration=random steps}
然后使用如下方法:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[DP/.style={%decorated path
decorate, decoration=random steps}]
\draw[DP] (0,0) -- (10,0);
\node[DP, draw, fill=yellow, inner sep=0.5cm] at (5,-2) {};
\end{tikzpicture}
\end{document}