TikZ: Simple Drawings
TikZ: First start with TikZ
From our previous subsection on SmartDiagram, we now
move onto a more complex topic: the TikZ package.
In this first part we plan on covering how to draw simple things and
how to put them together to make something look nice. If you're looking
to draw more difficult things, then the second part on TikZ will be able
to give you more ideas of what you can build with this package.
Simple drawings with TikZ
Now we will cover the very basics of the package:
Drawing lines
First of all TikZ has a path syntax with its commands that acts like this:
Note that inside the environment
With that said, you can give single orders to TikZ by using the command line
For example following drawing yields the following result:
\path[<Options>]specification;
.
Most common Path Actions:
\draw
: draws a given argument\fill
: fills a given argument with a color\filldraw
: draws the border of a given argument and fills it with a color\shade
: fills a given argument with a smooth transition from one color to another
Note that inside the environment
tikzpicture
all of these
command lines are a shortcut for \path[command]
.With that said, you can give single orders to TikZ by using the command line
\tikz
following it up with another TikZ command. For example following drawing yields the following result:
CODE
\documentclass[12pt]{article}
\title{Drawing Lines}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\tikz \draw (0,0) -- (10,10);
\end{document}
But what exactly did we do?
Firstly, we used the TikZ package with
Firstly, we used the TikZ package with
\usepackage{tikz}
,
then we used the \tikz
command line to introduce our
\draw
command and we specified the coordinates from which the line should start
and to where it should arrive. These coordinates work like (x,y) on a Cartesian plane and
as long as --
is present in between you can keep drawing from point to point until you
choose that you have drawn enough random lines.
Likewise we can draw curved lines introducing the controls
command which must be
separated by two dots:..
and given the coordinates of the control points makes that coordinate
behave like a "center of gravity" which pulls the line towards it:CODE
\documentclass[12pt]{article}
\title{Drawing Curved Lines}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\draw (0,0) .. controls (0,8) and (5,0) .. (10,10);
\end{tikzpicture}
\end{document}
Control points must be separated by
and
when we want
more than one to have effect on our \draw
.
The other way to draw with TikZ is to begin the
tikzpicture
environment through the \begin{tikzpicture}
command line
and it's the one we're going to keep using since it's required to make more complex images.
Other than that there are a number of options you can insert in your \draw
command
to modify the behaviour of your line:
Geometric options:
rotate= angle in degree
: rotates the argument by the given anglexshift= length
: shifts the argument by the given horizontal lengthyshift= length
: shifts the argument by the given vertical lengthscale= factor
: rescales the argument by the given factorxscale= factor
: rescales the x coordinates of the argument by the given factoryscale= factor
: rescales the y coordinates of the argument by the given factor
Color options:
color= color name
: colors the argument with the given coloropacity= factor
: determines the opacity of the given color
Line width options:
line width= dimension
: determines the width of the line
Line end options:
line cap= type: round, rect, butt
: applies one of the 3 types to the argumentrounded corners
: makes the corners round by a default sizerounded corners= size
: makes the corner round by the given sizeline join= type: round, bevel, miter
: applies one of the 3 types to the argument
Line pattern options:
dash pattern= dash pattern
: determines a custom dash pattern (for example dash pattern= on 2pt off 3pt on 4pt off 5pt)solid
dashed
dotted
dashdotted
densely dotted
loosely dotted
double
CODE
\documentclass[12pt]{article}
\title{Drawing Curved Lines}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\draw[dashed, color=blue, rotate=36, opacity= 0.8, line width=1.2pt] (0,0)
.. controls (0,8) and (3,0) .. (10,10);
\draw[densely dotted, color=teal, line cap= rect, line join= bevel,
rounded corners=8pt] (0,0) .. controls (5,0) and (0,5) .. (5,5);
\end{tikzpicture}
\end{document}
More complex drawings
We're going to cover the
\fill
, \filldraw
,\shade
commands
as well as the options to draw rectangles, circles, grids and arcs in this subsection of TikZ.
Let us first start by drawing a grid and from there we will implement all the
above described commands.
The step
option is needed to specify the step of the grid.CODE
\documentclass[12pt]{article}
\title{More Complex Drawings: Step 1}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\draw[step=1cm, opacity= 0.6] (-5,-5) grid (5,5);
\end{tikzpicture}
\end{document}
After having drawn our initial grid giving the starting coordinates, the step and the ending coordinates
we can start using the commands
\fill
and rectangle
to see
how they behave.CODE
\documentclass[12pt]{article}
\title{More Complex Drawings: Step 2}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\fill[color= green, opacity= 0.7] (-2,2) rectangle (-4,4);
\draw[step=1cm, opacity= 0.6] (-5,-5) grid (5,5);
\end{tikzpicture}
\end{document}
Highlighted in bold we can now see what has changed since our last step.
We have used the
\fill
command to fill the given rectangle area going from (-2,2) to (-4,4)
Next up we're going to see how \filldraw
and circle
behave, but they are really not
much different from what we've already seen.
CODE
\documentclass[12pt]{article}
\title{More Complex Drawings: Step 3}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\filldraw[color= yellow, opacity= 0.85, draw= black] (3,3) circle (2cm);
\fill[color= green, opacity= 0.7] (-2,2) rectangle (-4,4);
\draw[step=1cm, opacity= 0.6] (-5,-5) grid (5,5);
\end{tikzpicture}
\end{document}
\filldraw
behaves essentially the same as fill except that it takes the
draw
option for the border of the argument given,
and circle takes the coordinates of the center and the length of its radius.
Now that we've seen these things in action let's move onto two slightly trickier
command and option: \shade
and arc
.
CODE
\documentclass[12pt]{article}
\title{More Complex Drawings: Step 3}
\date{}
\usepackage{tikz}
\begin{document}
\maketitle
\begin{tikzpicture}
\shade[left color= blue, right color= orange, opacity= 0.8] (-5,-4) -- (-3, -4) arc (0:90:2cm);
\filldraw[color= yellow, opacity= 0.85, draw= black] (3,3) circle (2cm);
\fill[color= green, opacity= 0.7] (-2,2) rectangle (-4,4);
\draw[step=1cm, opacity= 0.6] (-5,-5) grid (5,5);
\end{tikzpicture}
\end{document}
Now we can see how
\shade
works. It takes the direction
from which the color shades into the other (this also includes
top, bottom, inner
and outer
color options).
While arc
takes the origin of the angle, the coordinates of
the points where it spreads to and the width of the angle along with
its size.
Beginning charts
The next Graphics subsection will be teaching you how to start
out with drawing charts, it's bound to become more complex but we are
going to use what we've learnt on this initial part while adding commands
which are going to become gradually harder but better. You should
expect to be able to draw your own chart or graph after having read both TikZ subsections.