Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
5  
...talk about pathmorphing a rectangle... into a bird's nest! tikz-tastic! Remove the snake, which bird's don't enjoy anyway, in order to fix the problem. :) –  Werner Dec 21 '11 at 8:04
3  
"I see it, but I don't believe it.". So I ran the code myself and can confirm that that genuinely is what one obtains. Totally bizarre. If this is a bug, I sincerely hope that they never fix it as that is a fantastic picture. –  Andrew Stacey Dec 21 '11 at 8:06
    
I think we should call it a feature. Amazing! –  qubyte Dec 21 '11 at 8:29
2  
Hahah, amazing indeed. Here is a microprocessor : \begin{tikzpicture}[scale=1] \draw[rounded corners=1cm, decorate,decoration={snake,amplitude=5mm,segment length=3}] (0,0) rectangle (5cm,5cm); \end{tikzpicture}. Crazy stuff –  percusse Dec 21 '11 at 8:40
add comment

1 Answer

up vote 20 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.