| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Decorated Paths} |
| 12 |
\label{section-tikz-decorations} |
| 13 |
|
| 14 |
\subsection{Overview} |
| 15 |
|
| 16 |
Decorations are a general concept to make (sub)paths ``more interesting''. |
| 17 |
Before we have a look at the details, let us have a look at some examples: |
| 18 |
% |
| 19 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 20 |
decorations.pathmorphing, |
| 21 |
decorations.pathreplacing, |
| 22 |
decorations.shapes, |
| 23 |
}}] |
| 24 |
\begin{tikzpicture}[thick] |
| 25 |
\draw (0,3) -- (3,3); |
| 26 |
\draw[decorate,decoration=zigzag] (0,2.5) -- (3,2.5); |
| 27 |
\draw[decorate,decoration=brace] (0,2) -- (3,2); |
| 28 |
\draw[decorate,decoration=triangles] (0,1.5) -- (3,1.5); |
| 29 |
\draw[decorate,decoration={coil,segment length=4pt}] (0,1) -- (3,1); |
| 30 |
\draw[decorate,decoration={coil,aspect=0}] (0,.5) -- (3,.5); |
| 31 |
\draw[decorate,decoration={expanding waves,angle=7}] (0,0) -- (3,0); |
| 32 |
\end{tikzpicture} |
| 33 |
\end{codeexample} |
| 34 |
|
| 35 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 36 |
\begin{tikzpicture} |
| 37 |
\node [fill=red!20,draw,decorate,decoration={bumps,mirror}, |
| 38 |
minimum height=1cm] |
| 39 |
{Bumpy}; |
| 40 |
\end{tikzpicture} |
| 41 |
\end{codeexample} |
| 42 |
|
| 43 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 44 |
\begin{tikzpicture} |
| 45 |
\filldraw[fill=blue!20] (0,3) |
| 46 |
decorate [decoration=saw] { -- (3,3) } |
| 47 |
decorate [decoration={coil,aspect=0}] { -- (2,1) } |
| 48 |
decorate [decoration=bumps] { -| (0,3) }; |
| 49 |
\end{tikzpicture} |
| 50 |
\end{codeexample} |
| 51 |
|
| 52 |
\begin{codeexample}[pre={\pgfmathsetseed{1}},preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 53 |
\begin{tikzpicture} |
| 54 |
\node [fill=yellow!50,draw,thick, minimum height=2cm, minimum width=3cm, |
| 55 |
decorate, decoration={random steps,segment length=3pt,amplitude=1pt}] |
| 56 |
{Saved from trash}; |
| 57 |
\end{tikzpicture} |
| 58 |
\end{codeexample} |
| 59 |
|
| 60 |
The general idea of decorations is the following: First, you construct a path |
| 61 |
using the usual path construction commands. The resulting path is, in essence, |
| 62 |
a series of straight and curved lines. Instead of directly using this path for |
| 63 |
filling or drawing, you can then specify that it should form the basis for a |
| 64 |
decoration. In this case, depending on which decoration you use, a new path is |
| 65 |
constructed ``along'' the path you specified. For instance, with the |zigzag| |
| 66 |
decoration, the new path is a zigzagging line that goes along the old path. |
| 67 |
|
| 68 |
Let us have a look at an example: In the first picture, we see a path that |
| 69 |
consists of a line, an arc, and a line. In the second picture, this path has |
| 70 |
been used as the basis of a decoration. |
| 71 |
% |
| 72 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 73 |
\tikz \fill |
| 74 |
[fill=blue!20,draw=blue,thick] (0,0) -- (2,1) arc (90:-90:.5) -- cycle; |
| 75 |
\end{codeexample} |
| 76 |
% |
| 77 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 78 |
\tikz \fill [decorate,decoration={zigzag}] |
| 79 |
[fill=blue!20,draw=blue,thick] (0,0) -- (2,1) arc (90:-90:.5) -- cycle; |
| 80 |
\end{codeexample} |
| 81 |
|
| 82 |
It is also possible to decorate only a subpath (the exact syntax will be |
| 83 |
explained later in this section). |
| 84 |
% |
| 85 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 86 |
\tikz \fill [decoration={zigzag}] |
| 87 |
[fill=blue!20,draw=blue,thick] (0,0) -- (2,1) |
| 88 |
decorate { arc (90:-90:.5) } -- cycle; |
| 89 |
\end{codeexample} |
| 90 |
|
| 91 |
The |zigzag| decoration will be called a \emph{path morphing} decoration |
| 92 |
because it morphs a path into a different, but topologically equivalent path. |
| 93 |
Not all decorations are path morphing; rather there are three kinds of |
| 94 |
decorations. |
| 95 |
|
| 96 |
\begin{enumerate} |
| 97 |
\item The just-mentioned \emph{path morphing} decorations morph the path in |
| 98 |
the sense that what used to be a straight line might afterwards be a |
| 99 |
squiggly line or might have bumps. However, a line is still and a line |
| 100 |
and path deforming decorations do not change the number of subpaths. |
| 101 |
|
| 102 |
Examples of such decorations are the |snake| or the |zigzag| |
| 103 |
decoration. Many such decorations are defined in the library |
| 104 |
|decorations.pathmorphing|. |
| 105 |
|
| 106 |
\item \emph{Path replacing} decorations completely replace the path by a |
| 107 |
different path that is only ``loosely based'' on the original path. For |
| 108 |
instance, the |crosses| decoration replaces a path by a path consisting |
| 109 |
of a sequence of crosses. Note how in the following example filling the |
| 110 |
path has no effect since the path consist only of (numerous) |
| 111 |
unconnected straight line subpaths: |
| 112 |
% |
| 113 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.shapes}}] |
| 114 |
\tikz \fill [decorate,decoration={crosses}] |
| 115 |
[fill=blue!20,draw=blue,thick] (0,0) -- (2,1) arc (90:-90:.5) -- cycle; |
| 116 |
\end{codeexample} |
| 117 |
|
| 118 |
Examples of path replacing decorations are |crosses| or |ticks| or |
| 119 |
|shape backgrounds|. Such decorations are defined in the library |
| 120 |
|decorations.pathreplacing|, but also in |decorations.shapes|. |
| 121 |
\item \emph{Path removing} decorations completely remove the |
| 122 |
to-be-decorated path. Thus, they have no effect on the main path that |
| 123 |
is being constructed. Instead, they typically have numerous \emph{side |
| 124 |
effects}. For instance, they might ``write some text'' along the |
| 125 |
(removed) path or they might place nodes along this path. Note that for |
| 126 |
such decorations the path usage command for the main path have no |
| 127 |
influence on how the decoration looks like. |
| 128 |
% |
| 129 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.text}}] |
| 130 |
\tikz \fill [decorate,decoration={text along path, |
| 131 |
text=This is a text along a path. Note how the path is lost.}] |
| 132 |
[fill=blue!20,draw=blue,thick] (0,0) -- (2,1) arc (90:-90:.5) -- cycle; |
| 133 |
\end{codeexample} |
| 134 |
% |
| 135 |
\end{enumerate} |
| 136 |
|
| 137 |
Decorations are defined in different decoration libraries, see |
| 138 |
Section~\ref{section-library-decorations} for details. It is also possible to |
| 139 |
define your own decorations, see Section~\ref{section-base-decorations}, but |
| 140 |
you need to use the \pgfname\ basic layer and a bit of theory is involved. |
| 141 |
|
| 142 |
Decorations can be used to decorate already decorated paths. In the following |
| 143 |
three graphics, we start with a simple path, then decorate it once, and then |
| 144 |
decorate the decorated path once more. |
| 145 |
% |
| 146 |
\begin{codeexample}[] |
| 147 |
\tikz \fill [fill=blue!20,draw=blue,thick] |
| 148 |
(0,0) rectangle (3,2); |
| 149 |
\end{codeexample} |
| 150 |
% |
| 151 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 152 |
\tikz \fill [fill=blue!20,draw=blue,thick] |
| 153 |
decorate[decoration={zigzag,segment length=10mm,amplitude=2.5mm}] |
| 154 |
{ (0,0) rectangle (3,2) }; |
| 155 |
\end{codeexample} |
| 156 |
% |
| 157 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 158 |
decorations.pathmorphing, |
| 159 |
decorations.shapes, |
| 160 |
}}] |
| 161 |
\tikz \fill [fill=blue!20,draw=blue,thick] |
| 162 |
decorate[decoration={crosses,segment length=2mm}] { |
| 163 |
decorate[decoration={zigzag,segment length=10mm,amplitude=2.5mm}] { |
| 164 |
(0,0) rectangle (3,2) |
| 165 |
} |
| 166 |
}; |
| 167 |
\end{codeexample} |
| 168 |
|
| 169 |
One final word of warning: Decorations can be pretty slow to typeset and they |
| 170 |
can be inaccurate. The reason is that \pgfname\ has to do a \emph{lot} of rather |
| 171 |
difficult computations in the background and \TeX\ is not very good at doing |
| 172 |
math. Decorations are fastest when applied to straight line segments, but even |
| 173 |
then they are much slower than other alternatives. For instance, the |ticks| |
| 174 |
decoration can be simulated by clever use of a dashing pattern and the dashing |
| 175 |
pattern will literally be thousands of times faster to typeset. However, for |
| 176 |
most decorations there are no real alternatives. |
| 177 |
|
| 178 |
\begin{tikzlibrary}{decorations} |
| 179 |
In order to use decorations, you first have to load a |decorations| library. |
| 180 |
This |decorations| library defines the basic options described in the |
| 181 |
following, but it does not define any new decorations. This is done by |
| 182 |
libraries like |decorations.text|. Since these more specialized libraries |
| 183 |
include the |decorations| library automatically, you usually do not have to |
| 184 |
bother about it. |
| 185 |
\end{tikzlibrary} |
| 186 |
|
| 187 |
|
| 188 |
\subsection{Decorating a Subpath Using the Decorate Path Command} |
| 189 |
|
| 190 |
The most general way to decorate a (sub)path is the following path command. |
| 191 |
|
| 192 |
\begin{pathoperation}{decorate}{\opt{\oarg{options}}\marg{subpath}} |
| 193 |
This path operation causes the \meta{subpath} to be decorated using the |
| 194 |
current decoration. Depending on the decoration, this may or may not extend |
| 195 |
the current path. |
| 196 |
% |
| 197 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 198 |
\begin{tikzpicture} |
| 199 |
\draw [help lines] grid (3,2); |
| 200 |
\draw decorate [decoration={name=zigzag}] |
| 201 |
{ (0,0) .. controls (0,2) and (3,0) .. (3,2) |- (0,0) }; |
| 202 |
\end{tikzpicture} |
| 203 |
\end{codeexample} |
| 204 |
% |
| 205 |
The path can include straight lines, curves, rectangles, arcs, circles, |
| 206 |
ellipses, and even already decorated paths (that is, you can nest |
| 207 |
applications of the |decorate| path command, see below). |
| 208 |
|
| 209 |
Due to the limits on the precision in \TeX, some inaccuracies in |
| 210 |
positioning when crossing input segment boundaries may occasionally be |
| 211 |
found. |
| 212 |
|
| 213 |
You can use nodes normally inside the \meta{subpath}. |
| 214 |
% |
| 215 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 216 |
decorations.pathmorphing, |
| 217 |
decorations.shapes, |
| 218 |
}}] |
| 219 |
\begin{tikzpicture} |
| 220 |
\draw [help lines] grid (3,2); |
| 221 |
\draw decorate [decoration={name=zigzag}] |
| 222 |
{ (0,0) -- (2,2) node (hi) [left,draw=red] {Hi!} arc(90:0:1)}; |
| 223 |
|
| 224 |
\draw [blue] decorate [decoration={crosses}] {(3,0) -- (hi)}; |
| 225 |
\end{tikzpicture} |
| 226 |
\end{codeexample} |
| 227 |
|
| 228 |
The following key is used to select the decoration and also to select |
| 229 |
further ``rendering options'' for the decoration. |
| 230 |
|
| 231 |
\begin{key}{/pgf/decoration=\meta{decoration options}} |
| 232 |
\keyalias{tikz} |
| 233 |
This option is used to specify which decoration is used and how it will |
| 234 |
look like. Note that this key will \emph{not} cause any decorations to |
| 235 |
be applied, immediately. It takes the |decorate| path command or the |
| 236 |
|decorate| option to actually decorate a path. The |decoration| option |
| 237 |
is only used to specify which decoration should be used, in principle. |
| 238 |
You can also use this option at the beginning of a picture or a scope |
| 239 |
to specify the decoration to be used with each invocation of the |
| 240 |
|decorate| path command. Naturally, any local options of the |decorate| |
| 241 |
path command override these ``global'' options. |
| 242 |
% |
| 243 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 244 |
decorations.pathmorphing, |
| 245 |
decorations.shapes, |
| 246 |
}}] |
| 247 |
\begin{tikzpicture}[decoration=zigzag] |
| 248 |
\draw decorate {(0,0) -- (3,2)}; |
| 249 |
\draw [red] decorate [decoration=crosses] {(0,2) -- (3,0)}; |
| 250 |
\end{tikzpicture} |
| 251 |
\end{codeexample} |
| 252 |
|
| 253 |
The \meta{decoration options} are special options (which have the path |
| 254 |
prefix |/pgf/decoration/|) that determine the properties of the |
| 255 |
decoration. Which options are appropriate for a decoration strongly |
| 256 |
depend on the decoration, you will have to look up the appropriate |
| 257 |
options in the documentation of the decoration, see |
| 258 |
Section~\ref{section-library-decorations}. |
| 259 |
|
| 260 |
There is one option (available only in \tikzname) that is special: |
| 261 |
% |
| 262 |
\begin{key}{/pgf/decoration/name=\meta{name} (initially none)} |
| 263 |
Use this key to set which decoration is to be used. The \meta{name} |
| 264 |
can both be a decoration or a meta-decoration (you need to worry |
| 265 |
about the difference only if you wish to define your own |
| 266 |
decorations). |
| 267 |
|
| 268 |
If you set \meta{name} to |none|, no decorations are added. |
| 269 |
% |
| 270 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 271 |
\begin{tikzpicture} |
| 272 |
\draw [help lines] grid (3,2); |
| 273 |
\draw decorate [decoration={name=zigzag}] |
| 274 |
{ (0,0) .. controls (0,2) and (3,0) .. (3,2) }; |
| 275 |
\end{tikzpicture} |
| 276 |
\end{codeexample} |
| 277 |
% |
| 278 |
Since this option is used so often, you can also leave out the |
| 279 |
|name=| part. Thus, the above example can be rewritten more |
| 280 |
succinctly: |
| 281 |
% |
| 282 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 283 |
\begin{tikzpicture} |
| 284 |
\draw [help lines] grid (3,2); |
| 285 |
\draw decorate [decoration=zigzag] |
| 286 |
{ (0,0) .. controls (0,2) and (3,0) .. (3,2) }; |
| 287 |
\end{tikzpicture} |
| 288 |
\end{codeexample} |
| 289 |
% |
| 290 |
In general, when \meta{decoration options} are parsed, for each |
| 291 |
unknown key it is checked whether that key happens to be a |
| 292 |
(meta-)decoration and, if so, the |name| option is executed for |
| 293 |
this key. |
| 294 |
\end{key} |
| 295 |
|
| 296 |
Further options allow you to adjust the position of decorations |
| 297 |
relative to the to-be-decorated path. See |
| 298 |
Section~\ref{section-decorations-adjust} below for details. |
| 299 |
\end{key} |
| 300 |
|
| 301 |
Recall that some decorations actually completely remove the to-be-decorated |
| 302 |
path. In such cases, the construction of the main path is resumed after the |
| 303 |
|decorate| path command ends. |
| 304 |
% |
| 305 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.text}}] |
| 306 |
\begin{tikzpicture}[decoration={text along path,text= |
| 307 |
around and around and around and around we go}] |
| 308 |
|
| 309 |
\draw (0,0) -- (1,1) decorate { -- (2,1) } -- (3,0); |
| 310 |
\end{tikzpicture} |
| 311 |
\end{codeexample} |
| 312 |
|
| 313 |
It is permissible to nest |decorate| commands. In this case, the path |
| 314 |
resulting from the first decoration process is used as the to-be-decorated |
| 315 |
path for the second decoration process. This is especially useful for |
| 316 |
drawing fractals. The |Koch snowflake| decoration replaces a straight line |
| 317 |
like \tikz\draw (0,0) -- (1,0); by |
| 318 |
\tikz[decoration=Koch snowflake] \draw decorate{(0,0) -- (1,0)};. |
| 319 |
Repeatedly applying this transformation to a triangle yields a fractal that |
| 320 |
looks a bit like a snowflake, hence the name. |
| 321 |
% |
| 322 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.fractals}}] |
| 323 |
\begin{tikzpicture}[decoration=Koch snowflake,draw=blue,fill=blue!20,thick] |
| 324 |
\filldraw (0,0) -- ++(60:1) -- ++(-60:1) -- cycle ; |
| 325 |
\filldraw decorate{ (0,-1) -- ++(60:1) -- ++(-60:1) -- cycle }; |
| 326 |
\filldraw decorate{ decorate{ (0,-2.5) -- ++(60:1) -- ++(-60:1) -- cycle }}; |
| 327 |
\end{tikzpicture} |
| 328 |
\end{codeexample} |
| 329 |
% |
| 330 |
\end{pathoperation} |
| 331 |
|
| 332 |
|
| 333 |
\subsection{Decorating a Complete Path} |
| 334 |
|
| 335 |
You may sometimes wish to decorate a path over whose construction you have no |
| 336 |
control. For instance, the path of the background of a node is created without |
| 337 |
having a chance to issue a |decorate| path command. In such cases you can use |
| 338 |
the following option, which allows you to decorate a path ``after the fact''. |
| 339 |
|
| 340 |
\begin{key}{/tikz/decorate=\opt{\meta{boolean}} (default true)} |
| 341 |
When this key is set, the whole path is decorated after it has been |
| 342 |
finished. The decoration used for decorating the path is set via the |
| 343 |
|decoration| way, in exactly the same way as for the |decorate| path |
| 344 |
command. Indeed, the following two commands have the same effect: |
| 345 |
% |
| 346 |
\begin{enumerate} |
| 347 |
\item |\path decorate[|\meta{options}|] {|\meta{path}|};| |
| 348 |
\item |\path [decorate,|\meta{options}|] |\meta{path}|;| |
| 349 |
\end{enumerate} |
| 350 |
% |
| 351 |
The main use or the |decorate| option is the you can also use it with the |
| 352 |
nodes. It then causes the background path of the node to be decorated. Note |
| 353 |
that you can decorate a background path only once in this manner. That is, |
| 354 |
in contrast to the |decorate| path command you cannot apply this option |
| 355 |
twice (this would just set it to |true|, once more). |
| 356 |
% |
| 357 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 358 |
decorations.pathmorphing, |
| 359 |
decorations.text, |
| 360 |
shapes.geometric, |
| 361 |
}}] |
| 362 |
\begin{tikzpicture}[decoration=zigzag] |
| 363 |
\draw [help lines] (0,0) grid (3,5); |
| 364 |
|
| 365 |
\draw [fill=blue!20,decorate] (1.5,4) circle (1cm); |
| 366 |
|
| 367 |
\node at (1.5,2.5) [fill=red!20,decorate,ellipse] {Ellipse}; |
| 368 |
|
| 369 |
\node at (1.5,1) [inner sep=6mm,fill=red!20,decorate,ellipse,decoration= |
| 370 |
{text along path,text={This is getting silly}}] {Ellipse}; |
| 371 |
\end{tikzpicture} |
| 372 |
\end{codeexample} |
| 373 |
|
| 374 |
In the last example, the |text along path| decoration removes the path. In |
| 375 |
such cases it is useful to use a pre- or postaction to cause the decoration |
| 376 |
to be applied only before or after the main path has been used. |
| 377 |
Incidentally, this is another application of the |decorate| option that you |
| 378 |
cannot achieve with the decorate path command. |
| 379 |
% |
| 380 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 381 |
decorations.pathmorphing, |
| 382 |
decorations.text, |
| 383 |
shapes.geometric, |
| 384 |
}}] |
| 385 |
\begin{tikzpicture}[decoration=zigzag] |
| 386 |
\node at (1.5,1) [inner sep=6mm,fill=red!20,ellipse, |
| 387 |
postaction={decorate,decoration= |
| 388 |
{text along path,text={This is getting silly}}}] {Ellipse}; |
| 389 |
\end{tikzpicture} |
| 390 |
\end{codeexample} |
| 391 |
% |
| 392 |
Here is more useful example, where a postaction is used to add the path |
| 393 |
after the main path has been drawn. |
| 394 |
% |
| 395 |
% \catcode`\|12 % !? |
| 396 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.text}}] |
| 397 |
\begin{tikzpicture} |
| 398 |
\draw [help lines] grid (3,2); |
| 399 |
\fill [draw=red,fill=red!20, |
| 400 |
postaction={decorate,decoration={raise=2pt,text along path, |
| 401 |
text=around and around and around and around we go}}] |
| 402 |
(0,1) arc (180:-180:1.5cm and 1cm); |
| 403 |
\end{tikzpicture} |
| 404 |
\end{codeexample} |
| 405 |
% |
| 406 |
\end{key} |
| 407 |
|
| 408 |
|
| 409 |
\subsection{Adjusting Decorations} |
| 410 |
\label{section-decorations-adjust} |
| 411 |
|
| 412 |
\subsubsection{Positioning Decorations Relative to the To-Be-Decorate Path} |
| 413 |
|
| 414 |
The following option, which are only available with \tikzname, allow you to |
| 415 |
modify the positioning of decorations relative to the to-be-decorated path. |
| 416 |
|
| 417 |
\begin{key}{/pgf/decoration/raise=\meta{dimension} (initially 0pt)} |
| 418 |
The segments of the decoration are raised by \meta{dimension} relative to |
| 419 |
the to-be-decorated path. More precisely, the segments of the path are |
| 420 |
offset by this much ``to the left'' of the path as we travel along the |
| 421 |
path. This raising is done after and in addition to any transformations set |
| 422 |
using the |transform| option (see below). |
| 423 |
|
| 424 |
A negative \meta{dimension} will offset the decoration ``to the right'' of |
| 425 |
the to-be-decorated path. |
| 426 |
% |
| 427 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.shapes}}] |
| 428 |
\begin{tikzpicture} |
| 429 |
\draw [help lines] (0,0) grid (3,2); |
| 430 |
|
| 431 |
\draw (0,0) -- (1,1) arc (90:0:2 and 1); |
| 432 |
\draw decorate [decoration=crosses] |
| 433 |
{ (0,0) -- (1,1) arc (90:0:2 and 1) }; |
| 434 |
\draw[red] decorate [decoration={crosses,raise=5pt}] |
| 435 |
{ (0,0) -- (1,1) arc (90:0:2 and 1) }; |
| 436 |
\end{tikzpicture} |
| 437 |
\end{codeexample} |
| 438 |
% |
| 439 |
\end{key} |
| 440 |
|
| 441 |
\begin{key}{/pgf/decoration/mirror=\opt{\meta{boolean}}} |
| 442 |
Causes the segments of the decoration to be mirrored along the |
| 443 |
to-be-decorated path. This is done after and in addition to any |
| 444 |
transformations set using the |transform| and/or |raise| options. |
| 445 |
% |
| 446 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathreplacing}}] |
| 447 |
\begin{tikzpicture} |
| 448 |
\node (a) {A}; |
| 449 |
\node (b) at (2,1) {B}; |
| 450 |
\draw (a) -- (b); |
| 451 |
\draw[decorate,decoration=brace] (a) -- (b); |
| 452 |
\draw[decorate,decoration={brace,mirror},red] (a) -- (b); |
| 453 |
\draw[decorate,decoration={brace,mirror,raise=5pt},blue] (a) -- (b); |
| 454 |
\end{tikzpicture} |
| 455 |
\end{codeexample} |
| 456 |
% |
| 457 |
\end{key} |
| 458 |
|
| 459 |
\begin{key}{/pgf/decoration/transform=\meta{transformations}} |
| 460 |
This key allows you to specify general \meta{transformations} to be applied |
| 461 |
to the segments of a decoration. These transformations are applied before |
| 462 |
and independently of |raise| and |mirror| transformations. The |
| 463 |
\meta{transformations} should be normal \tikzname\ transformations like |
| 464 |
|shift| or |rotate|. |
| 465 |
|
| 466 |
In the following example the |shift only| transformation is used to make |
| 467 |
sure that the crosses are \emph{not} sloped along the path. |
| 468 |
% |
| 469 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.shapes}}] |
| 470 |
\begin{tikzpicture} |
| 471 |
\draw [help lines] (0,0) grid (3,2); |
| 472 |
|
| 473 |
\draw (0,0) -- (1,1) arc (90:0:2 and 1); |
| 474 |
\draw[red,very thick] decorate [decoration={ |
| 475 |
crosses,transform={shift only},shape size=1.5mm}] |
| 476 |
{ (0,0) -- (1,1) arc (90:0:2 and 1) }; |
| 477 |
\end{tikzpicture} |
| 478 |
\end{codeexample} |
| 479 |
% |
| 480 |
\end{key} |
| 481 |
|
| 482 |
|
| 483 |
\subsubsection{Starting and Ending Decorations Early or Late} |
| 484 |
|
| 485 |
You sometimes may wish to ``end'' a decoration a bit early on the path. For |
| 486 |
instance, you might wish a |snake| decoration to stop 5mm before the end of the |
| 487 |
path and to continue in a straight line. There are different ways of achieving |
| 488 |
this effect, but the easiest may be the |pre| and |post| options, which only |
| 489 |
have an effect in \tikzname. Note, however, that they can only be used with |
| 490 |
decorations, not with meta-decorations. |
| 491 |
|
| 492 |
\begin{key}{/pgf/decoration/pre=\meta{decoration} (initially lineto)} |
| 493 |
This key sets a decoration that should be used before the main decoration |
| 494 |
starts. The \meta{decoration} will be used for a length of |pre length|, |
| 495 |
which |0pt| by default. Thus, for the |pre| option to have any effect, you |
| 496 |
also need to set the |pre length| option. |
| 497 |
% |
| 498 |
% TODO: Nesting tikzpictures is NOT supported |
| 499 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 500 |
\tikz [decoration={zigzag,pre=lineto,pre length=1cm}] |
| 501 |
\draw [decorate] (0,0) -- (2,1) arc (90:0:1); |
| 502 |
\end{codeexample} |
| 503 |
% |
| 504 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 505 |
\tikz [decoration={zigzag,pre=moveto,pre length=1cm}] |
| 506 |
\draw [decorate] (0,0) -- (2,1) arc (90:0:1); |
| 507 |
\end{codeexample} |
| 508 |
% |
| 509 |
\begin{codeexample}[preamble={\usetikzlibrary{ |
| 510 |
decorations.pathmorphing, |
| 511 |
decorations.shapes, |
| 512 |
}}] |
| 513 |
\tikz [decoration={zigzag,pre=crosses,pre length=1cm}] |
| 514 |
\draw [decorate] (0,0) -- (2,1) arc (90:0:1); |
| 515 |
\end{codeexample} |
| 516 |
|
| 517 |
Note that the default |pre| option is |lineto|, not |curveto|. This means |
| 518 |
that the default |pre| decoration will not follow curves (for efficiency |
| 519 |
reasons). Change the |pre| key to |curveto| if you have a curved path. |
| 520 |
% |
| 521 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 522 |
\tikz [decoration={zigzag,pre length=3cm}] |
| 523 |
\draw [decorate] (0,0) -- (2,1) arc (90:0:1); |
| 524 |
\end{codeexample} |
| 525 |
% |
| 526 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 527 |
\tikz [decoration={zigzag,pre=curveto,pre length=3cm}] |
| 528 |
\draw [decorate] (0,0) -- (2,1) arc (90:0:1); |
| 529 |
\end{codeexample} |
| 530 |
% |
| 531 |
\end{key} |
| 532 |
|
| 533 |
\begin{key}{/pgf/decoration/pre length=\meta{dimension} (initially 0pt)} |
| 534 |
This key sets the distance along which the pre-decoration should be used. |
| 535 |
If you do not need/wish a pre-decoration, set this key to |0pt| (exactly |
| 536 |
this string, not just to something that evaluates to the same things such |
| 537 |
as |0cm|). |
| 538 |
\end{key} |
| 539 |
|
| 540 |
\begin{key}{/pgf/decorations/post=\meta{decoration} (initially lineto)} |
| 541 |
Works like |pre|, only for the end of the decoration. |
| 542 |
\end{key} |
| 543 |
|
| 544 |
\begin{key}{/pgf/decorations/post length=\meta{dimension} (initially 0pt)} |
| 545 |
Works like |pre length|, only for the end of the decoration. |
| 546 |
\end{key} |
| 547 |
|
| 548 |
Here is a typical example that shows how these keys can be used: |
| 549 |
% |
| 550 |
\begin{codeexample}[preamble={\usetikzlibrary{decorations.pathmorphing}}] |
| 551 |
\begin{tikzpicture} |
| 552 |
[decoration=snake, |
| 553 |
line around/.style={decoration={pre length=#1,post length=#1}}] |
| 554 |
|
| 555 |
\draw[->,decorate] (0,0) -- ++(3,0); |
| 556 |
\draw[->,decorate,line around=5pt] (0,-5mm) -- ++(3,0); |
| 557 |
\draw[->,decorate,line around=1cm] (0,-1cm) -- ++(3,0); |
| 558 |
\end{tikzpicture} |
| 559 |
\end{codeexample} |