Graphing in LATEX.
In this section we will take a look at how to draw graph
in Latex. this can be easily made in Latex, it's simple, fast, and the result
is compact and clean. To draw a graph we will need to use two packages: TikZ and PGF,
so before starting don't forget to add them to the Latex document.
To add the packages write the following line before the start of the document
CODE
\usepackage{tikz}
\usepackage{pgfplot}
Drawing a Cartesian plane.
For drawing a Cartesian plane we need firs to create the environment
for the code. ...
CODE
\begin{tikzpicture}
\begin{axis}[option of the axis]
...
\end{axis}
\end{tikzpicture}
We can customize the axis by adding commands between the parentesis:
[option1, option2, option3,...]. We can choose the maximum and the minimal value to
be displayed in the x and y axis, we can also choose where to place ticks marks. Using
the commands: xmax=value ,xmin=value we can set the x axis, with ymax and ymax we set the
y axis. With only those commands it will be displayed a empty rectangle, to put the x and y
axis in the middle we ca use: axis x line=middle, axis y line=middle in the option parentesis.
For example if we want to display a cartesian plane where the axis x and y
go from -5 to 5 we can write:
CODE
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5, axis x line=middle, axis y line=middle]
\end{axis}
\end{tikzpicture}
Adding equation.
After setting the Cartesian plane we can add a function, Latex
will draw it automaticaly on the plane, to add an equation we simply add the
command line: \addplot[option]{function}.
Inside the [option] parentesis we can choose the domain of the function: [domain=-4:5],
now the domain will go from -4 to 5. If we want to display the graph of the function
F(x)=2x+2 we simply use the following code:
CODE
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5, axis x line=middle, axis y line=middle]
\addplot[domain=-5:5]{2*x+2};
\end{axis}
\end{tikzpicture}
We can also add more than one function in a single graph, to do this we just add another
\addplot[]{}; in the same environment. When adding multiple function in a graph is better
to distinguish them, Latex allow us to choose a color for each function. Now we will add
two more function, one with a red line and one with a blue line:
CODE
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5, axis x line=middle, axis y line=middle]
\addplot[domain=-5:5]{2*x+2};
\addplot[domain=-5:5, color=red]{2*x+2};
\addplot[domain=-5:5, color=blue]{2*x+2};
\end{axis}
\end{tikzpicture}
Adding points.
In the plane we can also add points by simply choosing the style
and the coordinates of each point, we use the code: \addplot[option]coordinates {(2,3)};
To draw multiples dot connected by a line write: \addplot[mark=*]{(1,2)(3,4)(-2,5)}; Instead
if we want multiple dots not connected between them, we have to make a \addplot[]{} for each
point.So if we want to add four dot in the intersection of our function the code will
be:
CODE
\begin{tikzpicture}
\begin{axis}[xmin=-5, xmax=5, ymin=-5, ymax=5, axis x line=middle, axis y line=middle]
\addplot[domain=-5:5]{2*x+2};
\addplot[domain=-5:5, color=red]{2*x+2};
\addplot[domain=-5:5, color=blue]{2*x+2};
\addplot[matk=*] coordinates {(2,2)};
\addplot[matk=*] coordinates {(0,0)};
\addplot[matk=*] coordinates {(-2,-2)};
\addplot[matk=*] coordinates {(0,2)};
\end{axis}
\end{tikzpicture}
Adding text.
We can even add a title to our graph, we just need to
add: [title=Title goes here] after the: \begin{axis}. the code will look
like this:
CODE
\begin{tikzpicture}
\begin{axis}[title=Here goes the title]
\addplot[domain=0:5]{x+2};
\end{axis}
\end{tikzpicture}
Exercise.
Now that you know the basic try to draw the function:
F(x)=x^2 -4 and
put 2 dot where the function intersect with the x axis.
For the solution move the mouse over the black box on the right side.
\begin{tikzpicture}
\begin{axis}[xmax=5, xmin=-5, ymax=5, ymin=-5, axis x line=middle, axis y line=middle]
\addplot[domain=-5:5]{x^2-4};
\addplot[mark=*] coordinates {(-2,0)};
\addplot[mark=*] coordinates {(2,0)};
\end{axis}
\end{tikzpicture}