12.06.2013 Views

Using TIKZ - College of the Redwoods

Using TIKZ - College of the Redwoods

Using TIKZ - College of the Redwoods

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Using</strong> <strong>TIKZ</strong><br />

David Arnold<br />

<strong>College</strong> <strong>of</strong> <strong>the</strong> <strong>Redwoods</strong><br />

Department <strong>of</strong> Ma<strong>the</strong>matics<br />

November 20, 2010<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 1 / 24


<strong>Using</strong> Tikz<br />

Open <strong>the</strong> file WorkshopFiles/workshop8.tex in TeXShop. Note <strong>the</strong><br />

new line \usepackage{tikz}, which loads <strong>the</strong> tikz package.<br />

\ d o c u m e n t c l a s s { a r t i c l e }<br />

\ usepackage [ n o s o l u t i o n s , p o i n t s o n l e f t ] { eqexam}<br />

\ usepackage {amsmath}<br />

\usepackage{tikz}<br />

\ t i t l e {Exam \#2A}<br />

\ s u b j e c t {Math 120}<br />

\ a u t h o r { David Arnold }<br />

\ date {November 20 , 2010}<br />

Note also <strong>the</strong> new title and subject.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 2 / 24


Exam Environment plus Instructions<br />

There is a new exam environment and instructions. Compile.<br />

\ b e g i n { document }<br />

\ m a k e t i t l e<br />

\ b e g i n {exam}{Exam2A}<br />

\ b e g i n { i n s t r u c t i o n s }<br />

Place <strong>the</strong> s o l u t i o n to each problem i n <strong>the</strong> space<br />

p r o v i d e d .<br />

\ end { i n s t r u c t i o n s }<br />

\ end {exam}<br />

\ end { document }<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 3 / 24


Create a Problem Environment<br />

After <strong>the</strong> instructions environment, create a new problem environment.<br />

Include a solution environment, reserving 3 inches <strong>of</strong> space for <strong>the</strong> user.<br />

\ b e g i n { problem } [ 1 0 ] On <strong>the</strong> g r i d t h a t f o l l o w s , s k e t c h<br />

<strong>the</strong> graph o f $y=2x +1$.<br />

\ b e g i n { s o l u t i o n } [ 3 i n ]<br />

S o l u t i o n h e r e .<br />

\ end { s o l u t i o n }<br />

\ end { problem }<br />

After you enter <strong>the</strong> problem and solution code, recompile workshop8.tex.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 4 / 24


Create a Grid<br />

After <strong>the</strong> problem statement (and before <strong>the</strong> solution environment), enter<br />

<strong>the</strong> grid code.<br />

\ b e g i n { c e n t e r }<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ gray ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw[, l i n e width=1pt ] ( −5 ,0) −−(5,0) node [ r i g h t<br />

] { $ x $ } ;<br />

\ draw[, l i n e width=1pt ] (0 , −5) −−(0,5) node [ above<br />

] { $ y $ } ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { c e n t e r }<br />

After entering <strong>the</strong> grid code, recompile workshop8.tex.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 5 / 24


Scaling <strong>the</strong> Grid<br />

You can scale <strong>the</strong> grid as follows.<br />

\ b e g i n { c e n t e r }<br />

\ b e g i n { t i k z p i c t u r e } [ scale=0.5 ]<br />

\ draw [ gray ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw[, l i n e width=1pt ] ( −5 ,0) −−(5,0) node [ r i g h t<br />

] { $ x $ } ;<br />

\ draw[, l i n e width=1pt ] (0 , −5) −−(0,5) node [ above<br />

] { $ y $ } ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { c e n t e r }<br />

After you enter <strong>the</strong> scaling adjustment, recompile workshop8.tex.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 6 / 24


Crafting a Solution<br />

Copy <strong>the</strong> grid code into <strong>the</strong> solution environment.<br />

\ b e g i n { s o l u t i o n } [ 3 i n ]<br />

\ b e g i n { c e n t e r }<br />

\ b e g i n { t i k z p i c t u r e } [ scale=0.5 ]<br />

\ draw [ gray ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw[, l i n e width=1pt ] ( −5 ,0) −−(5,0) node [ r i g h t<br />

] { $ x $ } ;<br />

\ draw[, l i n e width=1pt ] (0 , −5) −−(0,5) node [ above<br />

] { $ y $ } ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { c e n t e r }<br />

\ end { s o l u t i o n }<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 7 / 24


Drawing <strong>the</strong> Solution Line<br />

We want to draw <strong>the</strong> graph <strong>of</strong> <strong>the</strong> line y = 2x + 1 on <strong>the</strong> grid. Use <strong>the</strong><br />

\draw command to do this. Add this single line immediately after <strong>the</strong> code<br />

that creates <strong>the</strong> grid and just before <strong>the</strong> \end{tikzpicture} command.<br />

\ draw[, l i n e width=1pt , b l u e ] (−3,−5) −−(2,5) node [<br />

above r i g h t ] { $ y=2x +1$};<br />

\ end { t i k z p i c t u r e }<br />

After you enter <strong>the</strong> code to draw <strong>the</strong> graph <strong>of</strong> <strong>the</strong> line y = 2x + 1,<br />

recompile workshop8.tex. Nothing happens because we haven’t yet<br />

added <strong>the</strong> solutionsafter option.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 8 / 24


Change <strong>the</strong> Preamble<br />

To see solutions, use <strong>the</strong> option solutionsafter in <strong>the</strong> preamble.<br />

\ d o c u m e n t c l a s s { a r t i c l e }<br />

% Uncomment to p r o h i b i t s o l u t i o n s and p r o v i d e space<br />

% f o r s t u d e n t work<br />

% \ usepackage [ n o s o l u t i o n s , p o i n t s o n l e f t ] { eqexam}<br />

% Uncomment to p r o v i d e s o l u t i o n s<br />

\ usepackage [ s o l u t i o n s a f t e r , p o i n t s o n l e f t ] { eqexam}<br />

Recompile workshop8.tex. The solution is now visible.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 9 / 24


The Workarea<br />

Open Workshopfiles/workshop9.tex in TeXShop. We moved <strong>the</strong> grid<br />

code after problem statement into a workarea environment.<br />

\ b e g i n { workarea }{\ sameVspace }<br />

\ b e g i n { c e n t e r }<br />

\ b e g i n { t i k z p i c t u r e } [ s c a l e =0.5]<br />

\ draw [ gray ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw[, l i n e width=1pt ] ( −5 ,0) −−(5,0) node [ r i g h t<br />

] { $ x $ } ;<br />

\ draw[, l i n e width=1pt ] (0 , −5) −−(0,5) node [ above<br />

] { $ y $ } ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { c e n t e r }<br />

\ end { workarea }<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 10 / 24


Solutions After versus No Solutions<br />

Compile workshop9.tex with:<br />

\ usepackage [ n o s o l u t i o n s , p o i n t s o n l e f t ] { eqexam}<br />

In this case, <strong>the</strong> grid in <strong>the</strong> workarea environment will be<br />

superimposed on <strong>the</strong> white space provided for student work.<br />

Compile workshop9.tex with:<br />

\ usepackage [ s o l u t i o n s a f t e r , p o i n t s o n l e f t ] { eqexam}<br />

In this case, <strong>the</strong> workarea (whitespace for student work) is suppressed<br />

and only <strong>the</strong> solution appears.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 11 / 24


Plotting Functions<br />

We’re now going to plot <strong>the</strong> graph <strong>of</strong> <strong>the</strong> function f (x) = x(x + 2)(x − 2).<br />

Open WorkshopFiles/workshop10.tex in TeXShop and compile.<br />

\ d o c u m e n t c l a s s { a r t i c l e }<br />

\ usepackage { t i k z }<br />

\ b e g i n { document }<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ draw [ t h i c k , b l u e ] p l o t (\ x , { \ x ∗(\ x+2) ∗(\ x −2) }) ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { document }<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 12 / 24


Restricting <strong>the</strong> Domain<br />

You can restrict <strong>the</strong> domain <strong>of</strong> <strong>the</strong> function by adding <strong>the</strong> option<br />

domain=-3:3.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ draw [ t h i c k , blue , domain=-3:3 ] p l o t (\ x , { \ x ∗(\ x+2) ∗(\ x<br />

−2) }) ;<br />

\ end { t i k z p i c t u r e }<br />

Recompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 13 / 24


<strong>Using</strong> Matlab to Restrict <strong>the</strong> Domain<br />

You can your calculator or a s<strong>of</strong>tware program such as MATLAB to<br />

restrict <strong>the</strong> domain so that <strong>the</strong> range is restricted to [−5, 5].<br />

syms x<br />

f=x ∗( x+2) ∗( x −2) ;<br />

s o l=s o l v e ( f −5,x ) ;<br />

double ( s o l )<br />

s o l=s o l v e ( f +5,x ) ;<br />

double ( s o l )<br />

ans =<br />

2.4567<br />

ans =<br />

−2.4567<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 14 / 24


The Ideal Domain<br />

Use <strong>the</strong> results from MATLAB to set domain=-2.4567:2.4567.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ draw [ t h i c k , blue , domain=-2.4567:2.4567 ] p l o t (\ x , { \ x ∗(\<br />

x+2) ∗(\ x −2) }) ;<br />

\ end { t i k z p i c t u r e }<br />

Recompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 15 / 24


Clipping Rectangle<br />

A much simpler solution is to establish a clipping region. Use <strong>the</strong> scope<br />

environment so that <strong>the</strong> clipping region affects only <strong>the</strong> graph <strong>of</strong> <strong>the</strong><br />

polynomial.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ b e g i n { scope }<br />

\ c l i p (−5,−5) r e c t a n g l e ( 5 , 5 ) ;<br />

\ draw [ t h i c k , b l u e ] p l o t (\ x , { \ x ∗(\ x+2) ∗(\ x −2) }) ;<br />

\ end { scope }<br />

\ end { t i k z p i c t u r e }<br />

Recompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 16 / 24


Smoothing<br />

Use <strong>the</strong> smooth option to cure <strong>the</strong> jaggedness <strong>of</strong> <strong>the</strong> curve.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ b e g i n { scope }<br />

\ c l i p (−5,−5) r e c t a n g l e ( 5 , 5 ) ;<br />

\ draw [ t h i c k , blue , smooth ] p l o t (\ x , { \ x ∗(\ x+2) ∗(\ x −2)<br />

}) ;<br />

\ end { scope }<br />

\ end { t i k z p i c t u r e }<br />

Recompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 17 / 24


Adding Samples to Smooth <strong>the</strong> Curve<br />

Use <strong>the</strong> samples=100 option to cure <strong>the</strong> jaggedness <strong>of</strong> <strong>the</strong> curve.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw [ h e l p l i n e s ] (−5,−5) g r i d ( 5 , 5 ) ;<br />

\ draw [ t h i c k ,] ( −5 ,0) −−(5,0) node [ r i g h t ] {$ x $ } ;<br />

\ draw [ t h i c k ,] (0 , −5) −−(0,5) node [ above ] {$ y $ } ;<br />

\ b e g i n { scope }<br />

\ c l i p (−5,−5) r e c t a n g l e ( 5 , 5 ) ;<br />

\ draw [ t h i c k , blue , samples=100 ] p l o t (\ x , { \ x ∗(\ x+2) ∗(\<br />

x −2) }) ;<br />

\ end { scope }<br />

\ end { t i k z p i c t u r e }<br />

Recompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 18 / 24


Annotating <strong>the</strong> Plot<br />

Use <strong>the</strong> \node command to annotate <strong>the</strong> plot. Add <strong>the</strong> following lines to<br />

your source code.<br />

\ node [ below ] at ( −5 ,0) {$ −5$};<br />

\ node [ below ] at ( 5 , 0 ) {$5$};<br />

\ node [ l e f t ] at (0 , −5) {$ −5$};<br />

\ node [ l e f t ] at ( 0 , 5 ) {$5$};<br />

\ node [ above r i g h t , b l u e ] at ( 2 . 5 , 5 ) {$ f $ } ;<br />

Reompile workshop10.tex and note <strong>the</strong> difference.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 19 / 24


Drawing Diagrams<br />

Open <strong>the</strong> file WorkshopFiles/workshop11.tex and compile. A rectangle<br />

is drawn.<br />

\ d o c u m e n t c l a s s { a r t i c l e }<br />

\ usepackage { t i k z }<br />

\ b e g i n { document }<br />

\ b e g i n { c e n t e r }<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw ( 0 , 0 ) −−(6,0) −−(6,4) −−(0,4) −−(0,0) ;<br />

\ end { t i k z p i c t u r e }<br />

\ end { c e n t e r }<br />

\ end { document }<br />

In tikz one unit equals one centimeter. This default can be changed.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 20 / 24


Annotating <strong>the</strong> Diagram<br />

Use <strong>the</strong> node command to add annotations. Add <strong>the</strong> following and<br />

recompile workshop11.tex.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw ( 0 , 0 ) − − node [ below ] {$L$} ( 6 , 0 ) −−(6,4)<br />

−−(0,4) −−(0,0) ;<br />

\ end { t i k z p i c t u r e }<br />

The node command has a variety <strong>of</strong> options:<br />

node[below], node[above], node[left], node[right]<br />

Label <strong>the</strong> remaining sides <strong>of</strong> <strong>the</strong> rectangle. Let <strong>the</strong> width or height be<br />

represented by W and <strong>the</strong> length be represented by L.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 21 / 24


Positioning <strong>the</strong> Nodes<br />

Open <strong>the</strong> file WorkshopFiles/workshop12.tex and compile. A rectangle<br />

is drawn. Add <strong>the</strong> following node.<br />

\ b e g i n { t i k z p i c t u r e }<br />

\ draw ( 0 , 0 ) node [ below l e f t ] {$A$} − − ( 6 , 0 ) −−(6,4)<br />

−−(0,4) −−(0,0) ;<br />

\ end { t i k z p i c t u r e }<br />

Here are some fur<strong>the</strong>r options for nodes.<br />

node[below left], node[below right], node[above left],<br />

node[above right]<br />

Label <strong>the</strong> vertices <strong>of</strong> <strong>the</strong> rectangle A, B, C, and D and recompile.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 22 / 24


Geogebra<br />

A<br />

B<br />

D<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 23 / 24<br />

C


Tkz-Euclide and Tkz-Fct<br />

In France, Alain Mat<strong>the</strong>s is currently developing two packages that use<br />

fairly intuitive syntax when drawing geometrical figures and plotting<br />

functions. The manuals are written in French, but <strong>the</strong> examples are in<br />

English.<br />

The first package is called tkz-euclide. This is an exciting project that<br />

entails numerous macros for creating geometrical figures.<br />

The second package is called tkz-fct. This package <strong>of</strong> macros greatly<br />

simplifies <strong>the</strong> plotting <strong>of</strong> functions.<br />

David Arnold (CR) <strong>Using</strong> <strong>TIKZ</strong> November 20, 2010 24 / 24

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!