Note on Tikz

Opinionated “Good” Habit when using Tikz

Create a “bezier curve"

\draw (0, 0) to[bend right=40] (5, 5);

Can bend right or bend left, depends on from (0, 0) to (5, 5) or the other direction.

bend right=40 means the degree of bending.

Define points on arbitrary curve

Here I use “bezier curve” as example.

\draw (0, 0) to[bend right=40]
    node[pos=0.2,draw,fill=red,circle,inner sep=1pt] (a) {}
    (5, 5);

The pos option in node defines what fraction of this curve should I put a point on it. This node is defined as a.

Get x and y coordinate of arbitrary point

Let the arbitrary be a.

\path (a); \pgfgetlastxy{\xcoord}{\ycoord};
\coordinate (a_x) at (\xcoord, 0);
\coordinate (a_y) at (0, \ycoord);

First, let the path be on the point a so that pgf can remember it.

Second, \pgfgetlastxy outputs the x-coordinate \xcoord and y-coordinate \ycoord of the last path, which we define as point a.

Finally, we can define the a_x and a_y points for the corresponding coordinate points.

Draw “tangent line” on curve

Here I use “bezier curve” as example.

\draw (0, 0) to[bend right=40]
    node[pos=0.5,draw,fill=red,circle,inner sep=1pt] (a) {}
    node[pos=0.51] (b) {}
    (5, 5);
\draw[shorten >=-1cm, shorten <=-1cm, thick, red] (a) -- (b);

Instead of do the tangent line in a delicated way, I found out that just define two close point (a and b) and connect them together.

Notice that I didn’t draw the inner point at point b.

When connecting two points, use negative number in shorten to actually extend the line out.

Decoration: brace

Need to add \usetikzlibrary{decorations.pathreplacing} in preamble.

%%% brace on up/right
\draw [decorate,decoration={brace,amplitude=4pt},xshift=0pt,yshift=3pt]
       (a) -- (b) node [black,midway,yshift=.3cm] {\footnotesize $foo$};
%%% brace on down/left (mirror)
\draw [decorate,decoration={brace,amplitude=4pt, mirror},xshift=0pt,yshift=3pt]
       (a) -- (b) node [black,midway,yshift=.3cm] {\footnotesize $foo$};

Need to modify xshift and yshift to micro-adjust the brace display.

Intersection between two curves

Source

Need to add \usetikzlibrary{intersections} in preamble.

\documentclass[tikz, margin = 1mm]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
    \draw[name path=a] (0, 0) to[bend right = 40] (2, 0);
    \draw[name path=b] (0, -.5) to[bend left = 40] (2, -.5);
    \path[name intersections={of=a and b, by=e}];
    \node[draw,fill=red,circle,inner sep=1pt] at (e) {};
\end{tikzpicture}
\end{document}

Explanation:

Change the font size of all node/label

Simply use \tikzstyle is suffice:

\begin{tikzpicture}
    \tikzstyle{every node}=[font=\scriptsize]
    ...
\end{tikzpicture}

Calculation points

Need to add \usetikzlibrary{calc}

The syntax of calc library is

\documentclass[tikz, margin = 1mm]{standalone}
\usetikzlibrary{calc, intersections}
\begin{document}
\begin{tikzpicture}

    % The following figure shows how the golden section search separate the 2-D space.
    \pgfmathsetmacro{\x}{5};
    \pgfmathsetmacro{\y}{5};
    \pgfmathsetmacro{\tau}{0.618};

    \draw[-] (0, 0) -- (\x, 0) -- (\x, \y) -- (0, \y) -- (0, 0);

    % define (a, b) points in both dimension
    \coordinate[draw,fill=red,circle,inner sep=1pt] (x00) at ( $ (0, 0)$ );
    \coordinate[draw,fill=red,circle,inner sep=1pt] (x01) at ( $ (\x, 0) $ );
    \coordinate[draw,fill=red,circle,inner sep=1pt] (x10) at ( $ (0, \y)$ );
    \coordinate[draw,fill=red,circle,inner sep=1pt] (x11) at ( $ (\x, \y) $ );

    % use calc library to calculate the coordinate of the (c, d) points in both dimension
    \coordinate[draw,fill=blue,circle,inner sep=1pt](c1) at ( $ (x00)!1-\tau!(x01) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](c1mirror) at ( $ (x10)!1-\tau!(x11) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](d1) at ( $ (x00)!\tau!(x01) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](d1mirror) at ( $ (x10)!\tau!(x11) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](c2) at ( $ (x00)!1-\tau!(x10) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](c2mirror) at ( $ (x01)!1-\tau!(x11) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](d2) at ( $ (x00)!\tau!(x10) $ );
    \coordinate[draw,fill=blue,circle,inner sep=1pt](d2mirror) at ( $ (x01)!\tau!(x11) $ );

    % draw dashed line to connect coordinates
    \draw[dashed, name path = dashc1] (c1) -- (c1mirror);
    \draw[dashed, name path = dashd1] (d1) -- (d1mirror);
    \draw[dashed, name path = dashc2] (c2) -- (c2mirror);
    \draw[dashed, name path = dashd2] (d2) -- (d2mirror);

    % define the interior (c, d) points using coordinates
    \path[name intersections={of=dashc1 and dashc2, by=y00}];
    \node[draw,fill=orange,circle,inner sep=1pt] at (y00) {};

    \path[name intersections={of=dashc1 and dashd2, by=y10}];
    \node[draw,fill=orange,circle,inner sep=1pt] at (y10) {};

    \path[name intersections={of=dashd1 and dashc2, by=y01}];
    \node[draw,fill=orange,circle,inner sep=1pt] at (y01) {};

    \path[name intersections={of=dashd1 and dashd2, by=y11}];
    \node[draw,fill=orange,circle,inner sep=1pt] at (y11) {};

\end{tikzpicture}
\end{document}

Article tags: Miscellaneous

posted on: 2022-04-27, last edited on: 2023-07-28, written by: huijunchen9260