Creating vector graphics with LaTeX and TikZ

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Repetitive Patterns

Graphics often include a recurring sequence of elements. You could draw the dial of a train station clock (Figure 6) with 60 commands. But, you could also do it with just two (Listing 11, lines 2 and 3).

Figure 6: With just a few LaTeX lines, you can create a train station clock as a vector graphic.

Listing 11

A Clock

01 \begin{tikzpicture}
02 \foreach \x in {6,12,...,360} {\draw[line width=1.2pt] (\x:1.85cm) -- (\x:2cm);}
03 \foreach \y in {30,60,...,360} {\draw[line width=4pt] (\y:1.55cm) -- (\y:2cm);}
04 \draw[line width=6pt] (0,0) -- (0:1.45cm);
05 \draw[line width=4pt] (0,0) -- (90:1.85cm);
06 \draw[line width=2pt,red] (0,0) -- (180:2cm);
07 \fill[line width=2pt,red] (-1.45,0) circle (2mm);
08 \fill[line width=2pt] (0,0) circle (2mm);
09 \end{tikzpicture}

The vertical text position is indicated by above or below , and the horizontal positioning is indicated by left or right , thereby combining both positions.

If you want to create just the text box, you can precede the command with \path , as in line 2. The draw option draws a frame around the text, and the fill=<color> option fills the box with the given colors.

With the align=<orientation> option, you can align the text with the possible values left , center , right , and justify . To format the text, the usual LaTeX commands are available, including the math mode ones.

Cropping

Sometimes only a portion of a graphic really matters, so that it would make sense to create the whole graphic but then include only part of it in the document. This can be the case with function graphs where only the relevant parts need to be shown.

Listing 13 shows two modified lines for Listing 11. It first enlarges the graphic of the station clock by a factor of three (line 1), then it clips a 10-mm circle out of the nine-o'clock area (line 3) so that only that portion of the clock shows up.

Listing 13

Cropping

01 \begin{tikzpicture}[scale=3]
02 \clip (-2,0) circle (10mm);
03 % [... more as in Listing 11 ...]

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content