\documentclass[10pt]{report}

\usepackage[utf8]{inputenc}

\usepackage{geometry}
\geometry{hmargin=2.5cm,vmargin=2.5cm}

\usepackage{amssymb,amsmath,amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{center}
{\huge\bf Tikz Worksheet}
\end{center}

Include the package: {\tt {\textbackslash}usepackage\{pgfplots\}}

Tikz pictures are in the environment: {\tt {\textbackslash}begin\{tikzpicture\} ... {\textbackslash}end\{tikzpicture\}}

\vspace{1em}

{\large\bf Lines and Circles}

\begin{itemize}
\item a line from (1,1) to (3,3): {\tt {\textbackslash}draw (1,1) -- (3,3);}
\item two lines after each other: {\tt {\textbackslash}draw (1,1) -- (3,3) -- (4,2);}
\item a circle with center (1,1) and radius 2: {\tt {\textbackslash}draw (1,1) circle (2);}
\end{itemize}

Draw the following picture: 
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle;
\draw (1,1) circle (1);
\end{tikzpicture}

\vspace{1em}

{\large\bf 3 dimensional}

\begin{itemize}
\item Use three dimensional points, e.g. (3,1,0)
\item For different line styles, e.g.: {\tt {\textbackslash}draw[dotted]} or {\tt {\textbackslash}draw[dashed]}
\item For different line thickness, e.g.: {\tt {\textbackslash}draw[thick]} or {\tt {\textbackslash}draw[thin]}
\item You can combine line style and thickness, e.g.: {\tt {\textbackslash}draw[thick,dotted]}
\end{itemize}

Draw the following picture: 
\begin{tikzpicture}
\draw[dotted] (0,3,0) -- (0,0,0) -- (6,0,0);
\draw (6,0,0) -- (6,3,0) -- (0,3,0);
\draw (0,0,2) -- (6,0,2) -- (6,3,2) -- (0,3,2) -- cycle;
\draw[dotted] (0,0,0) -- (0,0,2);
\draw (6,0,0) -- (6,0,2);
\draw (0,3,0) -- (0,3,2);
\draw (6,3,0) -- (6,3,2);
\end{tikzpicture}

\vspace{1em}

{\large\bf Functions}

\begin{itemize}
\item Use the {\tt axis} environment inside the {\tt tikzpicture} environment.
\item To draw a function: {\tt {\textbackslash}addplot \{2*x\};}
\item Range of $x$- and $y$-values: The {\tt axis} environment takes options, e.g.: \\
{\tt {\textbackslash}begin\{axis\}[xmin=-2.5,xmax=2.5,ymin=-2.5,ymax=2.5,]}
\item After each plotted function add a legend entry for it, e.g.: {\tt {\textbackslash}addlegendentry\{2x\}}
\item To position the legend, add the following as an option in the {\tt axis} environment: {\tt legend pos=north west,}
\end{itemize}

Draw the following graph: 
\begin{tikzpicture}
\begin{axis}[
    xmin=-2.5,xmax=2.5,ymin=-2.5,ymax=2.5,
    legend pos=north west,
    width=0.4\textwidth,height=0.4\textwidth,
]
\addplot[domain=-2.5:2.5,blue] {x^3 - x};
\addlegendentry{$x^3-x$};
\addplot[domain=-2.5:2.5,red] {sin(deg(x))};
\addlegendentry{$\sin(x)$};
\end{axis}
\end{tikzpicture}

\vspace{1em}

{\large\bf Shaded area}

\begin{itemize}
\item Include the tikzlibrary: {\tt {\textbackslash}usepgfplotslibrary\{fillbetween\}}
\item Give each function a name, for that add the option {\tt name path=F} to {\tt {\textbackslash}addplot[...]}
\item To fill the area between the two graphs: {\tt {\textbackslash}addplot[color=gray,fill=gray,fill opacity=0.5] fill between [of=F and G, soft clip=\{domain=-1:2.5\}];}
\end{itemize}

Draw the following graph:
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel={$x$}, ylabel={$y$},
    xmin=-2.5,xmax=2.5,ymin=-2.5,ymax=2.5,
    width=0.4\textwidth,height=0.4\textwidth,
]
\addplot[name path=F,domain=-2.5:2.5] {x^2-1/2*x-2};
\addplot[name path=G,domain=-2.5:2.5] {-1/2*x+1};
\addplot[color=gray,fill=gray,fill opacity=0.5] fill between [of=F and G, soft clip={domain=-1.73:1.73}];
\end{axis}
\end{tikzpicture}

\vspace{1em}

{\large\bf 3 dimensional surfaces}

\begin{itemize}
\item Draw a surface: {\tt {\textbackslash}addplot3[surf,opacity=0,color=blue,fill opacity=0.2,z buffer=sort,domain=0:2*pi,y domain=0:1,samples=20] (\{cos(deg(x))*sin(deg(y))\},\{sin(deg(x))*sin(deg(y))\},\{cos(deg(y))\});}
\item To make part of the axis dashed, make the axis disappear {\tt axis line style=\{draw=none\}} and then draw them by hand
\end{itemize}

Draw the following graph:
\begin{tikzpicture}
\begin{axis}[
    width=0.45\textwidth, height=0.45\textwidth,
    axis lines = center,
    xlabel={$x$},ylabel={$y$},zlabel={$z$},
    view={105}{15},
	trig format plots=rad,
 	xmin=-1.5,xmax=1.5,ymin=-1.5,ymax=1.5,zmin=-0.5,zmax=1.5,
	xtick={-1,1},ytick={-1,1},ztick={-1,1},
	axis line style={draw=none}
]

\addplot3[surf,opacity=0,color=blue,fill opacity=0.2,z buffer=sort,domain=0:2*pi,y domain=0:1,samples=20] ({cos(x)},{sin(x)},{y});
\draw[dashed] (-1.5,0,0) -- (1,0,0);
\draw[-stealth] (1,0,0) -- (1.5,0,0);
\draw (0,-1.5,0) -- (0,-1,0);
\draw[dashed] (0,-1,0) -- (0,1,0);
\draw[-stealth] (0,1,0) -- (0,1.5,0);
\draw (0,0,-1.5) -- (0,0,-0.25);
\draw[dashed] (0,0,-0.25) -- (0,0,0.75);
\draw[-stealth] (0,0,0.75) -- (0,0,1.5);
\end{axis}
\end{tikzpicture}

\end{document}
