{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Tutorial 1" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "from tikzpics import TikzFigure\n", "from tikzpics.node import Node\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "n1 = fig.add_node(0, 0, shape=\"circle\", color=\"white\", fill=\"blue\", content=\"Hello!\")\n", "n2 = fig.add_node(5, 0, shape=\"circle\", color=\"white\", fill=\"red\", content=\"Hi!\")\n", "\n", "fig.draw([n1, n2], options=[\"->\", \"line width=2\"], color=\"gray\")\n", "\n", "fig.show()\n", "print(fig.generate_tikz())" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "n1 = Node(0, 0, shape=\"circle\", color=\"white\", fill=\"blue\", content=\"Hello!\")\n", "n2 = Node(5, 0, shape=\"circle\", color=\"white\", fill=\"red\", content=\"Hi!\")\n", "\n", "fig.add([n1, n2])\n", "\n", "fig.draw([n1, n2], options=[\"<-\", \"line width=2\"], color=\"gray\")\n", "\n", "fig.show()\n", "print(fig.generate_tikz())" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "nodes_heart = [\n", " [2, 0],\n", " [0, 3.5],\n", " [1, 4.5],\n", " [2, 3.0],\n", " [3, 4.5],\n", " [4, 3.5],\n", " [3, 2],\n", "]\n", "\n", "fig.colorlet(\"lightred\", \"red!40!white\")\n", "\n", "# TODO: Not every corner should be rounded!\n", "fig.draw(\n", " nodes_heart,\n", " options=[\"draw\", \"rounded corners=20pt\", \"line width=4\"],\n", " layer=0,\n", " cycle=True,\n", " color=\"red\",\n", " fill=\"lightred\",\n", " comment=\"Heart shape\",\n", ")\n", "\n", "fig.show()\n", "print(fig.generate_tikz())" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "nodes_star = [\n", " [0, 0],\n", " [1, 2.5],\n", " [2, 0],\n", " [-0.5, 1.5],\n", " [2.5, 1.5],\n", " [0, 0],\n", "]\n", "\n", "fig.draw(\n", " nodes_star,\n", " options=[\"draw\", \"line width=3\"],\n", " layer=0,\n", " cycle=True,\n", " color=\"gold\",\n", " fill=\"yellow!50!white\",\n", " comment=\"Star shape\",\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "N = 200\n", "theta = np.linspace(0, 8 * np.pi, N)\n", "r = np.linspace(0.1, 2, N)\n", "x_spiral = r * np.cos(theta) + 5\n", "y_spiral = r * np.sin(theta) + 2\n", "nodes_spiral = list(zip(x_spiral, y_spiral))\n", "\n", "fig.draw(\n", " nodes_spiral,\n", " options=[\"draw\", \"line width=2\"],\n", " layer=1,\n", " color=\"teal\",\n", " comment=\"Spiral curve\",\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "\n", "# Circle (sun core)\n", "theta = np.linspace(0, 2 * np.pi, 50)\n", "x_circle = np.cos(theta) * 1 + 6\n", "y_circle = np.sin(theta) * 1 + 6\n", "nodes_circle = list(zip(x_circle, y_circle))\n", "\n", "fig.filldraw(\n", " nodes_circle,\n", " options=[\"line width=3\"],\n", " layer=0,\n", " cycle=True,\n", " color=\"orange\",\n", " fill=\"yellow!50!white\",\n", " comment=\"Sun core\",\n", ")\n", "\n", "# Rays\n", "for angle in np.linspace(0, 2 * np.pi, 12, endpoint=False):\n", " x0, y0 = 6 + np.cos(angle), 6 + np.sin(angle)\n", " x1, y1 = 6 + 1.5 * np.cos(angle), 6 + 1.5 * np.sin(angle)\n", " fig.draw(\n", " [(x0, y0), (x1, y1)],\n", " options=[\"draw\", \"line width=2\"],\n", " layer=0,\n", " color=\"orange\",\n", " comment=\"Sun ray\",\n", " )\n", "\n", "# TODO: Draw the rays with a tikz loop!\n", "# num_rays = 12\n", "# with fig.loop(\"k\", range(num_rays), comment=\"Sun rays\") as loop_k:\n", "# angle = 2*np.pi/num_rays * loop_k.index # angle step\n", "# x0, y0 = 6 + np.cos(angle), 6 + np.sin(angle)\n", "# x1, y1 = 6 + 1.5*np.cos(angle), 6 + 1.5*np.sin(angle)\n", "# loop_k.draw(\n", "# [(x0, y0), (x1, y1)],\n", "# options=[\"draw\", \"line width=2\"],\n", "# layer=0,\n", "# color=\"orange\",\n", "# )\n", "\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "theta = np.linspace(0, 2 * np.pi, 50)\n", "x_circle = np.cos(theta) * 1 + 6\n", "y_circle = np.sin(theta) * 1 + 6\n", "\n", "# Coordinates for the Olympic rings\n", "positions = [\n", " (0, 0), # Blue\n", " (2.5, 0), # Black\n", " (5, 0), # Red\n", " (1.25, -1.25), # Yellow\n", " (3.75, -1.25), # Green\n", "]\n", "\n", "colors = [\"blue\", \"black\", \"red\", \"yellow\", \"green\"]\n", "\n", "# TODO: How to make the rings overlap correctly?\n", "# Add the rings\n", "for pos, color in zip(positions, colors):\n", " nodes_circle = list(zip(x_circle + pos[0], y_circle + pos[1]))\n", " fig.draw(\n", " nodes_circle,\n", " options=[\"draw\", \"line width=5\"],\n", " layer=0,\n", " cycle=True,\n", " color=color,\n", " fill=\"none\",\n", " comment=f\"{color} ring\",\n", " )\n", "\n", "fig.show()\n", "print(fig.generate_tikz())" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "fig = TikzFigure()\n", "\n", "n1 = fig.add_node(content=\"A\", label=\"n1\")\n", "\n", "n2 = fig.add_node(options=\"right of=n1\", content=\"B\")\n", "\n", "fig.draw([n1, n2], options=[\"->\", \"bend left=30\"], color=\"gray\")\n", "fig.draw([n2, n1], options=[\"->\", \"bend left=30\"], color=\"gray\")\n", "\n", "fig.show()\n", "print(fig.generate_tikz())" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "# Let's try to reproduce this: https://rmwu.github.io/tutorial/latex/2019/11/21/positioning/\n", "\n", "fig = TikzFigure(figure_setup=\"every node/.append style={draw, minimum size=0.8cm}\")\n", "\n", "fig.add_node(0, 0, draw=\"none\", content=\"\\\\textbullet\", comment=\"center\")\n", "fig.add_node(0, 0)\n", "fig.add_node(0, 0, options=\"anchor=center, label=below:{{center}}\")\n", "\n", "fig.add_node(2, 0, draw=\"none\", content=\"\\\\textbullet\", comment=\"West and east\")\n", "fig.add_node(2, 0, anchor=\"west\", content=\"west\")\n", "fig.add_node(2, 0, anchor=\"east\", content=\"east\")\n", "\n", "\n", "fig.add_node(4, 0, draw=\"none\", content=\"\\\\textbullet\", comment=\"North and south\")\n", "fig.add_node(4, 0, anchor=\"north\", content=\"north\")\n", "fig.add_node(4, 0, anchor=\"south\", content=\"south\")\n", "\n", "fig.add_node(6, 0, draw=\"none\", content=\"\\\\textbullet\", comment=\"NW, NE, SE, and SW\")\n", "fig.add_node(6, 0, anchor=\"north west\", content=\"nw\")\n", "fig.add_node(6, 0, anchor=\"north east\", content=\"ne\")\n", "fig.add_node(6, 0, anchor=\"south east\", content=\"se\")\n", "fig.add_node(6, 0, anchor=\"south west\", content=\"sw\")\n", "\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "fig = TikzFigure()\n", "\n", "fig.add_variable(\"pivar\", 3.14)\n", "fig.add_variable(\"radius\", 3)\n", "\n", "N = 40\n", "# TODO: Get this example to work with sin and cos and then use fig.loop\n", "for i in range(N):\n", " frac = round(i / N, 3) # avoid floating-point noise\n", " fig.add_node(\n", " x=f\"{{\\\\radius*{math.cos(frac * 2 * 3.14)}}}\",\n", " y=f\"{{\\\\radius*{math.sin(frac * 2 * 3.14)}}}\",\n", " content=\"\\\\textbullet\",\n", " )\n", "\n", "\n", "fig.add_node(\"-\\\\radius\", \"-\\\\radius\", content=\"A\")\n", "fig.add_node(\"\\\\radius\", \"-\\\\radius\", content=\"B\")\n", "fig.add_node(\"\\\\radius\", \"\\\\radius\", content=\"C\")\n", "fig.add_node(\"-\\\\radius\", \"\\\\radius\", content=\"D\")\n", "\n", "# print(fig.generate_standalone())\n", "\n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "env_tikzpics", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }