【LaTeX】TikZ官方手册笔记1:画坐标轴图

这是TikZ官方手册的摘录,我把重要的部分和代码的解释摘录下来了,还稍微加入了一点个人的理解以及代码的修饰。

请配合官方手册()食用,此文可做备忘录

链接:https://pan.baidu.com/s/1jBtTuMMp70hqWQ08JE2Ifw
密码:jmbt

本笔记包括手册中的:

两个部分。本文主要学习用TikZ画下面这幅图:



Generally speaking, when you use TikZ you “program” your graphics, just as you “program” your document when you use TEX.

With TikZ you get all the advantages of the “TeX-approach to typesetting” for your graphics:

  • Quick creation of simple graphic

  • Precise positioning

  • The use of marcros

It turns out that there are actually two layers below TikZ:

  • System layer: As a user, you will note use the system layer directly. So forget it.

  • Basic layer: The basic layer provides a set of basic commands that allow you to produce complex graphics in a much easier manner than by using the system layer directly.

In theory, TikZ itself is just one of several possible “frontends,” which are sets of commands or a special syntax that makes using the basic layer easier.

In practice, TikZ is the only “serious” frontend for pgf.

Since most users will only use TikZ and almost no one will use the system layer directly, this manual is mainly about TikZ in the first parts; the basic layer and the system layer are explained at the end.

It's not important.

It's not important.

This manual describes both the design of TikZ and its usage.

Typically, you will not need to read the sections on the basic layer.But you will only need to read the part on the system layer if you intend to write your own frontend or if you wish to port PGF to a new driver.

The “public” commands and environments provided by the system are described throughout the text. In each such description, the described command, environment or option is printed in red. Text shown in green is optional and can be left out.

It's not important.

It's not important.



This tutorial is intended for new users of TikZ. It does not give an exhaustive account of all the features of TikZ, just of those that you are likely to use right away.

First, Karl had to download and install a package called PGF. Then it turns out that inside this package there is another package called TikZ.

His son assures him that TikZ’s name is intended to warn people that TikZ is not a program that you can use to draw graphics with your mouse or tablet. Rather, it is more like a “graphics language.”

Karl wants to put a graphic on the next worksheet for his students. He is currently teaching his students about sine and cosine. What he would like to have is something that looks like this (ideally):

In LaTeX this is done using the environment {tikzpicture}, in plain T E X you just use \tikzpicture to start the picture and \endtikzpicture to end it.

To setting up the environment in LaTeX, you code as follows:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on 
    \begin{tikzpicture}
        \draw (-1.5,0) -- (1.5,0);
        \draw (0,-1.5) -- (0,1.5); 
    \end{tikzpicture}.
\end{document}

When executed, that is, run via pdflatex or via LaTeX followed by dvips, the resulting will contain something that looks like this:

Let’s have a more detailed look at the code. First, the package tikz is loaded. This package is a so-called “frontend” to the basic pgf system.The frontend makes things easier by providing a simpler syntax.

Inside the environment there are two \drawcommands. They mean:

The path, which is specified following the command up to the semicolon, should be drawn.” The first path is specified as (-1.5,0) --(0,1.5), which means “a straight line from the point at position (−1.5, 0) to the point at position (0, 1.5).” Here, the positions are specified within a special coordinate system in which, initially, one unit is 1cm.

The basic building block of all pictures in TikZ is the path. A path is a series of straight lines and curves that are connected:

that is not the whole picture, but let us ignore the complications for the moment

You start a path by specifying the coordinates of the start position as a point in round brackets, as in (0,0). This is followed by a series of “path extension operations.” The simplest is --, which we used already.

See the code:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on
        \tikz \draw (-1.5,0) -- (1.5,0) -- (0,-1.5) -- (0,1.5);
\end{document}

There is no {tikzpicture}environment, here. Instead, the little command \tikz is used. This command either takes one argument or collects everything up to the next semicolon (;) and puts it inside a {tikzpicture} environment.

The next thing Karl wants to do is to draw the circle. For this, TikZ provides a special syntax. One or two “control points” are needed.

Suppose the curve should end at and the second support point is . Then the curve will, indeed, end at and the tangent of the curve at point will go through .

See the example:

\begin{tikzpicture}
\filldraw [gray] (0,0) circle [radius=2pt] (1,1) circle [radius=2pt] (2,1) circle [radius=2pt] (2,0) circle [radius=2pt];
\draw (0,0) .. controls (1,1) and (2,1) .. (2,0); 
\end{tikzpicture}

And we weill have:

The general syntax for extending a path in a “curved” way is.

 .. controls  and < second control point > .. 

You can leave out the and 〈 second control point 〉 , which causes the first one to be used twice.

So we can have a half circle by coding:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on:
    \begin{tikzpicture}
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5);
        \draw (-1,0) .. controls (-1,0.555) and (-0.555,1) .. (0,1) .. controls (0.555,1) and (1,0.555) .. (1,0);
    \end{tikzpicture}
\end{document}

In order to draw a circle, the path construction operation circle can be used:

\tikz \draw (0,0) circle [radius=1cm]

You can also append an ellipse to the path using the ellipse operation. Instead of a single radius you can specify two of them:

\draw (0,0) ellipse [x radius=20pt, y radius=10pt];

So, returning to Karl’s problem, he can write\draw (0,0) circle [radius=1cm];to draw the circle:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on:
    \begin{tikzpicture}
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5);
        \draw (0,0) circle [radius=1cm]
    \end{tikzpicture}
\end{document}

We'll have:

The next things we would like to have is the grid in the background. There are several ways to produce it.For example, one might draw lots of rectangles.

To add a rectangle to the current path, use the rectangle path construction operation. See:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on:
    \begin{tikzpicture}
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5);
        \draw (0,0) circle [radius=1cm];
        \draw (0,0) rectangle (0.5,0.5);
        \draw (-0.5,-0.5) rectangle (-1,-1);
    \end{tikzpicture}
\end{document}

While this may be nice in other situations, this is not really leading anywhere with Karl’s problem: First, we would need an awful lot of these rectangles and then there is the border that is not “closed.”

Karl is about to resort to simply drawing four vertical and four horizontal lines using the nice \draw command, when he learns that there is a grid path construction operation.

The grid path operation adds a grid to the current path.

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    We are working on:
    \begin{tikzpicture}
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
    \end{tikzpicture}
\end{document}

To subdue the grid, Karl adds two more options to the \draw command that draws the grid.

  • First, he uses the color gray for the grid lines.

  • Second, he reduces the line width to very thin.

  • Finally, he swaps the ordering of the commands so that the grid is drawn first and everything else on top.

Instead of the options gray, very thin Karl could also have said help lines. Styles are predefined sets of options that can be used to organize how a graphic is drawn.

By saying help lines you say “use the style that I (or someone else) has set for drawing help lines.”

If one want to use the color blue!50 instead of gray, he could provide the following option:

help lines/.style={color=blue!50,very thin}

The effect of this “style setter” is that in the current scope or environment the help lines option has the same effect as color=blue!50,very thin.

Using styles makes your graphics code more flexible. You can change the way things look easily in a consistent manner.

Normally, styles are defined at the beginning of a picture. However, you may sometimes wish to define a style globally, so that all pictures of your document can use this style. To do this, you can use the \tikzset command at the beginning of the document as in:

\tikzset{help lines/.style=very thin}

To build a hierarchy of styles you can have one style use another. So in order to define a style Karl’s grid that is based on the grid style Karl could say:

\tikzset{Karl’s grid/.style={help lines,color=blue!50}}
...
\draw[Karl’s grid] (0,0) grid (5,5);

Styles are made even more powerful by parametrization. This means that, like other options, styles can also be used with a parameter. For instance, Karl could parameterize his grid so that, by default, it is blue, but he could also use another color:

\begin{tikzpicture}
    [Karl’s grid/.style ={help lines,color=#1!50}, Karl’s grid/.default=blue]
    \draw[Karl’s grid] (0,0) grid (1.5,2); 
    \draw[Karl’s grid=red] (2,0) grid (3.5,2); 
\end{tikzpicture}

Line width option:

  • ultra thin

  • very thin

  • thin

  • normal

  • thick

  • very thick

  • ultra thick

To use the width between the options ahead, one can use semi-xx, such as semithick.

Another useful thing one can do with lines is to dash or dot them. Line style option:

  • dashed

  • dotted

  • loosely dashed

  • densely dashed

  • loosely dotted

  • densely dotted

If he really, really needs to, Karl can also define much more complex dashing patterns with the dash pattern option, but his son insists that dashing is to be used with utmost care and mostly distracts.

Our next obstacle is to draw the arc for the angle. For this, the arc path construction operation is useful, which draws part of a circle or ellipse.

This arc operation is followed by options in brackets that specify the arc. An example would be arc[start angle=10, end angle=80, radius=10pt], which means exactly what it says.

By coding:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
    \end{tikzpicture}
\end{document}

We get:

Karl thinks this is really a bit small and he cannot continue unless he learns how to do scaling. For this, he can add the [scale=3] option. He could add this option to each \draw command, but that would be awkward. Instead, he adds it to the whole environment, which causes this option to apply to everything within.

As for circles, you can specify “two” radii in order to get an elliptical arc:

\tikz \draw (0,0) arc [start angle=0, end angle=315, x radius=1.75cm, y radius=1cm]

In order to save space in this manual, it would be nice to clip Karl’s graphics a bit so that we can focus on the “interesting” parts.

Clipping is pretty easy in TikZ. You can use the \clip command to clip all subsequent drawing. It works like \draw, only it does not draw anything, but uses the given path to clip everything subsequently.

See:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \clip (-0.1, -0.2) rectangle (1.1, 0.75);
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
    \end{tikzpicture}
\end{document}

You can also do both at the same time: Draw and clip a path. For this, use the \draw command and add the clip option.

Here is an example:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \clip[draw] (0.5,0.5) circle (0.6cm);
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
    \end{tikzpicture}
\end{document}

Although Karl does not need them for his picture, he is pleased to learn that there are parabola and and path operations for adding parabolas and sine and cosine curves to the current path.

  • For Parabola : for the option \parabola the current point will lie on the parabola as well as the point given after the parabola operation. Consider the following example:

\tikz \draw (0,0) rectangle (1,1) (0,0) parabola (1,1);


    It is also possible to place the bend somewhere else:

    ```LaTeX
\tikz \draw[x=1pt,y=1pt] (0,0) parabola bend (4,16) (6,12);
  • For & : The operations sin and cos add a sine or cosine curve in the interval [0, π/2] such that the previous current point is at the start of the curve and the curve ends at the given end point. Here are the example:
\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
        \draw[x=1cm, y=1cm] (-1,-1) cos (0,0) sin (1,1) cos (2,0);
    \end{tikzpicture}
\end{document}

Returning to the picture, Karl now wants the angle to be “filled” with a very light green. For this he uses \fill instead of \draw. Here is what Karl does:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
        \fill [green!10!white] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=60, radius=3mm] -- (0,0);
    \end{tikzpicture}
\end{document}

The color green!20!white means 20% green and 80% white mixed together.

What would have happened, if Karl had not “closed” the path using --(0,0) at the end? In this case, the path is closed automatically, so this could have been omitted. Indeed, it would even have been better to write the following, instead:

\fill [green!10!white] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=60, radius=3mm] -- cycle;

The --cycle causes the current path to be closed (actually the current part of the current path) by smoothly joining the first and last point:

\begin{tikzpicture}[line width=5pt]
    \draw (0,0) -- (1,0) -- (1,1) -- (0,0); 
    \draw (2,0) -- (3,0) -- (3,1) -- cycle; 
    \useasboundingbox (0,1.5); % make bounding box higher
\end{tikzpicture}

You can also fill and draw a path at the same time using the \filldraw command. This will first draw the path, then fill it. This may not seem too useful, but you can specify different colors to be used for filling and for stroking. These are specified as optional arguments like this:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
        \filldraw [fill=green!10!white, draw=black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=60, radius=3mm] -- cycle;
    \end{tikzpicture}
\end{document}

Karl briefly considers the possibility of making the angle “more fancy” by shading it.

Instead of filling the area with a uniform color, a smooth transition between different colors is used. For this, \shade and \shadedraw, for shading and drawing at the same time, can be used:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=60, radius=3mm];
        \shadedraw [left color=green, right color=black!30!white,draw=black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=60, radius=3mm] -- cycle;
    \end{tikzpicture}
\end{document}

There are different ways of specifying coordinates. The easiest way is to say something like:

  • (10pt,2cm): this means 10pt in x-direction and 2cm in y-directions.

  • Alternatively, you can also leave out the units as in (1,2), which means “one times the current x-vector plus twice the current y-vector.” These vectors default to 1cm in the x-direction and 1cm in the y-direction, respectively.

In order to specify points in polar coordinates, use the notation (30:1cm), which means 1cm in direction 30 degree. This is obviously quite useful to “get to the point (cos 30 ◦ , sin 30 ◦ ) on the circle.”

You can add a single + sign in front of a coordinate or two of them as in +(0cm,1cm) or ++(2cm,0cm). Such coordinates are interpreted differently:

  • The first form means “1cm upwards from the previous specified position”

  • The second means “2cm to the right of the previous specified position, making this the new specified position.” For example, we can draw the sine line as follows:

See:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=30, radius=3mm];
        \shadedraw [left color=green, right color=blue!30!white,draw=black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[color=red, very thick] (30:1cm) -- +(0,-0.5);
    \end{tikzpicture}
\end{document}

Karl used the fact sin 30 ◦ = 1/2. However, he very much doubts that his students know this, so it would be nice to have a way of specifying “the point straight down from (30:1cm) that lies on the x-axis.”

In general, the meaning of < p > |- < q > is “the intersection of a vertical line through p and a horizontal line through q.”

Next, let us draw the cosine line. One way would be to say(30:1cm |- 0,0) -- (0,0). Another way is the following: we “continue” from where the sine ends:

\documentclass{article} % say 
\usepackage{tikz} 
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \draw[step=.5cm, gray, very thin, dashed] (-1.4,-1.4) grid (1.4,1.4);
        \draw (-1.5,0) -- (1.5,0); 
        \draw (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \draw (3mm, 0mm) arc [start angle=0, end angle=30, radius=3mm];
        \shadedraw [left color=green, right color=blue!30!white,draw=black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[color=red, very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue, very thick] (30:1cm) ++ (0,-0.5) -- (0,0);
    \end{tikzpicture}
\end{document}

The natural difference of + and ++ lies on whether the reference point had changed.

*The code in this section of manual is no run-able in my XeLaTeX environment

*So I have used another way

Karl is left with the line for tan α, which seems difficult to specify using transformations and polar coordinates. The first – and easiest – thing he can do is so simply use the coordinate (1,{tan(30)}) since TikZ’s math engine knows how to compute things like tan(30).

See the code:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        \clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,very thin] (-1.4,-1.4) grid (1.4,1.4); 
            \draw[->] (-1.5,0) -- (1.5,0); 
        \draw[->] (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}

Karl now wants to add the little arrow tips at the end of the axes.

It turns out that adding arrow tips is pretty easy: Karl adds the option -> to the drawing commands for the axes:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,very thin] (-1.4,-1.4) grid (1.4,1.4); 
        \draw[->, very thick] (-1.5,0) -- (1.5,0); 
        \draw[->, very thick] (0,-1.5) -- (0,1.5); 
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}

There many kinds of arrows in TikZ, such as:

  • [->]

  • [<-]

  • [<->]

  • [«-]

  • ...

Karl wonders whether such a military name for the arrow type is really necessary. He is not really mollified when his son tells him that Microsoft’s PowerPoint uses the same name.

If Karl wishes to set a certain graphic option for the whole picture, he can simply pass this option to the \tikz command or to the{tikzpicture} environment.

However, if Karl wants to apply graphic options to a local group, he put these commands inside a {scope} environment.

See:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,very thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0); 
            \draw[->,] (0,-1.5) -- (0,1.5); 
        \end{scope}
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}

The output TikZ picture will be the same to above.

Forget about it.

Karl’s next aim is to add little ticks on the axes at positions −1, −1/2, 1/2, and 1. For this, it would be nice to use some kind of “loop,” especially since he wishes to do the same thing at each of these positions.

TikZ introduces yet another command, called \foreach, which I introduced since I could never remember the syntax of the other packages.

In its basic form, the \foreach command is easy to use:

“” can be coded as \foreach \x in {1,2,3} {$ x = \x $}

The basic syntax is:

\foreach  in {} 

See the example below:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,ultra thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \foreach \x in {-1cm, -0.5cm, 0,5.cm, 1cm} \draw (\x, -1pt) -- (\x, 1pt);
        \foreach \y in {-1cm, -0.5cm, 0.5cm, 1cm} \draw (-1pt, \y) -- (1pt, \y);
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0); 
            \draw[->,] (0,-1.5) -- (0,1.5); 
        \end{scope}
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}

We have:

It is possible to use ... inside the \foreach statement to iterate over a large number of values (which must, however, be dimensionless real numbers) as in the following example:

\tikz \foreach \x in {1,...,10} \draw (\x,0) circle (0.4cm);
\tikz \foreach \x in {-1,-0.5,...,1} \draw (\x cm,-1pt) -- (\x cm,1pt);

If you provide two numbers before the ..., the \foreach statement will use their difference for the stepping, see:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,ultra thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \foreach \x in {-1cm, -0.5cm, 0,5.cm, 1cm} \draw (\x, -1pt) -- (\x, 1pt);
        \foreach \y in {-1cm, -0.5cm, 0.5cm, 1cm} \draw (-1pt, \y) -- (1pt, \y);
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0); 
            \draw[->,] (0,-1.5) -- (0,1.5); 
        \end{scope}
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \foreach \x in {-2,-1.5,...,2} \draw (\x,0) circle (0.4cm);
        \end{tikzpicture}
\end{document}

We can also nest loops to create interesting effects:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document} 
    \begin{tikzpicture}
        \foreach \x in {1,2,...,5,7,8,...,12}
            \foreach \y in {1,...,5}
            {
                \draw (\x ,\y ) + (-0.5,-0.5) rectangle ++(0.5,0.5);
                \draw (\x, \y ) node {\x, \y};
            }
    \end{tikzpicture}
\end{document}

2.21.1 Basic

Karl is, by now, quite satisfied with the picture. However, the most important parts, namely the labels, are still missing!

The basic idea is the following: When TikZ is constructing a path and encounters the keyword node in the middle of a path, it reads a node specification. The keyword node is typically followed by some options and then some text between curly braces.

This text is put inside a normal TeX box and then placed at the current position, that is, at the last specified position.

Notice! All nodes are drawn only after the path has been completely drawn /filled/shaded/clipped/whatever.

See the code:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}
        \draw (0,0) rectangle (2,2);
        \draw (0.5,0.5) node[fill=yellow!20!white] {Node1} -- (1.5,1.5) node {Node 2};
    \end{tikzpicture}
\end{document}

We have:

2.21.2 Setup the \node's placement

Obviously, Karl would not only like to place nodes on the last specified position, but also to the left or the right of these positions. For this, every node object that you put in your picture is equipped with several anchors.

For example, the north anchor is in the middle at the upper end of the shape, the south anchor is at the bottom and the north east anchor is in the upper right corner.

When you give the option anchor=north, the text will be placed such that this northern anchor will lie on the current position and the text is, thus, below the current position.

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,ultra thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \foreach \x in {-1, -0.5, 0.5, 1} \draw (\x, 1pt) -- (\x, -1pt) node[anchor=north] {$\x$};
        \foreach \y in {-1, -0.5, 0.5, 1} \draw (1pt, \y) -- (-1pt, \y) node[anchor=east] {$\y$};
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0); 
            \draw[->,] (0,-1.5) -- (0,1.5); 
        \end{scope}
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}

However, Karl thinks that, though “correct,” it is quite counter-intuitive that in order to place something below a given point, he has to use the north anchor.

For this reason, there is an option called below, which does the same as anchor=north. Similarly, above right does the same as anchor=south west.

If given, the shape will additionally be shifted downwards by the given amount. So, below=1pt can be used to put a text label below some point and, additionally shift it 1pt downwards.

2.21.3 use x/\xtext to show whatever texts you like

Karl now faces a problem: For the \foreach statement, the position \x should still be given as 0.5 since TikZ will not know where \frac{1}{2} is supposed to be. On the other hand, the typeset text should really be \frac{1}{2}.

To solve this problem, \foreach offers a special syntax: Instead of having one variable \x, Karl can specify two (or even more) variables separated by a slash as in \x / \xtext. Then, the elements in the set over which \foreach iterates must also be of the form 〈 first 〉 / 〈 second 〉 . In each iteration, \x will be set to 〈 first 〉 and 1xtex will be set to 〈 second 〉 . If no 〈 second 〉 is given, the 〈 first 〉 will be used again. So, here is the new code for the ticks:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,ultra thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \foreach \x/\xtext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2},1} \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};
        \foreach \y/\ytext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1} \draw (1pt, \y) -- (-1pt, \y) node[anchor=east] {$\ytext$};
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0); 
            \draw[->,] (0,-1.5) -- (0,1.5); 
        \end{scope}
        \draw (0,0) circle [radius=1cm]; 
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- +(0,-0.5);
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) -- (0,0);
        \draw[orange, very thick] (1,0) -- (1, {tan(30)});
        \end{tikzpicture}
\end{document}
2.21.4 Setup the background of the \node

Karl is quite pleased with the result, but his son points out that this is still not perfectly satisfactory: The grid and the circle interfere with the numbers and decrease their legibility. Karl is not very concerned by this (his students do not even notice), but his son insists that there is an easy solution: Karl can add the [fill=white] option to fill out the background of the text shape with a white color.

2.21.4 Setup the \node's slope

The next thing Karl wants to do is to add the labels like . For this, he would like to place a label “in the middle of the line.” To do so, instead of specifying the label node {$\sin\alpha$} directly after one of the endpoints of the line (which would place the label at that endpoint), Karl can give the label directly after the --, before the coordinate.

By default, this places the label in the middle of the line, but the pos= options can be used to modify this. Also, options like near start and near end can be used to modify this position:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document} 
    \begin{tikzpicture}[scale=3]
        %\clip (-0.1,-0.2) rectangle (1.1,1.51); 
        \draw[step=.5cm,gray,ultra thin, dashed] (-1.4,-1.4) grid (1.4,1.4); 
        \draw (0,0) circle [radius=1cm]; 
        \foreach \x/\xtext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2},1} \draw (\x cm,1pt) -- (\x cm,-1pt) node[below, fill=white] {$\xtext$};
        \foreach \y/\ytext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1} \draw (1pt, \y) -- (-1pt, \y) node[left, fill=white] {$\ytext$};
        \begin{scope}[very thick]
            \draw[->] (-1.5,0) -- (1.5,0) coordinate (x axis); 
            \draw[->,] (0,-1.5) -- (0,1.5) coordinate (y axis) ; 
        \end{scope}
        \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc [start angle=0, end angle=30, radius=3mm] -- cycle;
        \draw[red,very thick] (30:1cm) -- node[left=1pt, fill=white] {$\sin(\alpha)$}  +(0,-0.5) ;
        \draw[blue,very thick]  (30:1cm) ++(0,-0.5) --  node[below=1.2pt, fill=white] {$\cos (\alpha)$}  (0,0);
        \draw[orange, very thick, near start] (1,0) --  node[right=1pt, fill=white] {$\tan(\alpha) = \frac{\color{red} \sin (\alpha)}{\color{blue} \cos (\alpha)}$} (1, {tan(30)});
        \draw (0,0) -- (1,{tan(30)});
        \end{tikzpicture}
\end{document}

You can also position labels on curves and, by adding the sloped option, have them rotated such that they match the line’s slope. Here is an example:

\documentclass{article} % say 
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document} 
    \begin{tikzpicture}
        \draw (0,0) .. controls (6,1) and (9,1)  .. node[near start, sloped, above] {Near start} node[midway] {Mid way} node[very near end, sloped, below] {Very near end} (12,0);
    \end{tikzpicture}
\end{document}

你可能感兴趣的:(【LaTeX】TikZ官方手册笔记1:画坐标轴图)