Sei sulla pagina 1di 13

LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

Documentation Home  Search help library....


(/learn/Main_Page)
Learn LaTeX in 30
minutes (/learn
/Learn_LaTeX_in_30_minutes)
LaTeX Graphics using TikZ: A
Overleaf guides Tutorial for Beginners (Part
Creating a document in
Overleaf (/learn
1)—Basic Drawing
/Kb/Creating_a_document_in_Overleaf)
Uploading a project
(/learn Part 1 | Part 2 (/learn
/Kb/Uploading_a_project) /LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_2)
Copying a project (/learn %E2%80%94Generating_TikZ_Code_from_GeoGebra) | Part 3 (/learn
/Kb/Copying_a_project) /LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_3)
Creating a project from a %E2%80%94Creating_Flowcharts) | Part 4 (/learn
template (/learn /LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_4)
/Kb/Creating_a_project_from_a_template)
%E2%80%94Circuit_Diagrams_Using_Circuitikz) | Part 5 (/learn
Including images in /LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_5)
Overleaf (/learn %E2%80%94Creating_Mind_Maps)
/Kb/Including_images_in_ShareLaTeX)
Author: Josh Cassidy (August 2013)
Exporting your work
from Overleaf (/learn This �ve-part series of articles uses a combination of video and
/Kb/Exporting_your_work_from_ShareLaTeX)
textual descriptions to teach the basics of creating LaTeX graphics
Working of�ine in using TikZ. These tutorials were �rst published on the original
Overleaf (/learn
ShareLateX blog site during August 2013; consequently, today's
/Kb/Working_Of�ine_in_Overleaf_v2)
editor interface (Overleaf) has changed considerably due to the
Using Track Changes in
development of ShareLaTeX and the subsequent merger of
Overleaf (/learn
ShareLaTeX and Overleaf. However, much of the content is still
/Kb/Track_Changes_in_Overleaf_v2)
relevant and teaches you some basic LaTeX—skills and expertise that
Using bibliographies in
will apply across all platforms.
Overleaf (/learn
/Kb/Using_bibliographies_in_ShareLaTeX)
Sharing your work with
others (/learn
/Kb/Sharing_your_work_with_others)
Debugging Compilation
timeout errors (/learn
/Kb/Debugging_Compilation_timeout_errors)
How-to guides (/learn
/Kb/Knowledge_Base)

1 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

LaTeX Basics
Basic Drawing Using TikZ
Creating your �rst LaTeX
document (/learn
/Creating_a_document_in_LaTeX)
Choosing a LaTeX
Compiler (/learn
/Choosing_a_LaTeX_Compiler)
Paragraphs and new lines
(/learn
/Paragraphs_and_new_lines)
Bold, italics and
underlining (/learn
/Bold,_italics_and_underlining)
Lists (/learn/Lists)
Errors (/learn/Errors)
TikZ (http://cremeronline.com/LaTeX/minimaltikz.pdf) is a LaTeX
package (https://ctan.org/pkg/pgf?lang=en) that allows you to create
Mathematics
high quality diagrams—and often quite complex ones too. In this �rst
Mathematical post we'll start with the basics, showing how to draw simple shapes,
expressions (/learn with subsequent posts introducing some of the interesting things you
/Mathematical_expressions) can do using the tikz package.
Subscripts and
superscripts (/learn To get started with TikZ we need to load up the tikz package:
/Subscripts_and_superscripts)
\usepackage{tikz}
Brackets and
Parentheses (/learn Now whenever we want to create a TikZ diagram we need to use the
/Brackets_and_Parentheses) tikzpicture environment.
Fractions and Binomials
(/learn
\begin{tikzpicture}
<code goes here>
/Fractions_and_Binomials)
\end{tikzpicture}
Aligning Equations
(/learn
Basic shapes
/Aligning_equations)
Operators (/learn One of the simplest and most commonly used commands in TikZ is
/Operators) the \draw command. To draw a straight line we use this command,
Spacing in math mode then we enter a starting co-ordinate, followed by two dashes before
(/learn the ending co-ordinate. We then �nish the statement by closing it
/Spacing_in_math_mode) with a semicolon.
Integrals, sums and limits
\draw (0,0) -- (4,0);
(/learn
/Integrals,_sums_and_limits)
(/learn/File:Tiksline.png)
Display style in math
mode (/learn
/Display_style_in_math_mode) We can then add more co-ordinates in like this to make it a square:
List of Greek letters and
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
math symbols (/learn

2 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

/List_of_Greek_letters_and_math_symbols)
Mathematical fonts
(/learn
/Mathematical_fonts)

Figures and
tables (/learn

Inserting Images (/learn


/Inserting_Images)
Tables (/learn/Tables)
Positioning Images and
Tables (/learn
/Positioning_images_and_tables)
/File:Tikssquare.png)
Lists of Tables and
Figures (/learn However this isn't particularly good style. As we are drawing a line
/Lists_of_tables_and_�gures) that ends up in the same place we started, it is better to �nish the
Drawing Diagrams statement with the keyword cycle rather than the last co-ordinate.
Directly in LaTeX (/learn
\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;
/Picture_environment)
TikZ package (/learn To simplify this code further we can use the rectangle keyword
/TikZ_package)
after the starting co-ordinate and then follow it with the co-ordinate
of the corner diagonally opposite.
References and
Citations \draw (0,0) rectangle (4,4);

Bibliography We can also add lines that aren't straight. For example, this is how we
management in LaTeX draw a parabola:
(/learn
\draw (0,0) parabola (4,4);
/Bibliography_management_in_LaTeX)
Bibliography
management with
biblatex (/learn
/Bibliography_management_in_LaTeX)
Biblatex bibliography
styles (/learn
/Biblatex_bibliography_styles) (/learn
Biblatex citation styles
(/learn
/Biblatex_citation_styles)
Bibliography
management with natbib
(/learn
/File:Tiksparabola.png)
/Bibliography_management_with_natbib)
Natbib bibliography To add a curved line we use control points. We begin with our starting
styles (/learn co-ordinate, then use two dots followed by the keyword controls
/Natbib_bibliography_styles) and then the co-ordinates of our control points separated by an and.

3 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

Natbib citation styles Then after two more dots we have the �nal point. These control
(/learn points act like magnets attracting the line in their direction:
/Natbib_citation_styles)
\draw (0,0) .. controls (0,4) and (4,0) .. (4,4);
Bibliography
management with bibtex
(/learn
/Bibliography_management_with_bibtex)
Bibtex bibliography styles
(/learn
/Bibtex_bibliography_styles)
(/learn/File:Tikscurve.png)
Languages
International language
support (/learn
/International_language_support)
Quotations and
quotation marks (/learn
We can then add a circle like this. The �rst co-ordinate is the circle's
/Typesetting_quotations)
centre and the length in brackets at the end is the circle's radius:
Arabic (/learn/Arabic)
Chinese (/learn/Chinese)
\draw (2,2) circle (3cm);

French (/learn/French)
German (/learn/German)
Greek (/learn/Greek)
Italian (/learn/Italian)
Japanese (/learn
/Japanese)
Korean (/learn/Korean)
Portuguese (/learn
/Portuguese)
Russian (/learn/Russian)
Spanish (/learn/Spanish)

Document
structure
Sections and chapters
(/learn/File:Tiksnicecircle.png)
(/learn
/Sections_and_chapters) This is how we draw an ellipse. This time the lengths in the brackets
Table of contents (/learn separated by an and, are the x-direction radius and the y-direction
/Table_of_contents) radius respectively:
Cross referencing
sections and equations
\draw (2,2) ellipse (3cm and 1cm);
(/learn
/Cross_referencing_sections_and_equations)
Indices (/learn/Indices)

4 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

Glossaries (/learn
/Glossaries)
Nomenclatures (/learn
/Nomenclatures)
Management in a large
project (/learn
/Management_in_a_large_project) (/learn/File:Tiksellipse.png)
Multi-�le LaTeX projects
(/learn/Multi- This is how we draw an arc. In the �nal bracket we enter the starting
�le_LaTeX_projects) angle, the ending angle and the radius. This time they are separated
Hyperlinks (/learn by colons:
/Hyperlinks)
\draw (3,0) arc (0:75:3cm);

Formatting
Lengths in LATEX (/learn
/Lengths_in_LaTeX)
Headers and footers
(/learn/File:Tiksarcsegment.png)
(/learn
/Headers_and_footers)
Page numbering (/learn
/Page_numbering)
Paragraph formatting
(/learn To customise the way these lines are drawn we add extra arguments
/Paragraph_formatting)
into the \draw command. For example, we can edit the circle we
Line breaks and blank
drew so that the line is red, thick and dashed:
spaces (/learn
/Line_breaks_and_blank_spaces) \draw[red,thick,dashed] (2,2) circle (3cm);
Text alignment (/learn
/Text_alignment)
Page size and margins
(/learn
/Page_size_and_margins)
Single sided and double
sided documents (/learn
/Single_sided_and_double_sided_documents)
Multiple columns (/learn
/Multiple_columns)
Counters (/learn
/Counters)
Code listing (/learn
/Code_listing)
Code Highlighting with
minted (/learn
/Code_Highlighting_with_minted)
(/learn/File:Tiksredthickdashed.png)
Using colours in LaTeX

5 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

(/learn
/Using_colours_in_LaTeX)
Footnotes (/learn Grids
/Footnotes)
Margin notes (/learn
Very often when drawing diagrams we will want to draw a grid. To do
/Margin_notes) this we use the \draw command followed by by some additional
arguments. For example, we specify the grid step size using step=
Fonts and a length. We've also speci�ed the colour gray and told it to make

Font sizes, families, and


the lines very thin. After these arguments we enter the co-
styles (/learn ordinates of the bottom-left corner, followed by the keyword grid
/Font_sizes,_families,_and_styles) and then the co-ordinates of the top right-corner:
Font typefaces (/learn
\draw[step=1cm,gray,very thin] (-2,-2) grid (6,6);
/Font_typefaces)
Supporting modern fonts
with XƎLATEX (/learn
/XeLaTeX)

Presentations
Beamer (/learn/Beamer)
Powerdot (/learn
/Powerdot)
Posters (/learn/Posters)

Commands
Commands (/learn
/Commands)
Environments (/learn
/Environments)

Field speci�c
Theorems and proofs
(/learn/File:Tiksgrid1.png)
(/learn
/Theorems_and_proofs) If we want to remove the outer lines around this grid we can crop the
Chemistry formulae size slightly like this:
(/learn
/Chemistry_formulae) \draw[step=1cm,gray,very thin] (-1.9,-1.9) grid (5.9,5.9);

Feynman diagrams
(/learn
/Feynman_diagrams)
Molecular orbital
diagrams (/learn
/Molecular_orbital_diagrams)
Chess notation (/learn
/Chess_notation)

6 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

Knitting patterns (/learn


/Knitting_patterns)
CircuiTikz package
(/learn
/CircuiTikz_package)
Pgfplots package (/learn
/Pgfplots_package)
Typing exams in LaTeX
(/learn (/learn
/Typing_exams_in_LaTeX)
Knitr (/learn/Knitr)
Attribute Value Matrices
(/learn
/Attribute_Value_Matrices)

Class �les
Understanding packages
and class �les (/learn /File:Tiksgrid2.png)
/Understanding_packages_and_class_�les)
List of packages and class Colour �lling
�les (/learn
/List_of_packages_and_class_�les) Now lets add a shape onto our grid and colour it in. To do this we use
Writing your own the \fill command instead of the \draw command. Then in square
package (/learn brackets we enter a colour. For example, this speci�es a colour that is
/Writing_your_own_package) 40% blue mixed with 60% white. Then we just specify a closed shape
Writing your own class as we would normally:
(/learn
/Writing_your_own_class) \fill[blue!40!white] (0,0) rectangle (4,4);

Tips (/learn/Tips)

7 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

(/learn

/File:Tiks�ll.png)

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 �ll colour and a draw colour speci�ed:

\filldraw[fill=blue!40!white, draw=black] (0,0) rectangle (4,4);

(/learn

/File:Tiks�lldraw.png)

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

8 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

specify a left colour and a right colour:

\shade[left color=blue,right color=red] (0,0) rectangle (4,4);

(/learn

/File:Tiksshade1.png)

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);

(/learn

/File:Tiksshade2.png)

9 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

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);

(/learn

/File:Tiksshade3.png)

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

(/learn

/File:Tiksshadedraw.png)

10 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

Axes
Let's �nish 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);

(/learn

/File:Tiksplainarrows.png)

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 speci�cation 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


\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east

11 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

(/learn/File:Tiksnodes.png)

To �nish 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
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east

(/learn/File:Tiksforeach.png)

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

12 of 13 4/14/2019, 10:50 PM
LaTeX Graphics using TikZ: A Tutorial for Beginners (Part 1)—Basic ... https://www.overleaf.com/learn/latex/LaTeX_Graphics_using_TikZ:_A_...

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 (/learn
/LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_2)
%E2%80%94Generating_TikZ_Code_from_GeoGebra) we'll look
exporting TikZ code from GeoGebra (https://www.geogebra.org
/?lang=en-GB).

All articles in this series


Part 1: Basic Drawing;
Part 2: Generating TikZ Code from GeoGebra (/learn
/LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_2)
%E2%80%94Generating_TikZ_Code_from_GeoGebra);
Part 3: Creating Flowcharts (/learn
/LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_3)
%E2%80%94Creating_Flowcharts);
Part 4: Circuit Diagrams Using Circuitikz (/learn
/LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_4)
%E2%80%94Circuit_Diagrams_Using_Circuitikz);
Part 5: Creating Mind Maps (/learn
/LaTeX_Graphics_using_TikZ:_A_Tutorial_for_Beginners_(Part_5)
%E2%80%94Creating_Mind_Maps).

Please do keep in touch with us via Facebook


(https://www.facebook.com/overleaf.editor/), Twitter
(https://twitter.com/overleaf) or via e-mail on our contact us page
(https://www.overleaf.com/contact).

13 of 13 4/14/2019, 10:50 PM

Potrebbero piacerti anche