\documentclass[11pt, oneside]{article} % use "amsart" instead of "article" for AMSLaTeX format \usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots. \geometry{letterpaper} % ... or a4paper or a5paper or ... %\geometry{landscape} % Activate for for rotated page geometry %\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent \usepackage{graphicx} % Use pdf, png, jpg, or epsĀ§ with pdflatex; use eps in DVI mode % TeX will automatically convert eps --> pdf in pdflatex \usepackage{amssymb,amsmath} \usepackage{subcaption} \usepackage{tikz} \usetikzlibrary{calc} \usepackage{verbatim} \begin{document} % HELPERS: % %The most common error you'll see is almost certainly a lack of semicolons. Make sure every path command ends with a semicolon. Other than that, it's easy to get an option name wrong, or to accidentally add an extra backslash in \verb|\foreach| loops (writing \verb|\foreach \x \in ...| instead of the correct version: \verb|\foreach \x in ...|), so watch out for those! % % Intro TikZ is a package for TeX/LaTeX for drawing figures, diagrams, and images. It's comprehensive, and extremely powerful, and capable of doing pretty much anything you could want it to. My goal for this next hour is to show you some of the basics of TikZ, so that you can get started making figures. Before I begin, I'll try and convince you that's its worth knowing, by showing you some of the figures you can create with it. % Show examples! Ok, let's get started. How do you use TikZ? First, include the package: %\usepackage{tikz} Now, to actually create a figure, begin the environment `\verb|tikzpicture|'. If you're going to be putting this figure into a larger document, you'll probably want to surround with a `figure' environment so that it floats appropriately, and so you can give it a caption and label. %\begin{figure} %\begin{tikzpicture} % %\end{tikzpicture} %\end{figure} One thing that makes TikZ a little confusing at first is that it has an entirely different syntax than LaTeX, and the TikZ code you'll be writing won't necessarily look like anything you've seen in LaTeX before. Once you have the environment set up, you start drawing. The basic way to draw something in TikZ is with a command like: \verb|\draw (0, 0) -- (1, 0) -- (0, 1) -- (1, 1) -- (2, 1);| Commands will usually start with `\verb|\draw|' and then have a series of coordinates with instructions between them, ending with a semicolon. TikZ has a nice shorthand for creating pictures with a single command. I can write `\verb|\tikz \draw ...;|' instead of beginning and ending an environment. We'll use this shorthand to spare typing whenever possible. Here's an example: \verb|\tikz \draw (0, 0) -- (1, 0) -- (0, 1) -- (1, 1) -- (2, 1);| produces \tikz \draw (0, 0) -- (1, 0) -- (0, 1) -- (1, 1) -- (2, 1);, and is exactly the same as \begin{verbatim} \begin{tikzpicture} \draw (0, 0) -- (1, 0) -- (0, 1) -- (1, 1) -- (2, 1); \end{tikzpicture} \end{verbatim} You can read the previous command as saying `Draw a path starting at (0, 0) then connected by a straight line to (1, 0) then connect that with a straight line to (1, 0)', and so on\dots The coordinates here are in an implicit cartesian plane, as familiar from high school algebra and calculus. The only difference is that the grid is implicit, and not displayed with your figure. % Draw this on the board to illustrate the coordinate system. Two interesting things to observe: First, the default coordinate system is has the positive $x$-axis pointing right, and the positive $y$-axis pointing up, and one unit corresponds to one centimeter. If you want to work with different units, you can simply add the units to your coordinates, like so: `\verb|(2in,0.3pt)|'. It's pretty straightforward to resize everything after you've made an image, so it's probably a good idea to ignore absolute sizes of things when creating an image, and only afterward, try to make things fit in a certain space. Second, TikZ takes care of computing the size of your figure and making sure that your entire figure is visible in the `\verb|tikzpicture|' so you also don't have to worry about any part of your figure getting cut off. Finally, for those of you who don't have experience with computer graphics programming, you'll have to get used to thinking in terms of coordinates when creating your diagrams, which might be a challenge at first, but TikZ can help you out. Another available command that will almost certainly be useful when planning out a figure is `\verb|\grid|'. Here's an example: \tikz \draw[help lines] (0, 0) grid (5, 5); Let's take a look at what this generates. There are two differences between this draw command and the previous one. In the previous one, we used the `\verb|--|' instruction to say draw a straight line between the previous and following coordinates. Here we are using the `\verb|grid|' instruction to draw a grid in the rectangle with corners at $(0, 0)$ and $(5, 5)$. The other new feature is the option given to the draw command, `\verb|help lines|'. This is an example of a \emph{style}, which is a collection of different options, each of which controls a parameter used when drawing, such as line width, line color, line style, coordinate transformations, line start and end decorations, and many many more. See the manual (available online for the full list.) A style is just a collection of settings that can be invoked at once. In this case, `\verb|help lines|' just sets the line width to thin, and makes the line color a lighter gray. To give you a sense of the possibilities available, The following figures all have the very same path drawn, just with different options. \centering{ \tikz[scale=3] \draw[color=blue, ultra thick, |->, dashed] (0, 0) -- (1, 0) -- (1, 1); \tikz[scale=3] \draw[color=red, thin, <->] (0, 0) -- (1, 0) -- (1, 1); \tikz[scale=3] \draw[color=teal, semithick, <-, dotted] (0, 0) -- (1, 0) -- (1, 1); } I'll let you play around with what you've seen in a second, but I just want to present two alternative ways of specifying coordinates. First, polar coordinates. For example, I can write the following: \tikz \draw[color=violet, thick] (0:2) -- (60:2) -- (120:2) -- (180:2) -- (240:2) -- (300:2) -- (0:2); to draw a hexagon using polar coordinates. To use polar coordinates, you specify an angle (in degrees) followed by a \verb|:|, followed by a radius. You can freely mix polar and cartesian coordinates in your figures. Finally, you can specify relative coordinates with a neat special syntax. If you want to say `the point 2 units above the previous point, you can write: `\verb|++(2, 0)|'. Here's an example. Let's say I want to draw a square 3 units above a triangle. Instead of translating all the coordinates in my head when drawing the square, I can just do the following: % Draw this example on the board. \begin{tikzpicture}[scale=0.5] \draw (0, 0) -- (0, 1) -- (1, 0) -- (0, 0); \draw (0, 2) -> ++(1, 0) -> ++(0, 1) -> ++(-1, 0) -> ++(0, -1); % Uses relative coordinates, so each point is relative to the previous one. Imagine a pen changing locations. \end{tikzpicture} Ok, now an exercise. I would like you to try and recreate this figure: \begin{tikzpicture} \draw (0:1) -- (36:4) -- (72:1) -- (108:4) -- (144:1) -- (180:4) -- (216:1) -- (252:4) -- (288:1) -- (324:4) -- (0:1); \draw[purple, very thick] (36:1) -- (72:3) -- (108:1) -- (144:3) -- (180:1) -- (216:3) -- (252:1) -- (288:3) -- (324:1) -- (0:3) -- (36:1); \end{tikzpicture} Now that you have a decent grasp on the basic functionality needed to make line drawings, we'll move on to some more sophisticated functionality. I can draw a curve as follows: \tikz[scale=0.4] \draw (0, 0) to [out=135, in=100] (1, 1); The \verb|out| option specifies the angle at which the curve should leave the starting point and the \verb|in| option specifies the angle at which the curve should arrive at the end point. To draw a circle, \tikz \draw[orange, semithick] (1, 1) circle [radius=0.5]; The first point is the center in this case. To draw a rectangle, \tikz[scale=0.4] \draw[brown] (0, 0) rectangle (2, 4); Here the first point is one corner of the rectangle and the second point is the opposite corner of the rectangle. There are many many more different handy shortcuts that TikZ provides for common shapes and patterns, and you should take a look at the manual to find out more, but I would like to move on. You may not be surprised at this point to learn that the draw command I've been using this whole time is actually shorthand for yet another command, which is the actual command underlying all these different path commands, namely `\verb|\path|'. The command I used for the circle just a minute ago was equivalent to the following command, which is exactly what TikZ expands the above command into. \tikz \path[draw=orange, semithick] (1, 1) circle [radius=0.5]; Just like there is a `\verb|draw|' option to the \verb|path| command, there is also an option to \verb|fill| a path, which can be given in the same way, so I can fill the same circle as above with: \tikz \path[draw=orange, fill=green, thick] (1, 1) circle [radius=0.5]; I'm actually both filling and drawing this circle, and with two different colors. I can choose to just fill the circle by using a shortcut \tikz \fill[green] (1, 1) circle [radius=0.5]; or by using the `\verb|none|' value instead of color as the value for the `\verb|draw|' option. Rather than go through any more small isolated pieces of functionality, I'm going to try and work through a larger example, and introduce some new concepts as needed. Here's the diagram I want to create: % Bullseye \begin{tikzpicture} \foreach \x in {5,4,...,0}{ \path[fill=red,draw=black] (0,0) circle[radius=\x+0.5]; \path[fill=white,draw=black] (0,0) circle[radius=\x]; }; \end{tikzpicture} Looking at this, it seems clear that writing this by hand using the \verb|\draw ... circle ...;| would be really annoying, and if I wanted to adjust it at all, I'd have to make a lot of changes so everything lined up. So TikZ offers a really nice feature, which will be familiar to those of you with some programming experience: For-loops. Here's the code I used to make this bullseye-like figure: \begin{verbatim} % Bullseye \begin{tikzpicture} \foreach \x in {5,4,...,0}{ \draw[fill=red,draw=black] (0,0) circle[radius=\x+0.5]; \draw[fill=white,draw=black] (0,0) circle[radius=\x]; }; \end{tikzpicture} \end{verbatim} The interesting part is the loop, which begins with \verb|\foreach \x in {5,4,...,0}|. This first part means, take whatever code I have below, and copy and paste it once for each item in the list going $5,4,\dotsc, 0$ and have \verb|\x| be the current item in the list each time. Note that we used the value of \verb|\x| in our for-loop to adjust the radius of each circle that we draw, and that we do simple arithmetic to make the red circle bigger than the white circle. Here's a question for you: What would happen if we switched the order in which we drew the two circles? Here's the answer: \begin{tikzpicture} \foreach \x in {5,4,...,0}{ \path[fill=white,draw=black] (0,0) circle[radius=\x]; \path[fill=red,draw=black] (0,0) circle[radius=\x+0.5]; }; \end{tikzpicture} What happens is the red circles are drawn after the white circles, and go right on top of the white circles, so the white circles can't be seen. Another question: What would happen if we went back to the original order for drawing the circles, but instead of counting down, counted up? Here's the answer: \begin{tikzpicture} \foreach \x in {0,1,...,5}{ \draw[fill=red,draw=black] (0,0) circle[radius=\x+0.5]; \draw[fill=white,draw=black] (0,0) circle[radius=\x]; }; \end{tikzpicture} The last two circles cover all of the previous circles, so they can't be seen. Basically, in TikZ, every new part of a drawing goes on top of everything else, so you need to make sure to pay attention to the order in which you draw things to ensure that they don't overwrite each other incorrectly. Sometimes, this is actually very handy, since you'll want to draw something on top of part of your drawing, and it's nice to not have to worry about specifying what's behind what, when all you want is newer stuff to be on top of older stuff. Ok, I'll do one more example, and then give you exercises to work on. Here's what I want to produce: \begin{tikzpicture}[scale=4] \node[label=above left:$y$] (topLeft) at (0, 2) {}; \node[label=below right:$x$] (botRight) at (2, 0) {}; \draw[xstep=0.2,ystep=0.2,gray,very thin] (0,0) grid (2,2); \draw[<->] (topLeft.center) -- (0, 0) -- (botRight.center); \draw[domain=0:2,fill=none,samples=100,draw=black] plot (\x, {\x*sin(2*360*\x)/2 + 1}); \node[fill=white] at (2, 2) {$f(x) = x \sin(x)$}; \end{tikzpicture} I make this diagram with a few commands. First, I use \begin{verbatim} \node[label=above left:$y$] (topLeft) at (0, 2) {}; \node[label=below right:$x$] (botRight) at (2, 0) {}; \end{verbatim} This defines two new coordinates, which can be referred to by \verb|(topLeft)| and \verb|botRight| respectively, and adds labels to the image with positions relative to the coordinates. The \verb|{}| contains any text associated with the node. In this case, we don't want the nodes themselves to have text. Generally, nodes are like coordinates in that they have a location and can be referred to later, but they can also have their own appearance and text content, though I'm not using either of those features in this case. To give a node a label, use the \verb|label| option and provide a location relative to the node, and then the label text. I find it pretty confusing to talk about node labels, since nodes usually contain text themselves, and are often used as labels, but remember that labels are always associated with a node, while nodes can be inserted pretty much anywhere in a path. Next, I draw the grid using the grid command, specifying that I want horizontal lines to be drawn at every \verb|ystep| units, and vertical lines to be drawn at every \verb|xstep| units, and that I want them to be drawn with very thin, gray lines. Now, I draw the axes with a single path, starting from the center of the \verb|topLeft| node, and going to the origin, then the center of the \verb|botRight| node. The next command actually draws the curve. \begin{verbatim} \draw[domain=0:2,fill=none,samples=100,draw=black] plot (\x, {\x*sin(2*360*\x)/2 + 1}); \end{verbatim} This command uses the plot path instruction, which creates a path from the plot of a function. I specify the function with \verb|(\x, {\x*sin(2*360*\x)/2 + 1})|. Note that you have to use \verb|\x| as your variable in the plot instruction unless you use an option to select a different variable name. Also, the function must be surrounded by curly braces, and can use any of many builtin functions. In this example, we only use $\sin$ but there are many builtins, which can be found in the documentation. Two other things to note: We use the \verb|domain| option to define the domain of coordinates over which we are plotting the function. If you want to plot a function over a domain that's different from the coordinates in which you putting the plot, you'll have to use some other options to shift things around, but this will work for the time being. The way TikZ actually plots the function is by taking samples of the function value at various points in the domain and interpolating the missing values, and then trying to smooth out the curve, so the more complex your function the more samples you'll need to get something that looks right. In this case, I use the \verb|samples| option to specify that TikZ should use $100$ samples. I don't have time to explain this in its entirety, but at this point you should be almost able to create some pretty cool complex figures like: \begin{tikzpicture}[scale=1.8] \foreach \x in {0,1,...,4} { \foreach \y in {0,1,...,4} { \draw[shift={+(1.2*\x,1.2*\y)}, <->] (1, 0) -- (0, 0) -- (0, 1); \draw[shift={+(1.2*\x,1.2*\y)}, dashed] (0, 0.5) -- (1, 0.5); \draw[shift={+(1.2*\x, 1.2*\y)},variable=\t,domain=0:1,smooth,samples=100] plot (\t, {sin(360*\t*(\x/2+1)+30*\y)/2 + 0.5}); }; }; \end{tikzpicture} %\begin{tikzpicture}[scale=1.5] %\def\rC{2.5} %\def\rZ{2.0} %\def\rB{1.5} %\def\xr{2} %\def\spacing{6} %\coordinate[label=below:$0$] (n1) at (0, 0); %\coordinate[label=below:$0$] (n2) at (\spacing, 0); %\coordinate[label=below:$0$] (n3) at (2*\spacing, 0); %\draw let \p1 = ($ (n1) - (1, 0) $), % \p2 = ($ (n3) + (1, 0) $) % in (\p1) -- (\p2); % % %\foreach \point / \n in {n1/k+1, n2/k, n3/k-1} { % \draw[fill=purple!20] ($ (\point) + (0,\rC) $) circle [y radius=\rC,x radius=\rC-0.5]; % \node[label=below:$C_{\n}$] at ($ (\point) + 2*(0,\rC) $) {}; % \draw[fill=purple!30] ($ (\point) + (0,\rZ) $) circle [y radius=\rZ,x radius=\rZ-0.5]; % \node[label=below:$Z_{\n}$] at ($ (\point) + 2*(0,\rZ) $) {}; % \draw[fill=purple!40] ($ (\point) + (0,\rB) $) circle [y radius=\rB,x radius=\rB-0.5]; % \node[label=below:$B_{\n}$] at ($ (\point) + 2*(0,\rB) $) {}; % \draw[fill=black] (\point) circle [radius=0.1]; %} % %\foreach \start / \end / \k in {n1/n2/k, n2/n3/k-1} { % \draw let \p1 = ($ (\start) + (0, 2*\rC) $), % \p2 = ($ (\end) + (0, 2*\rB) $) % in (\p1) -- (\p2); % \draw let \p3 = ($ (\start) + (0, 2*\rZ) $), % \p4 = (\end) % in (\p3) -- (\p4); % \node[anchor=north] at ($ (\start)!.5!(\end) + (0,2*\rZ) $) {$\partial_{\k}$}; % \node[anchor=north] at ($ (\start)!.5!(\end) + (0,\rB) $) {$\partial_{\k}$}; %} % %\end{tikzpicture} %\end{tikzpicture} \end{document}