Learning Objectives:
Intermediate techniques of TikZ:
- Tree
- Grouping
- Plotting
- Calculate Coordinates
- For loop
Tree
General Tree
\begin{tikzpicture}
\node(0){a}
child{node{b}}
child{node{c}
child{node{d}}
child{node{e}}
};
\end{tikzpicture}
Game Tree
Since nodes in game trees are often defined by dots, we need to specify them.
\begin{tikzpicture}
\tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
\tikzstyle{solid node}=[circle,draw,inner sep=1.5,fill=black]
\node[hollow node]{}
child{node[solid node]{}}
child{node[solid node]{}}
child{node[solid node]{}}
;
\end{tikzpicture}
Grouping
We can apply decoration to multiple lines.
Scope
Using scope is to allow decorations to be applied to multiple lines.
\begin{tikzpicture}
\begin{scope} [red, thick]
\draw (0,0)--(3,3);
\draw (0,3)--(3,0);
\end{scope}
\end{tikzpicture}
Style
Using style is to allow decorations to be applied with flexibility:
\begin{tikzpicture}[marked/.style = {red, very thick}]
\draw[marked] (0,0)--(3,3);
\draw[marked] (0,3)--(3,0);
\end{tikzpicture}
tikzset
Using style is to allow decorations to be applied to multiple figures.
\tikzset[marked/.style = {red, very thick}]
\tikzset[trivial/.style = {blue, thin}]
\begin{tikzpicture}
\draw[marked] (0,0)--(3,3);
\draw[trivial] (0,3)--(3,0);
\end{tikzpicture}
Plotting
Mathematical Expression
\begin{tikzpicture}
\draw[blue,<->] (4,0) -| (0,4);
\draw[red, domain=0:2] plot (\x,\x*\x);
\end{tikzpicture}
Data plot
For data plot, we need to load the package \usepackage{pgfplots}
.
\usepackage{tikz}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[xlabel=x,ylabel=y]
\addplot coordinates {(1,1) (2,0) (3,3) (4,2)};
\end{axis}
Scale
Zoom out
We can use scale=0.5
to make the zoom out.
\begin{tikzpicture}[scale=0.5]
\draw (0,0)--(1,1);
\draw (0,1)--(1,0);
\end{tikzpicture}
Zoom in
We can use scale=2
to make the zoom in.
\begin{tikzpicture}[scale=2]
\draw (0,0)--(1,1);
\draw (0,1)--(1,0);
\end{tikzpicture}
Find coordinates
Grid
One of the main difficulties in using TikZ to draw diagram is to find the appropriate coordinates. One way to see where is should put the coordinate is by using grid
\begin{tikzpicture}
\draw[step=1,gray,very thin] (0,0) grid (4,4);
\end{tikzpicture}
Relative to two points
To calculate new coordinates based on $(a,b)$ and $(c,d)$, we can use |-
and -|
. We have $(a,d)$ if we have (a,b|-c,d)
and $(c,b)$ if we have (a,b-|c,d)
\begin{tikzpicture}
\node[circle, fill=red] at (1,1) {};
\node[circle, fill=blue] at (3,3){};
\node[circle, fill=green] at (1,1|- 3,3){};
\node[circle, fill=purple] at (1,1-| 3,3){};
\end{tikzpicture}
Average of two points
To calculate mid point of based on $(a,b)$ and $(c,d)$, we can use ($(a,b)!0.5!(c,d)$)
.
\begin{tikzpicture}
\node[circle, fill=red] at (1,1) {};
\node[circle, fill=blue] at (3,3){};
\node[circle, fill=green] at ($(1,1)!0.5!(3,3)$){};
\node[circle, fill=purple] at ($(1,1)!0.25!(3,3)$){};
\end{tikzpicture}
Intersection of two named paths
To calculate in intersections, we need to use \usetikzlibrary{intersections}
.
\usetikzlibrary{intersections}
\begin{tikzpicture}
\draw[name path=Demand] (0,3) -- (3,0);
\draw[name path=Supply] (0,0) -- (3,3);
\path [name intersections={of=Demand and Supply,by=E}];
\fill [red] (E) circle (2pt);
\end{tikzpicture}
Multiple intersections of two named paths
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{tikzpicture}
\draw[name path=circle] (0,0) circle (2);
\draw[name path=line] (-3,-3) -- (3,3);
\fill [red, name intersections={of = circle and line}] (intersection-1) circle (4pt);
\fill [blue, name intersections={of =circle and line}] (intersection-2) circle (4pt);
\end{tikzpicture}
Example: Demand and Supply
We illustrate how to draw demand and supply curve using the techniques presented above:
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{tikzpicture}[scale=0.8]
\draw [thick](0,6) node [left] {P} |- (6,0) node [below] {Q};
\draw[thick,blue!90, name path =Supply](1,1)--(5,5) node[right]{Supply};
\draw[thick,blue!90, name path=Demand](1,5)--(5,1) node[right]{Demand};
\path [name intersections={of=Demand and Supply,by=E}];
\draw[dashed] (E -| 0,0) node[left]{$P^*$}--(E)--(E |- 0,0) node[below]{$Q^*$};
\draw[black,fill] (E) circle [radius=0.15];
\end{tikzpicture}
Programming
For Loop
The syntax to write a for loop is \foreach
and variable would be \x
.
\draw (0,0)--(6,0);
\foreach \x in {0,...,6}{
\draw (\x,-1)--(\x,1);
}