\mathrm{\LaTeX} has the capability to draw beautiful graphics. This feature is possible with TikZ package. Here is the plot of f(x) = x,
In \mathrm{\LaTeX}, everything has to be coded. From axes, to labels, to points on the xy-plane; that explains why four lines of codes, only for single, very simple plot.
To start sketching, one has to enclose the drawing inside the
The first four lines of the script are almost the same with the previous one above, except for the option,
Here are other examples taken from TikZ and PGF manual,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{tikzpicture}[domain = 0:5] | |
\draw[->] (-1,0) -- (5.5,0) node[right] {x}; | |
\draw[->] (0,-1) -- (0,5.5) node[right] {f(x)}; | |
\draw[color = red, thick] (0,0) -- (1,1) -- (2,2) -- (3,3) -- (4,4) -- (5,5); | |
\end{tikzpicture} |

To start sketching, one has to enclose the drawing inside the
tikzpicture
environment with options, for this case domain
-- the domain of x. On second and third line, the codes draw the x and y axes; with usage,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\draw[options] (x1,y1) -- (x2,y2) node[node alignment] {label}; |
options
above is set to ->
, which is the type of line, that is, with pointed end. (x1,y1)
and (x2,y2)
are cartesian coordinates for the line segment. Finally, the fourth line connects the xy-points: \{(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)\}.
As witnessed, \mathrm{\LaTeX} has no capability when it comes to plotting functions, since one has to manually enter the points computed from the function to the draw output. However, \mathrm{\LaTeX} can call external program and do the plotting. The said program is called gnuplot. This program has to be installed first, and then include this as option on \mathrm{\LaTeX} compiler. For example on my pdflatex, I include this, --shell-escape
. For Texmaker users, go to Options > Configure Texmaker, on PdfLaTeX insert --shell-escape
. See photo below,
With this option, \mathrm{\LaTeX} can finally call the gnuplot and do the computation on functions to obtain the xy-points that is needed by TikZ for plotting. For example, here are the plots of the functions: f(x) = x; g(x) = x^2; and h(x) = \sin(x).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{tikzpicture}[domain = 0:4.5, scale = 1.5] | |
\draw[gray, very thin] (-0.1,-0.1) grid (4.3,4.3); | |
\draw[->] (-0.4,0) -- (4.5,0) node[right] {x}; | |
\draw[->] (0,-0.4) -- (0,4.5) node[right] {y}; | |
\foreach \x/\xtext in {1/1, 2/2, 3/3, 4/4} | |
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\xtext}; | |
\foreach \y/\ytext in {1/1, 2/2, 3/3, 4/4} | |
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\ytext}; | |
\draw (-6pt,-7pt) node {0}; | |
\draw[color = red, thick] (0,0) plot[id = x] function{x} node[right] {x}; | |
\draw[color = green, thick, domain = 0:2.2] (0,0) plot[id = x2] function{x**2} node[right] {x^2}; | |
\draw[color = blue, thick] (0,0) plot[id = sinx] function{sin(x)} node[right] {\sin(x)}; | |
\end{tikzpicture} |

scale
, which scales the x and y axes; and the second line, that draws the grid on the graph. The next four lines are loops, that generate the ticks of the two axes. Line 9, is a label for tick 0. The proceeding lines, plot the three functions. Unlike before, the xy-points are computed by the gnuplot, from the statement plot[id] function{...}
.Here are other examples taken from TikZ and PGF manual,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{tikzpicture} | |
\draw[fill=yellow] (0,0) -- (60:.75cm) arc (60:180:.75cm); | |
\draw(120:0.4cm) node {\alpha}; | |
\draw[fill=green!30] (0,0) -- (right:.75cm) arc (0:60:.75cm); | |
\draw(30:0.5cm) node {\beta}; | |
\begin{scope}[shift={(60:2cm)}] | |
\draw[fill=green!30] (0,0) -- (180:.75cm) arc (180:240:.75cm); | |
\draw (30:-0.5cm) node {\gamma}; | |
\draw[fill=yellow] (0,0) -- (240:.75cm) arc (240:360:.75cm); | |
\draw (-60:0.4cm) node {\delta}; | |
\end{scope} | |
\begin{scope}[thick] | |
\draw (60:-1cm) node[fill=white] {E} -- (60:3cm) node[fill=white] {F}; | |
\draw[red] (-2,0) node[left] {A} -- (3,0) node[right]{B}; | |
\draw[blue,shift={(60:2cm)}] (-3,0) node[left] {C} -- (2,0) node[right]{D}; | |
\draw[shift={(60:1cm)},xshift=4cm] node [right,text width=6cm,rounded corners,fill=red!20,inner sep=1ex] | |
{ | |
When we assume that \color{red}AB and \color{blue}CD are | |
parallel, i.\,e., {\color{red}AB} \mathbin{\|} \color{blue}CD, | |
then \alpha = \delta and \beta = \gamma. | |
}; | |
\end{scope} | |
\end{tikzpicture} |

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{tikzpicture}[scale=2] | |
\shade[top color=blue,bottom color=gray!50] (0,0) parabola (1.5,2.25) |- (0,0); | |
\draw (1.05cm,2pt) node[above] {\displaystyle\int_0^{3/2} \!\!x^2\mathrm{d}x}; | |
\draw[style=help lines] (0,0) grid (3.9,3.9) | |
[step=0.25cm] (1,2) grid +(1,1); | |
\draw[->] (-0.2,0) -- (4,0) node[right] {x}; | |
\draw[->] (0,-0.2) -- (0,4) node[above] {f(x)}; | |
\foreach \x/\xtext in {1/1, 1.5/1\frac{1}{2}, 2/2, 3/3} | |
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\xtext}; | |
\foreach \y/\ytext in {1/1, 2/2, 2.25/2\frac{1}{4}, 3/3} | |
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\ytext}; | |
\draw (-.5,.25) parabola bend (0,0) (2,4) node[below right] {x^2}; | |
\end{tikzpicture} |

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\begin{tikzpicture} | |
\draw[gray,very thin] (-1.9,-1.9) grid (2.9,3.9) | |
[step=0.25cm] (-1,-1) grid (1,1); | |
\draw[blue] (1,-2.1) -- (1,4.1); % asymptote | |
\draw[->] (-2,0) -- (3,0) node[right] {x(t)}; | |
\draw[->] (0,-2) -- (0,4) node[above] {y(t)}; | |
\foreach \pos in {-1,2} | |
\draw[shift={(\pos,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\pos}; | |
\foreach \pos in {-1,1,2,3} | |
\draw[shift={(0,\pos)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\pos}; | |
\fill (0,0) circle (0.064cm); | |
\draw[thick,parametric,domain=0.4:1.5,samples=200] plot[id=asymptotic-example] function{(t*t*t)*sin(1/(t*t*t)),(t*t*t)*cos(1/(t*t*t))} node[right] {\bigl(x(t),y(t)\bigr) = (t\sin \frac{1}{t}, t\cos \frac{1}{t})}; | |
\fill[red] (0.63662,0) circle (2pt) node [below right,fill=white,yshift=-4pt] {(\frac{2}{\pi},0)}; | |
\end{tikzpicture} |
