26.10.2014 Views

TikZ and pgf

TikZ and pgf

TikZ and pgf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

\begin{tikzpicture}<br />

\draw (-1.5,0) -- (1.5,0);<br />

\draw (0,-1.5) -- (0,1.5);<br />

\draw (0,0) circle (1cm);<br />

\draw[step=.5cm] (-1.4,-1.4) grid (1.4,1.4);<br />

\end{tikzpicture}<br />

Having another look at the desired picture, Karl notices that it would be nice for the grid to be more<br />

subdued. (His son told him that grids tend to be distracting if they are not subdued.) To subdue the grid,<br />

Karl adds two more options to the \draw comm<strong>and</strong> that draws the grid. First, he uses the color gray for the<br />

grid lines. Second, he reduces the line width to very thin. Finally, he swaps the ordering of the comm<strong>and</strong>s<br />

so that the grid is drawn first <strong>and</strong> everything else on top.<br />

\begin{tikzpicture}<br />

\draw[step=.5cm,gray,very thin] (-1.4,-1.4) grid (1.4,1.4);<br />

\draw (-1.5,0) -- (1.5,0);<br />

\draw (0,-1.5) -- (0,1.5);<br />

\draw (0,0) circle (1cm);<br />

\end{tikzpicture}<br />

3.8 Adding a Touch of Style<br />

Instead of the options gray,very thin Karl could also have said style=help lines. Styles are predefined<br />

sets of options that can be used to organize how a graphic is drawn. By saying style=help lines you say<br />

“use the style that I (or someone else) has set for drawing help lines.” If Karl decides, at some later point,<br />

that grids should be drawn, say, using the color blue!50 instead of gray, he could say the following:<br />

\tikzstyle help lines=[color=blue!50,very thin]<br />

Alternatively, he could have said the following:<br />

\tikzstyle help lines+=[color=blue!50]<br />

This would have added the color=blue!50 option. The help lines style would now contain two color<br />

options, but the second would override the first.<br />

Using styles makes your graphics code more flexible. You can change the way things look easily in a<br />

consistent manner.<br />

To build a hierarchy of styles you can have one style use another. So in order to define a style Karl’s grid<br />

that is based on the grid style Karl could say<br />

\tikzstyle Karl’s grid=[style=help lines,color=blue!50]<br />

...<br />

\draw[style=Karl’s grid] (0,0) grid (5,5);<br />

You can also leave out the style=. Thus, whenever <strong>TikZ</strong> encounters an options that it does not know<br />

about, it will check whether this option happens to be the name of a style. If so, the style is used. Thus,<br />

Karl could also have written:<br />

\tikzstyle Karl’s grid=[help lines,color=blue!50]<br />

...<br />

\draw[Karl’s grid] (0,0) grid (5,5);<br />

For some styles, like the very thin style, it is pretty clear what the style does <strong>and</strong> there is no need to say<br />

style=very thin. For other styles, like help lines, it seems more natural to me to say style=help lines.<br />

But, mainly, this is a matter of taste.<br />

3.9 Drawing Options<br />

Karl wonders what other options there are that influence how a path is drawn. He saw already that the<br />

color=〈color〉 option can be used to set the line’s color. The option draw=〈color〉 does nearly the same, only<br />

24

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!