[Tex/LaTex] How to draw a decorated rectangle with rounded corners

tikz-pgf

As the title says, I was trying to draw a rectangle with rounded corners, and then decided it needed decorating. Well, the result looks awesome, but isn't really what I had in mind.
a) What happened?
b) How do I do it properly?

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing} 
\begin{document}
\begin{tikzpicture}[scale=2]
    \draw[rounded corners=40mm,
           decorate,decoration={snake,amplitude=5,segment length=30}]
    (0,0) rectangle (5,5);
\end{tikzpicture}
\end{document}

enter image description here

Best Answer

The problem is that the rounded corners option is getting applied to the result of the decoration. So you need to turn it off after the initial path is constructed but before the decoration is applied. This cannot be done in the decoration key because the options for that are actually processed when this key is originally parsed (ie at the start of the path). So putting /tikz/sharp corners in the decoration simply negates the rounded corners.

Decorating the path is the very next thing done after the path has been processed so it's hard to stick something in between the path being constructed and being decorated! However, it is possible and I'll present two ways. The first works by splitting the construction into two pieces. First we construct the rounded rectangle. We do not draw it, but we save it. Then we decorate it in a completely different command. This separates the construction from the decoration and means that the scope of the rounded corners option is over by the time we get to the decoration.

The second way works because TikZ (or PGF) constructs a path as it reads it, rather than saving everything for the end. This means that it is possible to insert options in the middle of the path and have them take effect from that point on (this only applies to options that affect how the path is constructed, not how it is rendered (so colour doesn't work)). We basically want to turn off rounded corners at the end of the path so we do precisely that: at the end of the path we put [sharp corners]. Then by the time the decoration is processed, we no longer round corners.

The second method is simpler, but the first is more powerful. Indeed, it's the idea behind my spath library (still in alpha - indeed, looking at the code for this answer showed that that library lacks a few essential pieces): namely that a path can be defined and used in two different places.

With all that, here's the code (note that the \makeatletter ... \makeatother piece is only needed for the first of the two solutions):

\documentclass[a4paper]{article}
%\url{http://tex.stackexchange.com/q/38989/86}
\usepackage{tikz}
\makeatletter
\tikzset{
  use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother
\usetikzlibrary{decorations.pathmorphing} 
\begin{document}
\begin{tikzpicture}
 % First method: define and then use
 \path[rounded corners=30mm,save path=\rectpath]
    (0,0) rectangle (8,8);
 \draw[use path=\rectpath,
    decorate,decoration={snake,amplitude=5,segment length=5}] (0,0);
 % Second solution: sharp corners at the end
 \draw[rounded corners=30mm,
    decorate,decoration={snake,amplitude=5,segment length=5}]
    (0,9) rectangle +(8,8) [sharp corners];
\end{tikzpicture}
\end{document}

Here's the result:

rounded corners with decoration

As I said in my comment, I hope that even if they fix this then they provide a way to turn it back on as the original picture - whilst not what you want - is quite fun! I especially like the fact that the snake decoration turns into a birds nest.