LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic Drawing
Part 1 | Part 2 | Part 3 | Part 4 | Part 5
Author: Josh Cassidy (August 2013)
This five-part series of articles uses a combination of video and textual descriptions to teach the basics of creating LaTeX graphics using TikZ. These tutorials were first published on the original ShareLateX blog site during August 2013; consequently, today's editor interface (Overleaf) has changed considerably due to the development of ShareLaTeX and the subsequent merger of ShareLaTeX and Overleaf. However, much of the content is still relevant and teaches you some basic LaTeX—skills and expertise that will apply across all platforms.
TikZ is a LaTeX package that allows you to create high quality diagrams—and often quite complex ones too. In this first post we'll start with the basics, showing how to draw simple shapes, with subsequent posts introducing some of the interesting things you can do using the tikz
package.
To get started with TikZ we need to load up the tikz
package:
\usepackage{tikz}
Now whenever we want to create a TikZ diagram we need to use the tikzpicture
environment.
\begin{tikzpicture}
<code goes here>
\end{tikzpicture}
Basic shapes
One of the simplest and most commonly used commands in TikZ is the \draw
command. To draw a straight line we use this command, then we enter a starting co-ordinate, followed by two dashes before the ending co-ordinate. We then finish the statement by closing it with a semicolon.
\draw (0,0) -- (4,0);
We can then add more co-ordinates in like this to make it a square:
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
However this isn't particularly good style. As we are drawing a line that ends up in the same place we started, it is better to finish the statement with the keyword cycle
rather than the last co-ordinate.
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
To simplify this code further we can use the rectangle
keyword after the starting co-ordinate and then follow it with the co-ordinate of the corner diagonally opposite.
\draw (0,0) rectangle (4,4);
We can also add lines that aren't straight. For example, this is how we draw a parabola:
\draw (0,0) parabola (4,4);
To add a curved line we use control points. We begin with our starting co-ordinate, then use two dots followed by the keyword controls
and then the co-ordinates of our control points separated by an and
. Then after two more dots we have the final point. These control points act like magnets attracting the line in their direction:
\draw (0,0) .. controls (0,4) and (4,0) .. (4,4);
We can then add a circle like this. The first co-ordinate is the circle's centre and the length in brackets at the end is the circle's radius:
\draw (2,2) circle (3cm);
This is how we draw an ellipse. This time the lengths in the brackets separated by an and
, are the x-direction radius and the y-direction radius respectively:
\draw (2,2) ellipse (3cm and 1cm);
This is how we draw an arc. In the final bracket we enter the starting angle, the ending angle and the radius. This time they are separated by colons:
\draw (3,0) arc (0:75:3cm);
To customise the way these lines are drawn we add extra arguments into the \draw
command. For example, we can edit the circle we drew so that the line is red, thick and dashed:
\draw[red,thick,dashed] (2,2) circle (3cm);
Grids
Very often when drawing diagrams we will want to draw a grid. To do this we use the \draw
command followed by by some additional arguments. For example, we specify the grid step size using step=
and a length. We've also specified the colour gray
and told it to make the lines very thin
. After these arguments we enter the co-ordinates of the bottom-left corner, followed by the keyword grid
and then the co-ordinates of the top right-corner:
\draw[step=1cm,gray,very thin] (-2,-2) grid (6,6);
If we want to remove the outer lines around this grid we can crop the size slightly like this:
\draw[step=1cm,gray,very thin] (-1.9,-1.9) grid (5.9,5.9);
Colour filling
Now lets add a shape onto our grid and colour it in. To do this we use the \fill
command instead of the \draw
command. Then in square brackets we enter a colour. For example, this specifies a colour that is 40% blue mixed with 60% white. Then we just specify a closed shape as we would normally:
\fill[blue!40!white] (0,0) rectangle (4,4);
If we wanted to add a border around this shape we could change it to the \filldraw
command and then alter the arguments so that we have both a fill colour and a draw colour specified:
\filldraw[fill=blue!40!white, draw=black] (0,0) rectangle (4,4);
If instead of one solid colour we want a colour gradient, we could change it to the \shade
command. Then in the square brackets we specify a left colour and a right colour:
\shade[left color=blue,right color=red] (0,0) rectangle (4,4);
Instead of doing it from left to right we could do it from top to bottom:
\shade[top color=blue,bottom color=red] (0,0) rectangle (4,4);
Or we could even change it by specifying an inner and outer colour like this:
\shade[inner color=blue,outer color=red] (0,0) rectangle (4,4);
Finally we could also add a border to this by using the \shadedraw
command and adding a draw colour:
\shadedraw[inner color=blue,outer color=red, draw=black] (0,0) rectangle (4,4);
Axes
Let's finish this post by adding some labeled axes to our grid. To do this we draw two normal lines both from (0,0), but we'll make them thick and add arrowheads using a dash and a pointed bracket:
\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);
We can also label our axes using nodes. To do this we add the keyword node
into both \draw
statements next to the end co-ordinates, followed by an anchor
specification in square brackets and the text in curly brackets. Every node
we create in TikZ has a number of anchors. So when we specify the north west
anchor for the x-axis node, we are telling TikZ to use the anchor in the top-left-hand corner to anchor the node to the co-ordinate:
\draw[thick,->] (0,0) -- (4.5,0) node[anchor=north west] {x axis};
\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east] {y axis};
To finish our axes we can add in ticks and numbering like this:
\foreach \x in {0,1,2,3,4}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
This clever piece of code uses two for each
loops to systematically go along the axes adding the ticks and numbers.
In each one, the variable x or y takes on all of the numbers in the curly brackets, each in turn and executes the \draw
command.
This concludes our discussion on basic drawing in TikZ. If you want to play around with the document we created in this post you can access it here. In the next post we'll look exporting TikZ code from GeoGebra.
All articles in this series
- Part 1: Basic Drawing;
- Part 2: Generating TikZ Code from GeoGebra;
- Part 3: Creating Flowcharts;
- Part 4: Circuit Diagrams Using Circuitikz;
- Part 5: Creating Mind Maps.
Please do keep in touch with us via Facebook, Twitter or via e-mail on our contact us page.
Overleaf guides
- Creating a document in Overleaf
- Uploading a project
- Copying a project
- Creating a project from a template
- Using the Overleaf project menu
- Including images in Overleaf
- Exporting your work from Overleaf
- Working offline in Overleaf
- Using Track Changes in Overleaf
- Using bibliographies in Overleaf
- Sharing your work with others
- Using the History feature
- Debugging Compilation timeout errors
- How-to guides
- Guide to Overleaf’s premium features
LaTeX Basics
- Creating your first LaTeX document
- Choosing a LaTeX Compiler
- Paragraphs and new lines
- Bold, italics and underlining
- Lists
- Errors
Mathematics
- Mathematical expressions
- Subscripts and superscripts
- Brackets and Parentheses
- Matrices
- Fractions and Binomials
- Aligning equations
- Operators
- Spacing in math mode
- Integrals, sums and limits
- Display style in math mode
- List of Greek letters and math symbols
- Mathematical fonts
- Using the Symbol Palette in Overleaf
Figures and tables
- Inserting Images
- Tables
- Positioning Images and Tables
- Lists of Tables and Figures
- Drawing Diagrams Directly in LaTeX
- TikZ package
References and Citations
- Bibliography management with bibtex
- Bibliography management with natbib
- Bibliography management with biblatex
- Bibtex bibliography styles
- Natbib bibliography styles
- Natbib citation styles
- Biblatex bibliography styles
- Biblatex citation styles
Languages
- Multilingual typesetting on Overleaf using polyglossia and fontspec
- Multilingual typesetting on Overleaf using babel and fontspec
- International language support
- Quotations and quotation marks
- Arabic
- Chinese
- French
- German
- Greek
- Italian
- Japanese
- Korean
- Portuguese
- Russian
- Spanish
Document structure
- Sections and chapters
- Table of contents
- Cross referencing sections, equations and floats
- Indices
- Glossaries
- Nomenclatures
- Management in a large project
- Multi-file LaTeX projects
- Hyperlinks
Formatting
- Lengths in LaTeX
- Headers and footers
- Page numbering
- Paragraph formatting
- Line breaks and blank spaces
- Text alignment
- Page size and margins
- Single sided and double sided documents
- Multiple columns
- Counters
- Code listing
- Code Highlighting with minted
- Using colours in LaTeX
- Footnotes
- Margin notes
Fonts
Presentations
Commands
Field specific
- Theorems and proofs
- Chemistry formulae
- Feynman diagrams
- Molecular orbital diagrams
- Chess notation
- Knitting patterns
- CircuiTikz package
- Pgfplots package
- Typesetting exams in LaTeX
- Knitr
- Attribute Value Matrices
Class files
- Understanding packages and class files
- List of packages and class files
- Writing your own package
- Writing your own class