Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
66 views18 pages

Tikz - Samples: 1 Lines and Grids

The document provides examples of using Tikz to draw various graphics elements including lines, grids, curves, shapes, and paths. It includes 15 sections that demonstrate how to draw simple and connected lines, add line thickness, style, and color. It also shows how to draw grids, arrows, curved lines, circles, rectangles, polygons, ellipses, and use paths to draw, fill, or draw and fill shapes. The examples cover basic through more advanced Tikz graphics capabilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views18 pages

Tikz - Samples: 1 Lines and Grids

The document provides examples of using Tikz to draw various graphics elements including lines, grids, curves, shapes, and paths. It includes 15 sections that demonstrate how to draw simple and connected lines, add line thickness, style, and color. It also shows how to draw grids, arrows, curved lines, circles, rectangles, polygons, ellipses, and use paths to draw, fill, or draw and fill shapes. The examples cover basic through more advanced Tikz graphics capabilities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Tikz - Samples

Ren Tristan A. de la Cruz


December 22, 2020

1 Lines and Grids


1. Simple Line:

\draw (0, 0) -- (1,1);

2. Multiple Lines:

\draw (0, 0) -- (1,1);


\draw (2, 0) -- (2,1);
\draw (3, 1) -- (4,0);

3. Connected Lines:

\draw (0, 0) -- (0,1) -- (1,2) -- (2,1) -- (2,0);

4. Connected Lines + Cycle:

\draw (0, 0) -- (0,1) -- (1,2) -- (2,1) -- (2,0) -- cycle;

5. Help Lines Grid 1:

\draw [help lines] (0,0) grid (3,3);


\draw (0,0) -- (3,3);

1
6. Grid 2:

\draw [help lines] (-3,-3) grid (3,3);


\draw (0, 0) -- (3,3);

7. Grid 3:

\draw [help lines] (-3,-3) grid (3,3);


\draw (0, 0) -- (1,1);
\draw (1, 0) -- (2,1);
\draw (2, 0) -- (3,1);

8. Grid 4:

\draw[help lines] (-3,-3) grid (3,3);


\draw ( 0.5, 0.5) -- ( 1.5, 1.5);
\draw (-0.5, 0.5) -- (-2.0, 2.0);
\draw (-0.5, -0.5) -- (-2.5, -2.5);
\draw ( 0.5, -0.5) -- ( 3.0, -3.0);

2
9. Line Thickness 1:

\draw[ultra thin] (0, 0) -- (1,1);


\draw[very thin] (1, 0) -- (2,1);
\draw[thin] (2, 0) -- (3,1);
\draw (3, 0) -- (4,1);
\draw[semithick] (4, 0) -- (5,1);
\draw[thick] (5, 0) -- (6,1);
\draw[very thick] (6, 0) -- (7,1);
\draw[ultra thick] (7, 0) -- (8,1);

10. Line Thickness 2:

\draw [line width = 0.25] (0, 0) -- (1,1);


\draw [line width = 0.50] (1, 0) -- (2,1);
\draw [line width = 0.75] (2, 0) -- (3,1);
\draw [line width = 1.00] (3, 0) -- (4,1);
\draw [line width = 1.50] (4, 0) -- (5,1);
\draw [line width = 2.00] (5, 0) -- (6,1);
\draw [line width = 3.00] (6, 0) -- (7,1);
\draw [line width = 4.00] (7, 0) -- (8,1);
\draw [line width = 5.00] (8, 0) -- (9,1);

11. Dashed and Dotted Lines:

\draw [dashed, line width = 0.25] (0, 0) -- (1,1);


\draw [dashed, line width = 0.50] (1, 0) -- (2,1);
\draw [dashed, line width = 0.75] (2, 0) -- (3,1);
\draw [dashed, line width = 1.00] (3, 0) -- (4,1);
\draw [dashed, line width = 1.50] (4, 0) -- (5,1);
\draw [dotted, line width = 0.25] (5, 0) -- (6,1);
\draw [dotted, line width = 0.50] (6, 0) -- (7,1);
\draw [dotted, line width = 0.70] (7, 0) -- (8,1);
\draw [dotted, line width = 1.00] (8, 0) -- (9,1);
\draw [dotted, line width = 1.50] (9, 0) -- (10,1);

3
12. Colors:

\draw [red , very thick] (0,0) -- ( 1,1);


\draw [green , very thick] (1,0) -- ( 2,1);
\draw [blue , very thick] (2,0) -- ( 3,1);
\draw [yellow , very thick] (3,0) -- ( 4,1);
\draw [orange , very thick] (4,0) -- ( 5,1);
\draw [olive , very thick] (5,0) -- ( 6,1);
\draw [lime , very thick] (6,0) -- ( 7,1);
\draw [brown , very thick] (7,0) -- ( 8,1);
\draw [magenta, very thick] (8,0) -- ( 9,1);
\draw [cyan , very thick] (9,0) -- (10,1);

13. Colors 2:

\begin{tikzpicture}
[
define rgb/.code = {\definecolor{mycolor}{RGB}{#1}},
rgb color/.style = {define rgb={#1},mycolor}
]
\draw [rgb color={ 50, 100, 150}, very thick] (0,0) -- ( 1,1);
\draw [rgb color={200, 60, 35}, very thick] (1,0) -- ( 2,1);
\draw [rgb color={255, 255, 20}, very thick] (2,0) -- ( 3,1);
\draw [rgb color={ 30, 40, 180}, very thick] (3,0) -- ( 4,1);
\draw [rgb color={100, 180, 150}, very thick] (4,0) -- ( 5,1);

14. Grid + Style 1:

\draw[step=1.00, red , thin, dotted] ( 0, 0) grid ( 3, 3);


\draw[step=0.50, green, thin, dashed] ( 0, 0) grid (-3, 3);
\draw[step=0.25, blue , ultra thin] (-3, 0) grid ( 3, -3);

4
15. Arrows:

\draw [->] (0,0) -- (0,1);


\draw [<-] (1,0) -- (1,1);
\draw [<->] (2,0) -- (2,1);
\draw [>->] (3,0) -- (3,1);
\draw [|->] (4,0) -- (4,1);
\draw [<-|] (5,0) -- (5,1);
\draw [|-|] (6,0) -- (6,1);
\draw [->>] (7,0) -- (7,1);
\draw [<<-] (8,0) -- (8,1);
\draw [<<->>] (9,0) -- (9,1);

2 Curves
• Curved Lines:

\draw [dotted] (0,0) grid (12,1);


\draw [red] (0,0) .. controls( 1,1) .. ( 4,0);
\draw [green] (4,0) .. controls( 6,1) .. ( 8,0);
\draw [blue] (8,0) .. controls(11,1) .. (12,0);

• Curved Lines 2:

\draw [dotted] (0,0) grid (12,1);


\draw [yellow] (0,0) .. controls( 1,1) and ( 2,1) .. (4,0);
\draw [cyan] (4,0) .. controls( 5,1) and ( 7,1) .. (8,0);
\draw [magenta] (8,0) .. controls(10,1) and (11,1).. (12,0);

• Curved Lines 3:

\draw [dotted] (0,0) grid (12,1);


\draw [lime] (0,0) .. controls(0.1, 1.3) .. (4,0);
\draw [orange] (4,0) .. controls(7.9, 1.0) .. (8,0);
\draw [violet] (8,0) .. controls(9,2) and (11.9,0.1) .. (12,0);

• Curved Lines 4:

\draw [dotted] (0,0) grid (3,3);


\draw [pink, thick] (0,0) .. controls(0,5) and (3,-2) .. (3,3);

5
3 Shapes
1. Circle:

\draw [dotted] (-1,-1) grid (1,1);


\draw (0,0) circle (1cm);

2. Circle + Style:

\draw [dotted] (-2,-2) grid (2,2);


\draw [red , line width = 1.0] (0,0) circle (1.0cm);
\draw [green, line width = 2.0, dotted] (0,0) circle (1.5cm);
\draw [blue , line width = 0.5, dashed] (0,0) circle (2.0cm);

3. Circle + Fill:

\draw [dotted,step=0.5] (-4,2) grid (5,-2);


\draw[fill=yellow] ( 0.0, 0) circle (1.0cm);
\draw[fill=cyan] (-2.5, 0) circle (1.5cm);
\draw[fill=magenta] ( 3.0, 0) circle (2.0cm);

6
4. Rectangle:

\draw [dotted] (0,0) grid (4,3);


\draw [thick] (0,0) rectangle (3,2);

5. Rectangle + Style:

\draw [dotted] (-3,-3) grid (6,4);


\draw [pink , line width = 1.0] ( 0, 0) rectangle (1,1);
\draw [violet, line width = 2.0, dashed] (-1,-1) rectangle (3,2);
\draw [blue , line width = 3.0, dotted] (-2,-2) rectangle (5,3);

6. Rectangle + Fill:

\draw [dotted] (-1,-1) grid (8,5);


\draw [fill=green] (0,0) rectangle (1,1);
\draw [fill=orange] (2,0) rectangle (3,4);
\draw [fill=red] (4,0) rectangle (7,3);

7
7. Polygon:

\draw [dotted] (0,-2) grid (4,1);


\draw (0,0) -- (1,1) -- (2,0) -- (3,1) -- (4,0) -- (2,-2) -- cycle;

8. Polygon + Style:

\draw [dotted] (-3,-3) grid (4,3);


\draw [blue, thick, dotted] (0,0) -- (1.5,1.5) -- (3,0) -- cycle;
\draw [red , thick, dashed] (-0.3,0) -- (0.8,1.3) -- (-2,2) -- (-1.5,0) -- cycle;
\draw [gray, thick] (-1.5, -0.3) -- (1.5,-0.3) -- (2,-2) -- (-1.5,-2) -- cycle;

9. Polygon + Fill:

\draw [dotted] (-3,-3) grid (4,3);


\draw [fill=blue] (0,0) -- (1.5,1.5) -- (3,0) -- cycle;
\draw [fill=red] (-0.3,0) -- (0.8,1.3) -- (-2,2) -- (-1.5,0) -- cycle;
\draw [fill=gray] (-1.5, -0.3) -- (1.5,-0.3) -- (2,-2) -- (-1.5,-2) -- cycle;

8
10. Ellipse:

\draw [dotted] (-3,-3) grid (3,3);


\draw [violet] (0,0) ellipse (1.0 and 1.0);
\draw [olive] (0,0) ellipse (2.0 and 1.0);
\draw [orange] (0,0) ellipse (2.0 and 3.0);

4 Paths
1. Path + Draw: \draw is \path[draw]

\path [draw, dotted] (0,0) grid (2,1);


\path [draw, red] (0,0) -- (1,1);
\draw [red] (1,0) -- (2,1);

2. Path + Fill: \fill is \path[fill]

\path[draw, dotted] (0,0) grid (5,3);


\path[fill, cyan] (1,1) rectangle (2,2);
\fill[cyan] (3,1) rectangle (4,2);

9
3. Path + Fill + Draw: \filldraw is \path[draw, fill]

\path[draw, dotted] (0,0) grid (5,3);


\path[fill=yellow, draw=blue, very thick] (1,1) rectangle (2,2);
\filldraw[yellow, draw=blue, very thick] (3,1) rectangle (4,2);

4. Path + Shade: \shade is \path[shade]

\path [draw, dotted] (0,0) grid (5,3);


\path [shade, left color = red, right color = green] (1,1) rectangle (2,2);
\shade [left color = red, right color = green] (3,1) rectangle (4,2);

5. Path + Shade + Draw: \shadedraw is \path[shade,draw]

\path [draw, dotted] (0,0) grid (5,3);


\path [shade,draw, left color = violet, right color = pink, thick] (1,1) rectangle (2,2);
\shadedraw [left color = violet, right color = pink, thick] (3,1) rectangle (4,2);

6. Path + Clip: \clip is \path[clip]

\path [draw, dotted] (0,0) grid (4,2);


\begin{scope}
\path [clip] (1,1) rectangle (2,2);

10
\path [fill=pink] (1,1) circle (1.0cm);
\end{scope}
\begin{scope}
\clip (3,1) rectangle (4,2);
\path [fill=pink] (3,1) circle (1.0cm);
\end{scope}

5 Nodes
1. Nodes on a Path:

\draw [dotted] (0,0) grid (5,5);


\path [draw] (0,0) node {A} -- (0,1) node {B} -- (2,0) node {C} -- (1,3) node {D} -- (5,5) node {E};

A C

2. Nodes on a Path + Positioning Text:

\draw [dotted] (0,0) grid (5,5);


\path [draw] (0,0) node [below] {A} -- (0,1) node [left] {B} -- (2,0) node [below] {C} --
(1,3) node [left] {D} -- (5,5) node [right] {E};

A C

3. Nodes:

11
\draw [dotted] (0,0) grid (5,5);
\node [circle , fill = green] (nodeA) at (0,0) {A};
\node [rectangle, fill = yellow] (nodeB) at (0,1) {B};
\node [circle , fill = red] (nodeC) at (2,0) {C};
\node [circle , fill = purple] (nodeD) at (1,3) {D};
\node [rectangle, fill = cyan] (nodeE) at (5,5) {E};

A C

4. Nodes + Path:

\draw [dotted] (0,0) grid (5,5);


\node [circle , fill = green] (nodeA) at (0,0) {A};
\node [rectangle, fill = yellow] (nodeB) at (0,1) {B};
\node [circle , fill = red] (nodeC) at (2,0) {C};
\node [circle , fill = purple] (nodeD) at (1,3) {D};
\node [rectangle, fill = cyan] (nodeE) at (5,5) {E};
\draw [very thick] (nodeA) -- (nodeB) -- (nodeC) -- (nodeD) -- (nodeE);

A C

5. Nodes + Arrows:

\draw [dotted] (0,0) grid (5,5);


\node [circle , fill = green] (nodeA) at (0,0) {$\alpha$};
\node [rectangle, fill = yellow] (nodeB) at (0,1) {$\beta^2$};
\node [circle , fill = red] (nodeC) at (2,0) {$\kappa^3$};
\node [circle , fill = purple] (nodeD) at (1,3) {$\delta^4$};
\node [rectangle, fill = cyan] (nodeE) at (5,5) {$\epsilon^5$};
\draw [->, very thick] (nodeA) -- (nodeB);
\draw [->, very thick] (nodeB) -- (nodeC);
\draw [->, very thick] (nodeC) -- (nodeD);
\draw [->, very thick] (nodeD) -- (nodeE);
\draw [->, very thick] (nodeE) -- (nodeC);
\draw [->, very thick] (nodeC) -- (nodeA);
\draw [->, very thick] (nodeB) -- (nodeD);

12
5

δ4

β2

α κ3

6. Graph:

\draw [dotted] (-1,-3) grid (7,3);


\node [circle,draw] (n1) at (0 ,0) {$\alpha^1$};
\node [circle,draw] (n2) at (2, 2) {$\alpha^2$};
\node [circle,draw] (n3) at (2, 0) {$\alpha^3$};
\node [circle,draw] (n4) at (2,-2) {$\alpha^4$};
\node [circle,draw] (n5) at (4, 2) {$\alpha^5$};
\node [circle,draw] (n6) at (4, 0) {$\alpha^6$};
\node [circle,draw] (n7) at (4,-2) {$\alpha^7$};
\node [circle,draw] (n8) at (6, 0) {$\alpha^8$};
\draw [->] (n1) -- (n2);
\draw [->] (n1) -- (n3);
\draw [->] (n1) -- (n4);
\draw [->] (n2) -- (n5);
\draw [->] (n2) -- (n6);
\draw [->] (n2) -- (n7);
\draw [->] (n3) -- (n5);
\draw [->] (n3) -- (n6);
\draw [->] (n3) -- (n7);
\draw [->] (n4) -- (n5);
\draw [->] (n4) -- (n6);
\draw [->] (n4) -- (n7);
\draw [->] (n5) -- (n8);
\draw [->] (n6) -- (n8);
\draw [->] (n7) -- (n8);

α2 α5

α1 α3 α6 α8

α4 α7

7. Nodes + Relative Positions + Single Referene Node:

\usetikzlibrary{positioning}

13
\draw[dotted] (0,0) grid (4,4);
\node (n1) [draw, circle] at (2,2) {$1$};
\node (n2) [draw, circle, above = of n1] {$2$};
\node (n3) [draw, circle, right = of n1] {$3$};
\node (n4) [draw, circle, below = of n1] {$4$};
\node (n5) [draw, circle, left = of n1] {$5$};
\node (n6) [draw, circle, above left = of n1] {$6$};
\node (n7) [draw, circle, above right = of n1] {$7$};
\node (n8) [draw, circle, below left = of n1] {$8$};
\node (n9) [draw, circle, below right = of n1] {$9$};

2
6 7

5 1 3

8 9
4

8. Nodes + Relative Positions + Multiple Reference Nodes:

\usetikzlibrary{positioning}
\draw[dotted] (0,0) grid (7,4);
\node (n1) [draw, circle] at (1,1) {$1$};
\node (n2) [draw, circle, above = of n1] {$2$};
\node (n3) [draw, circle, right = of n1] {$3$};
\node (n4) [draw, circle, above = of n3] {$4$};
\node (n5) [draw, circle, right = of n3] {$5$};
\node (n6) [draw, circle] at (6,3) {$6$};
\node (n7) [draw, circle, below = of n6] {$7$};
\node (n8) [draw, circle, left = of n6] {$8$};

8 6
2 4

7
1 3 5

9. Nodes + Relative Positions + Node Distance:

\draw[dotted] (0,0) grid (4,4);


\node (n1) [draw, circle] at (2,2) {$1$};
\node (n2) [draw, circle, above = 0.3cm of n1] {$2$};
\node (n3) [draw, circle, right = 0.5cm of n1] {$3$};
\node (n4) [draw, circle, below = 0.7cm of n1] {$4$};
\node (n5) [draw, circle, left = 0.9cm of n1] {$5$};

14
2

5 1 3

10. Nodes + Relative Positions + Node Distance 2:

\draw[dotted] (0,0) grid (4,4);


\begin{scope}[node distance=5.0mm, minimum size=0.7cm]
\node (na) [draw, circle] at (2,2) {$a$};
\node (nb) [draw, circle, above = of na] {$b$};
\node (nc) [draw, circle, right = of na] {$c$};
\node (nd) [draw, circle, below = of na] {$d$};
\node (ne) [draw, circle, left = of na] {$e$};
\node (nf) [draw, circle, above left = of na] {$f$};
\node (ng) [draw, circle, above right = of na] {$g$};
\node (nh) [draw, circle, below left = of na] {$h$};
\node (ni) [draw, circle, below right = of na] {$i$};
\end{scope}

b
f g

e a c

h i
d

11. Node + Relative Positions + Node Distance 3:

\draw[dotted] (0,0) grid (4,4);


\begin{scope}[node distance=5.0mm and 5.0mm, minimum size=0.7cm]
\node (na) [draw, rectangle] at (2,2) {$a$};
\node (nb) [draw, rectangle, above = of na] {$b$};
\node (nc) [draw, rectangle, right = of na] {$c$};
\node (nd) [draw, rectangle, below = of na] {$d$};
\node (ne) [draw, rectangle, left = of na] {$e$};
\node (nf) [draw, rectangle, above left = of na] {$f$};
\node (ng) [draw, rectangle, above right = of na] {$g$};
\node (nh) [draw, rectangle, below left = of na] {$h$};
\node (ni) [draw, rectangle, below right = of na] {$i$};
\end{scope}

15
f b g

e a c

h d i

6 Controls
1. Scope:

\draw [dotted] (0,0) grid (4,3);


\begin{scope} [dotted, red, very thick]
\node (n1) [draw, circle] at (1,1) {$1$};
\node (n2) [draw, circle] at (2,2) {$2$};
\node (n3) [draw, circle] at (3,1) {$3$};
\end{scope}
\begin{scope} [dotted, blue, thick]
\node (n4) [draw, rectangle] at (1,2) {$4$};
\node (n5) [draw, rectangle] at (2,1) {$5$};
\node (n6) [draw, rectangle] at (3,2) {$6$};
\end{scope}

4 2 6

1 5 3

2. Style:

\begin{tikzpicture}[scale=1.0,
MyStyle1/.style = {draw, circle, green, thick, minimum size = 1.0cm},
MyStyle2/.style = {draw, rectangle, magenta, ultra thick, minimum size = 0.5cm}]
\draw [dotted] (0,0) grid (4,3);
\node (n1) [MyStyle1] at (1,1) {$1$};
\node (n2) [MyStyle1] at (1,2) {$2$};
\node (n3) [MyStyle1] at (3,2) {$3$};
\node (na) [MyStyle2] at (2,2) {$a$};
\node (nb) [MyStyle2] at (2,1) {$b$};
\node (nc) [MyStyle2] at (3,1) {$c$};
\end{tikzpicture}

2 a 3

1 b c

3. Loops:

16
\draw [dotted] (0,0) grid (10, 3);
\foreach \x in {1,...,9}
{
\node (n\x) [draw, circle] at (\x, 1) {$\x$};
\node (m\x) [draw, circle, above= 0.3cm of n\x] {$\x’$};
}

10 20 30 40 50 60 70 80 90

1 2 3 4 5 6 7 8 9

4. Loops 2:

\draw [dotted] (0,0) grid (6, 6);


\foreach \x in {1,...,5}
\foreach \y in {1,...,5}
{
\node (n\x\y) [draw, circle] at (\x, \y) {$\x,\y$};
}

1, 5 2, 5 3, 5 4, 5 5, 5

1, 4 2, 4 3, 4 4, 4 5, 4

1, 3 2, 3 3, 3 4, 3 5, 3

1, 2 2, 2 3, 2 4, 2 5, 2

1, 1 2, 1 3, 1 4, 1 5, 1

5. Anchors:

\draw [dotted] (0,0) grid (10,4);


\node (n1) [draw, circle] at (1,1) {$1$};
\node (n2) [draw, circle] at (3,3) {$2$};
\node (n3) [draw, circle] at (4,1) {$3$};
\node (n4) [draw, circle] at (6,3) {$4$};
\node (n5) [draw, circle] at (7,1) {$5$};
\node (n6) [draw, circle] at (9,3) {$6$};
\draw [->] (n1) -- (n2);
\draw [->] (n3.east) -- (n4.west);
\draw [->] (n5.north) -- (n6.south);
\draw [->] (n4.south east) -- (n5.north west);
\draw [->] (n1.north east) -- (n3.south west);

17
2 4 6

1 3 5

6. X:

18

You might also like