Creating vector graphics with LaTeX and TikZ

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Down to the Millimeter

LaTeX also provides a command to create a grid. You can thereby create a coordinate system or even generate graph paper.

The command in line 2 of Listing 3 causes LaTeX to draw a grid with 1-mm increments. The help lines option renders the lines thin and gray. The coordinates in the first parentheses indicate the lower left corner of the graphic, whereas the coordinates in the second set indicate the upper right corner.

Listing 3

Graph Paper

01 \begin{tikzpicture}
02 \draw[step=1mm,help lines] (0,0) grid (50mm,50mm);
03 \draw[step=10mm] (0,0) grid (50mm,50mm);
04 \end{tikzpicture}

With line 3, you superimpose a second set of thicker lines in 10-mm intervals, thus creating graph paper (see Figure 2).

Figure 2: It takes two lines of LaTeX code to create simple graph paper.

Line Styles

Table 2 shows a summary of the commands that you can use as options for setting line thicknesses. Listing 4 shows an example.

Table 2

From Thin to Thick

Command Thickness
line width=<thickness> Based on the given value
ultra thin 0.1 point
very thin 0.2 point
thin 0.4 point
semithick 0.6 point
thick 0.8 point
very thick 1.2 point
ultra thick 1.6 point

Listing 4

Line Thickness

01 \begin{tikzpicture}
02 \draw[ultra thin] (0,0) -- (6,0);
03 \draw[very thin] (0,0.5) -- (6,0.5);
04 \draw[thin] (0,1) -- (6,1);
05 \draw[semithick] (0,1.5) -- (6,1.5);
06 \draw[thick] (0,2) -- (6,2);
07 \draw[very thick] (0,2.5) -- (6,2.5);
08 \draw[ultra thick] (0,3) -- (6,3);
09 \draw[line width=2pt] (0,3.5) -- (6,3.5);
10 \end{tikzpicture}

Apart from line thickness, you can also determine the line style. Table 3 shows a summary of possible commands. Listing 5 shows an example, with the result in Figure 3.

Figure 3: LaTeX provides commands for different line patterns.

Table 3

Patterned

Command Pattern
solid Solid line (default)
dash pattern=on <width> off <width> Dashed line consisting of dashes at the specified on width and gaps at the specified off width
dotted Dotted line
densely dotted Densely dotted line
loosely dotted Loosely dotted line
dashed Dashed line
densely dashed Densely dashed line
loosely dashed Loosely dashed line
dashdotted Alternating dashed and dotted line
densely dashdotted Alternating densely dashed and dotted line
loosely dashdotted Alternating loosely dashed and dotted line
dashdotdotted Alternating dashed and double-dotted line
densely dashdotdotted Alternating densely dashed and double-dotted line
loosely dashdotdotted Alternating loosely dashed and double-dotted line

Listing 5

Dashed Lines

01 \begin{tikzpicture}
02 \draw[solid] (0,0) -- (6,0);
03 \draw[dash pattern=on 5mm off 2mm] (0,1) -- (6,1);
04 \draw[dotted] (0,2) -- (6,2);
05 \draw[densely dotted] (0,3) -- (6,3);
06 \draw[loosely dotted] (0,4) -- (6,4);
07 \draw[dashed] (0,5) -- (6,5);
08 \draw[densely dashed] (0,6) -- (6,6);
09 \draw[loosely dashed] (0,7) -- (6,7);
10 \draw[dashdotted] (0,8) -- (6,8);
11 \draw[densely dashdotted] (0,9) -- (6,9);
12 \draw[loosely dashdotted] (0,10) -- (6,10);
13 \draw[dashdotdotted] (0,11) -- (6,11);
14 \draw[densely dashdotdotted] (0,12) -- (6,12);
15 \draw[loosely dashdotdotted] (0,13) -- (6,13);
16 \end{tikzpicture}

You can create lines as arrows. Listing 6 provides two examples. You specify an option to determine the arrow's appearance. In the middle is always a double hyphen. The left-hand parameter sets the start of the arrow, and the right-hand parameter sets its ending. You use a greater-than symbol for the arrowhead, two greater-than symbols for a double arrowhead, and a vertical bar for a simple line ending.

Listing 6

Arrowheads

01 \tikz{\draw [|->] (0,0) -- (2,2);}
02 \tikz{\draw [->>] (0,0) arc (15:60:3cm);}

You can also specify how the line ends should look by using the line cap=<style> option. The styles include butt (default), rect , and round . Listing 7 shows an example and Figure 4 shows the result.

Figure 4: Various line endings: butt (bottom), rect (middle), and round (top).

Listing 7

Line Endings

01 \begin{tikzpicture}[line width=12pt]
02 \draw[line cap=butt] (0,0) -- (6,0);
03 \draw[line cap=rect] (0,1) -- (6,1);
04 \draw[line cap=round] (0,2) -- (6,2);
05 \end{tikzpicture}

With the line join=<style> option you can determine how corners should look. Styles include miter (default), bevel , and round . Listing 8 gives an example and Figure 5 shows the output.

Figure 5: Corners can look different: miter (bottom), bevel (middle), and round (top).

Listing 8

Corners

01 \begin{tikzpicture}[line width=12pt]
02 \draw[line join=miter] (0,0) -- (1,1) -- (0,2);
03 \draw[line join=bevel] (2,0) -- (3,1) -- (2,2);
04 \draw[line join=round] (4,0) -- (5,1) -- (4,2);
05 \end{tikzpicture}

You can change the line color by specifying an option. The tikz package automatically loads the xcolor package so that you can have extensive color application possibilities.

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