Beruflich Dokumente
Kultur Dokumente
04.05.16 15:52
About Us
ShareLaTeX (/) is an online LaTeX editor that
allows real-time collaboration in your browser
Try ShareLaTeX Now (/)
! Subscribe (/blog/rss.xml) / ! Atom
(/blog/atom.xml)
often quite complex diagrams. In this post were going to show you some of the basics and
Recent Posts
(/blog/2016/03/07/references-search.html)
Autocomplete of reference keys - Easier citing
(/blog/2016/02/09/autocomplete-of-referenceseaiser-citing.html)
Word count your LaTeX project
(/blog/2015/09/15/word-count.html)
500,000 Users, Long live LaTeX!
(/blog/2015/08/01/500-000-users-long-livelatex%20copy.html)
\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
(/blog/2015/02/10/shareLaTeX-github-sync.html)
two dashes before the ending co-ordinate. We then finish the statement by closing it with a
semicolon.
\draw (0,0) -- (4,0);
Video Guides
Creating your first LaTeX document (/blog/latexguides/beginners-tutorial/your-first-latex-
document.html)
Paragraphs and Sections in LaTeX (/blog/latexguides/beginners-tutorial/paragraphs-andsections-in-latex.html)
Mathematics in LaTeX (/blog/latexguides/beginners-tutorial/mathematics-inlatex.html)
Images in LaTeX (/blog/latex-guides/beginnerstutorial/images-in-latex.html)
Bibliographies and Natbib (/blog/latexguides/beginners-tutorial/bibliographies-inlatex.html)
Tables and Matrices in LaTeX (/blog/latexguides/beginners-tutorial/tables-and-matricesin-latex.html)
However this isnt 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;
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 1 von 11
04.05.16 15:52
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 arent 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 circles centre and the length in
brackets at the end is the circles 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
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 2 von 11
04.05.16 15:52
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. Weve 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);
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 3 von 11
04.05.16 15:52
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);
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 4 von 11
04.05.16 15:52
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);
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 5 von 11
04.05.16 15:52
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);
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 6 von 11
04.05.16 15:52
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
Lets finish this post by adding some labeled axes to our grid. To do this we draw two normal
lines both from (0,0), but well 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};
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 7 von 11
04.05.16 15:52
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
(https://www.sharelatex.com/project/521b4a1206bf09190f129751). In the next post well look
exporting TikZ code from GeoGebra. Please do keep in touch with us via Facebook
(https://www.facebook.com/ShareLaTeX), Twitter (https://twitter.com/sharelatex) & Google+
(https://plus.google.com/115074691861228882827/posts).
Seite 8 von 11
04.05.16 15:52
pt4.html)
Creating Mind Maps Using TikZ (https://www.sharelatex.com/blog/2013/09/04/tikz-seriespt5.html)
Posted by Josh Cassidy on 27 Aug 2013
1
!
" Share
% Recommend
Login
Sort by Best
2 years ago
Great tutorial thanks! Just started out learning TikZ with the WP-QuickLatex plugin on a Wordpress
site.
3&
'
Subscribe
Reply Share
2016 ShareLaTeX
Blog (/blog)
AGB (/tos)
Privacy
Datenschutz (/privacy_policy)
Universities (/university?ref=footer)
Sicherheit (/security)
Contact ()
(http://www.twitter.com/sharelat
(http://www.facebook.com/pages/S
(https://plus.google.com/11507469
(https://github.com/sharelatex/sha
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 9 von 11
04.05.16 15:52
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 10 von 11
04.05.16 15:52
https://de.sharelatex.com/blog/2013/08/27/tikz-series-pt1.html
Seite 11 von 11