Tutorial 1ΒΆ

[1]:
%load_ext autoreload
%autoreload 2
[2]:
from tikzpics import TikzFigure
from tikzpics.node import Node
import numpy as np
[3]:
fig = TikzFigure()

n1 = fig.add_node(0, 0, shape="circle", color="white", fill="blue", content="Hello!")
n2 = fig.add_node(5, 0, shape="circle", color="white", fill="red", content="Hi!")

fig.draw([n1, n2], options=["->", "line width=2"], color="gray")

fig.show()
print(fig.generate_tikz())


../_images/tutorials_tutorial_01_3_1.png


% --------------------------------------------- %
% Tikzfigure generated by tikzpics v0.1         %
% https://github.com/max-models/tikzpics        %
% --------------------------------------------- %
\begin{tikzpicture}

    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfsetlayers{0}

    % Layer 0
    \begin{pgfonlayer}{0}
        \node[shape=circle, color=white, fill=blue] (node0) at (0, 0) {Hello!};
        \node[shape=circle, color=white, fill=red] (node1) at (5, 0) {Hi!};
        \draw[->, line width=2, color=gray] (node0) to (node1);
    \end{pgfonlayer}{0}
\end{tikzpicture}

[4]:
fig = TikzFigure()

n1 = Node(0, 0, shape="circle", color="white", fill="blue", content="Hello!")
n2 = Node(5, 0, shape="circle", color="white", fill="red", content="Hi!")

fig.add([n1, n2])

fig.draw([n1, n2], options=["<-", "line width=2"], color="gray")

fig.show()
print(fig.generate_tikz())


../_images/tutorials_tutorial_01_4_1.png


% --------------------------------------------- %
% Tikzfigure generated by tikzpics v0.1         %
% https://github.com/max-models/tikzpics        %
% --------------------------------------------- %
\begin{tikzpicture}

    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfsetlayers{0}

    % Layer 0
    \begin{pgfonlayer}{0}
        \node[shape=circle, color=white, fill=blue] (node0) at (0, 0) {Hello!};
        \node[shape=circle, color=white, fill=red] (node1) at (5, 0) {Hi!};
        \draw[<-, line width=2, color=gray] (node0) to (node1);
    \end{pgfonlayer}{0}
\end{tikzpicture}

[5]:
fig = TikzFigure()

nodes_heart = [
    [2, 0],
    [0, 3.5],
    [1, 4.5],
    [2, 3.0],
    [3, 4.5],
    [4, 3.5],
    [3, 2],
]

fig.colorlet("lightred", "red!40!white")

# TODO: Not every corner should be rounded!
fig.draw(
    nodes_heart,
    options=["draw", "rounded corners=20pt", "line width=4"],
    layer=0,
    cycle=True,
    color="red",
    fill="lightred",
    comment="Heart shape",
)

fig.show()
print(fig.generate_tikz())
../_images/tutorials_tutorial_01_5_0.png
% --------------------------------------------- %
% Tikzfigure generated by tikzpics v0.1         %
% https://github.com/max-models/tikzpics        %
% --------------------------------------------- %
\begin{tikzpicture}
    \colorlet{lightred}{red!40!white}

    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfsetlayers{0}

    % Layer 0
    \begin{pgfonlayer}{0}

        % Heart shape
        \draw[draw, rounded corners=20pt, line width=4, color=red, fill=lightred] (2.0, 0.0) to (0.0, 3.5) to (1.0, 4.5) to (2.0, 3.0) to (3.0, 4.5) to (4.0, 3.5) to (3.0, 2.0) -- cycle;
    \end{pgfonlayer}{0}
\end{tikzpicture}

[6]:
fig = TikzFigure()
nodes_star = [
    [0, 0],
    [1, 2.5],
    [2, 0],
    [-0.5, 1.5],
    [2.5, 1.5],
    [0, 0],
]

fig.draw(
    nodes_star,
    options=["draw", "line width=3"],
    layer=0,
    cycle=True,
    color="gold",
    fill="yellow!50!white",
    comment="Star shape",
)
fig.show()
An error occurred while compiling the LaTeX document:

../_images/tutorials_tutorial_01_6_1.png
[7]:
fig = TikzFigure()

N = 200
theta = np.linspace(0, 8 * np.pi, N)
r = np.linspace(0.1, 2, N)
x_spiral = r * np.cos(theta) + 5
y_spiral = r * np.sin(theta) + 2
nodes_spiral = list(zip(x_spiral, y_spiral))

fig.draw(
    nodes_spiral,
    options=["draw", "line width=2"],
    layer=1,
    color="teal",
    comment="Spiral curve",
)
fig.show()
../_images/tutorials_tutorial_01_7_0.png
[8]:
fig = TikzFigure()


# Circle (sun core)
theta = np.linspace(0, 2 * np.pi, 50)
x_circle = np.cos(theta) * 1 + 6
y_circle = np.sin(theta) * 1 + 6
nodes_circle = list(zip(x_circle, y_circle))

fig.filldraw(
    nodes_circle,
    options=["line width=3"],
    layer=0,
    cycle=True,
    color="orange",
    fill="yellow!50!white",
    comment="Sun core",
)

# Rays
for angle in np.linspace(0, 2 * np.pi, 12, endpoint=False):
    x0, y0 = 6 + np.cos(angle), 6 + np.sin(angle)
    x1, y1 = 6 + 1.5 * np.cos(angle), 6 + 1.5 * np.sin(angle)
    fig.draw(
        [(x0, y0), (x1, y1)],
        options=["draw", "line width=2"],
        layer=0,
        color="orange",
        comment="Sun ray",
    )

# TODO: Draw the rays with a tikz loop!
# num_rays = 12
# with fig.loop("k", range(num_rays), comment="Sun rays") as loop_k:
#    angle = 2*np.pi/num_rays * loop_k.index  # angle step
#    x0, y0 = 6 + np.cos(angle), 6 + np.sin(angle)
#    x1, y1 = 6 + 1.5*np.cos(angle), 6 + 1.5*np.sin(angle)
#    loop_k.draw(
#       [(x0, y0), (x1, y1)],
#        options=["draw", "line width=2"],
#       layer=0,
#        color="orange",
#    )

fig.show()
../_images/tutorials_tutorial_01_8_0.png
[9]:
fig = TikzFigure()

theta = np.linspace(0, 2 * np.pi, 50)
x_circle = np.cos(theta) * 1 + 6
y_circle = np.sin(theta) * 1 + 6

# Coordinates for the Olympic rings
positions = [
    (0, 0),  # Blue
    (2.5, 0),  # Black
    (5, 0),  # Red
    (1.25, -1.25),  # Yellow
    (3.75, -1.25),  # Green
]

colors = ["blue", "black", "red", "yellow", "green"]

# TODO: How to make the rings overlap correctly?
# Add the rings
for pos, color in zip(positions, colors):
    nodes_circle = list(zip(x_circle + pos[0], y_circle + pos[1]))
    fig.draw(
        nodes_circle,
        options=["draw", "line width=5"],
        layer=0,
        cycle=True,
        color=color,
        fill="none",
        comment=f"{color} ring",
    )

fig.show()
print(fig.generate_tikz())
../_images/tutorials_tutorial_01_9_0.png
% --------------------------------------------- %
% Tikzfigure generated by tikzpics v0.1         %
% https://github.com/max-models/tikzpics        %
% --------------------------------------------- %
\begin{tikzpicture}

    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfsetlayers{0}

    % Layer 0
    \begin{pgfonlayer}{0}

        % blue ring
        \draw[draw, line width=5, color=blue, fill=none] (7.0, 6.0) to (6.9917900138232465, 6.127877161684506) to (6.9672948630390295, 6.2536545839095075) to (6.926916757346022, 6.375267004879374) to (6.871318704123389, 6.490717552003938) to (6.801413621867956, 6.5981105304912155) to (6.718349350097728, 6.695682550603486) to (6.623489801858733, 6.7818314824680295) to (6.518392568310525, 6.855142763005346) to (6.404783343122394, 6.914412623015813) to (6.284527586631032, 6.95866785303666) to (6.159599895033379, 6.98718178341445) to (6.032051577571655, 6.999486216200688) to (5.903976974092318, 6.995379112949198) to (5.777479066043686, 6.9749279121818235) to (5.654634945578692, 6.938468422049761) to (5.537461709759165, 6.886599306373) to (5.42788333987783, 6.820172254596956) to (5.327699109738683, 6.7402779970753155) to (5.238554041630866, 6.648228395307789) to (5.16191189510816, 6.545534901210549) to (5.099031132097581, 6.433883739117558) to (5.050944252989331, 6.315108218023621) to (5.018440843008935, 6.1911586287013725) to (5.002054607249663, 6.064070219980713) to (5.002054607249663, 5.935929780019287) to (5.018440843008935, 5.808841371298628) to (5.050944252989331, 5.68489178197638) to (5.099031132097581, 5.566116260882442) to (5.161911895108159, 5.454465098789451) to (5.238554041630866, 5.351771604692212) to (5.327699109738683, 5.2597220029246845) to (5.42788333987783, 5.179827745403045) to (5.537461709759165, 5.113400693627) to (5.6546349455786915, 5.06153157795024) to (5.777479066043686, 5.0250720878181765) to (5.903976974092317, 5.004620887050802) to (6.0320515775716546, 5.000513783799312) to (6.159599895033378, 5.01281821658555) to (6.284527586631032, 5.041332146963339) to (6.404783343122393, 5.085587376984187) to (6.518392568310524, 5.144857236994653) to (6.623489801858733, 5.21816851753197) to (6.718349350097727, 5.304317449396513) to (6.801413621867956, 5.401889469508784) to (6.871318704123389, 5.509282447996061) to (6.926916757346022, 5.624732995120626) to (6.9672948630390295, 5.746345416090492) to (6.9917900138232465, 5.8721228383154935) to (7.0, 6.0) -- cycle;

        % black ring
        \draw[draw, line width=5, color=black, fill=none] (9.5, 6.0) to (9.491790013823246, 6.127877161684506) to (9.46729486303903, 6.2536545839095075) to (9.42691675734602, 6.375267004879374) to (9.37131870412339, 6.490717552003938) to (9.301413621867956, 6.5981105304912155) to (9.218349350097728, 6.695682550603486) to (9.123489801858733, 6.7818314824680295) to (9.018392568310524, 6.855142763005346) to (8.904783343122393, 6.914412623015813) to (8.784527586631032, 6.95866785303666) to (8.659599895033379, 6.98718178341445) to (8.532051577571655, 6.999486216200688) to (8.403976974092318, 6.995379112949198) to (8.277479066043686, 6.9749279121818235) to (8.154634945578692, 6.938468422049761) to (8.037461709759164, 6.886599306373) to (7.92788333987783, 6.820172254596956) to (7.827699109738683, 6.7402779970753155) to (7.738554041630866, 6.648228395307789) to (7.66191189510816, 6.545534901210549) to (7.599031132097581, 6.433883739117558) to (7.550944252989331, 6.315108218023621) to (7.518440843008935, 6.1911586287013725) to (7.502054607249663, 6.064070219980713) to (7.502054607249663, 5.935929780019287) to (7.518440843008935, 5.808841371298628) to (7.550944252989331, 5.68489178197638) to (7.599031132097581, 5.566116260882442) to (7.661911895108159, 5.454465098789451) to (7.738554041630866, 5.351771604692212) to (7.827699109738683, 5.2597220029246845) to (7.92788333987783, 5.179827745403045) to (8.037461709759164, 5.113400693627) to (8.154634945578692, 5.06153157795024) to (8.277479066043686, 5.0250720878181765) to (8.403976974092316, 5.004620887050802) to (8.532051577571654, 5.000513783799312) to (8.659599895033377, 5.01281821658555) to (8.784527586631032, 5.041332146963339) to (8.904783343122393, 5.085587376984187) to (9.018392568310524, 5.144857236994653) to (9.123489801858733, 5.21816851753197) to (9.218349350097727, 5.304317449396513) to (9.301413621867956, 5.401889469508784) to (9.37131870412339, 5.509282447996061) to (9.42691675734602, 5.624732995120626) to (9.46729486303903, 5.746345416090492) to (9.491790013823246, 5.8721228383154935) to (9.5, 6.0) -- cycle;

        % red ring
        \draw[draw, line width=5, color=red, fill=none] (12.0, 6.0) to (11.991790013823246, 6.127877161684506) to (11.96729486303903, 6.2536545839095075) to (11.92691675734602, 6.375267004879374) to (11.87131870412339, 6.490717552003938) to (11.801413621867956, 6.5981105304912155) to (11.718349350097728, 6.695682550603486) to (11.623489801858733, 6.7818314824680295) to (11.518392568310524, 6.855142763005346) to (11.404783343122393, 6.914412623015813) to (11.284527586631032, 6.95866785303666) to (11.159599895033379, 6.98718178341445) to (11.032051577571655, 6.999486216200688) to (10.903976974092318, 6.995379112949198) to (10.777479066043686, 6.9749279121818235) to (10.654634945578692, 6.938468422049761) to (10.537461709759164, 6.886599306373) to (10.42788333987783, 6.820172254596956) to (10.327699109738683, 6.7402779970753155) to (10.238554041630866, 6.648228395307789) to (10.16191189510816, 6.545534901210549) to (10.099031132097581, 6.433883739117558) to (10.050944252989332, 6.315108218023621) to (10.018440843008936, 6.1911586287013725) to (10.002054607249663, 6.064070219980713) to (10.002054607249663, 5.935929780019287) to (10.018440843008936, 5.808841371298628) to (10.050944252989332, 5.68489178197638) to (10.099031132097581, 5.566116260882442) to (10.161911895108158, 5.454465098789451) to (10.238554041630866, 5.351771604692212) to (10.327699109738683, 5.2597220029246845) to (10.42788333987783, 5.179827745403045) to (10.537461709759164, 5.113400693627) to (10.654634945578692, 5.06153157795024) to (10.777479066043686, 5.0250720878181765) to (10.903976974092316, 5.004620887050802) to (11.032051577571654, 5.000513783799312) to (11.159599895033377, 5.01281821658555) to (11.284527586631032, 5.041332146963339) to (11.404783343122393, 5.085587376984187) to (11.518392568310524, 5.144857236994653) to (11.623489801858733, 5.21816851753197) to (11.718349350097727, 5.304317449396513) to (11.801413621867956, 5.401889469508784) to (11.87131870412339, 5.509282447996061) to (11.92691675734602, 5.624732995120626) to (11.96729486303903, 5.746345416090492) to (11.991790013823246, 5.8721228383154935) to (12.0, 6.0) -- cycle;

        % yellow ring
        \draw[draw, line width=5, color=yellow, fill=none] (8.25, 4.75) to (8.241790013823246, 4.877877161684506) to (8.21729486303903, 5.0036545839095075) to (8.17691675734602, 5.125267004879374) to (8.12131870412339, 5.240717552003938) to (8.051413621867956, 5.3481105304912155) to (7.968349350097728, 5.445682550603486) to (7.873489801858733, 5.5318314824680295) to (7.768392568310525, 5.605142763005346) to (7.654783343122394, 5.664412623015813) to (7.534527586631032, 5.70866785303666) to (7.409599895033379, 5.73718178341445) to (7.282051577571655, 5.749486216200688) to (7.153976974092318, 5.745379112949198) to (7.027479066043686, 5.7249279121818235) to (6.904634945578692, 5.688468422049761) to (6.787461709759165, 5.636599306373) to (6.67788333987783, 5.570172254596956) to (6.577699109738683, 5.4902779970753155) to (6.488554041630866, 5.398228395307789) to (6.41191189510816, 5.295534901210549) to (6.349031132097581, 5.183883739117558) to (6.300944252989331, 5.065108218023621) to (6.268440843008935, 4.9411586287013725) to (6.252054607249663, 4.814070219980713) to (6.252054607249663, 4.685929780019287) to (6.268440843008935, 4.558841371298628) to (6.300944252989331, 4.43489178197638) to (6.349031132097581, 4.316116260882442) to (6.411911895108159, 4.204465098789451) to (6.488554041630866, 4.101771604692212) to (6.577699109738683, 4.0097220029246845) to (6.67788333987783, 3.9298277454030446) to (6.787461709759165, 3.863400693627) to (6.9046349455786915, 3.81153157795024) to (7.027479066043686, 3.7750720878181765) to (7.153976974092317, 3.7546208870508018) to (7.2820515775716546, 3.7505137837993123) to (7.409599895033378, 3.7628182165855497) to (7.534527586631032, 3.791332146963339) to (7.654783343122393, 3.8355873769841873) to (7.768392568310524, 3.8948572369946532) to (7.873489801858733, 3.9681685175319696) to (7.968349350097727, 4.054317449396513) to (8.051413621867956, 4.151889469508784) to (8.12131870412339, 4.259282447996061) to (8.17691675734602, 4.374732995120626) to (8.21729486303903, 4.496345416090492) to (8.241790013823246, 4.6221228383154935) to (8.25, 4.75) -- cycle;

        % green ring
        \draw[draw, line width=5, color=green, fill=none] (10.75, 4.75) to (10.741790013823246, 4.877877161684506) to (10.71729486303903, 5.0036545839095075) to (10.67691675734602, 5.125267004879374) to (10.62131870412339, 5.240717552003938) to (10.551413621867956, 5.3481105304912155) to (10.468349350097728, 5.445682550603486) to (10.373489801858733, 5.5318314824680295) to (10.268392568310524, 5.605142763005346) to (10.154783343122393, 5.664412623015813) to (10.034527586631032, 5.70866785303666) to (9.909599895033379, 5.73718178341445) to (9.782051577571655, 5.749486216200688) to (9.653976974092318, 5.745379112949198) to (9.527479066043686, 5.7249279121818235) to (9.404634945578692, 5.688468422049761) to (9.287461709759164, 5.636599306373) to (9.17788333987783, 5.570172254596956) to (9.077699109738683, 5.4902779970753155) to (8.988554041630866, 5.398228395307789) to (8.91191189510816, 5.295534901210549) to (8.849031132097581, 5.183883739117558) to (8.800944252989332, 5.065108218023621) to (8.768440843008936, 4.9411586287013725) to (8.752054607249663, 4.814070219980713) to (8.752054607249663, 4.685929780019287) to (8.768440843008936, 4.558841371298628) to (8.800944252989332, 4.43489178197638) to (8.849031132097581, 4.316116260882442) to (8.911911895108158, 4.204465098789451) to (8.988554041630866, 4.101771604692212) to (9.077699109738683, 4.0097220029246845) to (9.17788333987783, 3.9298277454030446) to (9.287461709759164, 3.863400693627) to (9.404634945578692, 3.81153157795024) to (9.527479066043686, 3.7750720878181765) to (9.653976974092316, 3.7546208870508018) to (9.782051577571654, 3.7505137837993123) to (9.909599895033377, 3.7628182165855497) to (10.034527586631032, 3.791332146963339) to (10.154783343122393, 3.8355873769841873) to (10.268392568310524, 3.8948572369946532) to (10.373489801858733, 3.9681685175319696) to (10.468349350097727, 4.054317449396513) to (10.551413621867956, 4.151889469508784) to (10.62131870412339, 4.259282447996061) to (10.67691675734602, 4.374732995120626) to (10.71729486303903, 4.496345416090492) to (10.741790013823246, 4.6221228383154935) to (10.75, 4.75) -- cycle;
    \end{pgfonlayer}{0}
\end{tikzpicture}

[10]:
fig = TikzFigure()

n1 = fig.add_node(content="A", label="n1")

n2 = fig.add_node(options="right of=n1", content="B")

fig.draw([n1, n2], options=["->", "bend left=30"], color="gray")
fig.draw([n2, n1], options=["->", "bend left=30"], color="gray")

fig.show()
print(fig.generate_tikz())




../_images/tutorials_tutorial_01_10_1.png




% --------------------------------------------- %
% Tikzfigure generated by tikzpics v0.1         %
% https://github.com/max-models/tikzpics        %
% --------------------------------------------- %
\begin{tikzpicture}

    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfsetlayers{0}

    % Layer 0
    \begin{pgfonlayer}{0}
        \node (n1) {A};
        \node[right of=n1, ] (node1) {B};
        \draw[->, bend left=30, color=gray] (n1) to (node1);
        \draw[->, bend left=30, color=gray] (node1) to (n1);
    \end{pgfonlayer}{0}
\end{tikzpicture}

[11]:
# Let's try to reproduce this: https://rmwu.github.io/tutorial/latex/2019/11/21/positioning/

fig = TikzFigure(figure_setup="every node/.append style={draw, minimum size=0.8cm}")

fig.add_node(0, 0, draw="none", content="\\textbullet", comment="center")
fig.add_node(0, 0)
fig.add_node(0, 0, options="anchor=center, label=below:{{center}}")

fig.add_node(2, 0, draw="none", content="\\textbullet", comment="West and east")
fig.add_node(2, 0, anchor="west", content="west")
fig.add_node(2, 0, anchor="east", content="east")


fig.add_node(4, 0, draw="none", content="\\textbullet", comment="North and south")
fig.add_node(4, 0, anchor="north", content="north")
fig.add_node(4, 0, anchor="south", content="south")

fig.add_node(6, 0, draw="none", content="\\textbullet", comment="NW, NE, SE, and SW")
fig.add_node(6, 0, anchor="north west", content="nw")
fig.add_node(6, 0, anchor="north east", content="ne")
fig.add_node(6, 0, anchor="south east", content="se")
fig.add_node(6, 0, anchor="south west", content="sw")

fig.show()
../_images/tutorials_tutorial_01_11_0.png
[12]:
import math

fig = TikzFigure()

fig.add_variable("pivar", 3.14)
fig.add_variable("radius", 3)

N = 40
# TODO: Get this example to work with sin and cos and then use fig.loop
for i in range(N):
    frac = round(i / N, 3)  # avoid floating-point noise
    fig.add_node(
        x=f"{{\\radius*{math.cos(frac * 2 * 3.14)}}}",
        y=f"{{\\radius*{math.sin(frac * 2 * 3.14)}}}",
        content="\\textbullet",
    )


fig.add_node("-\\radius", "-\\radius", content="A")
fig.add_node("\\radius", "-\\radius", content="B")
fig.add_node("\\radius", "\\radius", content="C")
fig.add_node("-\\radius", "\\radius", content="D")

# print(fig.generate_standalone())

fig.show()
../_images/tutorials_tutorial_01_12_0.png