8

Here's a 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}

For a more complex picture, I wanted to apply the same decoration to all lines and nodes, and so I used "every path":

\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}

However, this results in an error: "I cannot decorate an empty path". I think I understand what the problem is: \node ... is replaced with \path node ... and we thus have an empty path.

But how can I achieve what I want, namely applying the same decoration to all lines AND nodes (let's assume there a lots of them) without having to specify it explicitly for each \draw and \node command?

7

This problem has been solved some time ago by Jake, I think, in this stellar answer. It is sort of the opposite of what you suggest: if you use \path (<coordinates>) node..., it works.

\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}

enter image description here

EDIT: The picture looks a bit as if the left edge of the node was straight. However, that's just an accident, as can be seen from the following animation in which the random seed varies.

\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}

enter image description here

EDIT: And if you don't want to write draw,decorate over and over, you could just append this to the node styles.

\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}
  • Thanks! Who would have thought? But shouldn't this then be considered kind of a bug in TikZ? In both cases there is an empty path, but only in case do we get an error message... BTW, I looked at Jake's answer and found it very interesting, but I couldn't find any reference to this problem here. – Frunobulax Aug 13 '18 at 20:39
  • 1
    @Frunobulax Jake didn't mention the problem but the way he wrote his code suggests that he encountered and just solved it. I prefer to give credit to somebody in case of doubt, and I would hesitate to call it a bug. TikZ is IMHO a really amazing tool that allows us to do many wonderful things, and in this case it just requires you to slightly change the syntax. Similar things happen here. That is, sometimes commands that appear to be equivalent are not. – user121799 Aug 13 '18 at 20:50
5

apparently every path doesn't work as you expected. you still need to explicit say, which lines, shapes has decorate 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}

so, it maybe more handy (for shorter writing) to define path style for example as

DP/.style={%decorated path
           decorate, decoration=random steps}

and than use as follows:

\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}

enter image description here

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.