Tutorial 4

[1]:
"""
Tutorial 4.

Add raw tikz code to the tikz subplot.
"""
[1]:
'\nTutorial 4.\n\nAdd raw tikz code to the tikz subplot.\n'
[2]:
from maxplotlib import Canvas
[3]:
c = Canvas(width=800, ratio=0.5)
tikz = c.add_tikzfigure(grid=False)
[4]:
# Add nodes
tikz.add_node(0, 0, "A", shape="circle", draw="black", fill="blue", layer=0)
tikz.add_node(10, 0, "B", shape="circle", draw="black", fill="blue", layer=0)
tikz.add_node(10, 10, "C", shape="circle", draw="black", fill="blue", layer=0)
tikz.add_node(0, 10, "D", shape="circle", draw="black", fill="blue", layer=2)
[4]:
<maxplotlib.subfigure.tikz_figure.Node at 0x7f6884aa1e20>
[5]:
# Add a line between nodes
tikz.add_path(
    ["A", "B", "C", "D"],
    path_actions=["draw", "rounded corners"],
    fill="red",
    opacity=0.5,
    cycle=True,
    layer=1,
)
[5]:
<maxplotlib.subfigure.tikz_figure.Path at 0x7f6884aa0fb0>
[6]:
raw_tikz = r"""
\foreach \i in {0, 45, 90, 135, 180, 225, 270, 315} {
    % Place a node at angle \i
    \node[circle, draw, fill=green] at (\i:3) (N\i) {};
}

% Draw lines connecting the nodes
\foreach \i/\j in {0/45, 45/90, 90/135, 135/180, 180/225, 225/270, 270/315, 315/0} {
    \draw (N\i) -- (N\j);
}
"""
[7]:
tikz.add_raw(raw_tikz)
[7]:
<maxplotlib.subfigure.tikz_figure.TikzWrapper at 0x7f6884aa22a0>
[8]:
tikz.add_node(0.5, 0.5, content="Cube", layer=10)
[8]:
<maxplotlib.subfigure.tikz_figure.Node at 0x7f6884aa1c10>
[9]:
# Generate the TikZ script
script = tikz.generate_tikz()
print(script)
# print(tikz.generate_standalone())
# tikz.compile_pdf("tutorial_04_01.pdf")
#
\begin{tikzpicture}
    % Define the layers library
    \pgfdeclarelayer{0}
    \pgfdeclarelayer{1}
    \pgfdeclarelayer{10}
    \pgfdeclarelayer{2}
    \pgfsetlayers{0,1,10,2}

    % Layer 0
    \begin{pgfonlayer}{0}
        \node[shape=circle, draw=black, fill=blue] (A) at (0, 0) {};
        \node[shape=circle, draw=black, fill=blue] (B) at (10, 0) {};
        \node[shape=circle, draw=black, fill=blue] (C) at (10, 10) {};

        \foreach \i in {0, 45, 90, 135, 180, 225, 270, 315} {
            % Place a node at angle \i
            \node[circle, draw, fill=green] at (\i:3) (N\i) {};
        }

        % Draw lines connecting the nodes
        \foreach \i/\j in {0/45, 45/90, 90/135, 135/180, 180/225, 225/270, 270/315, 315/0} {
            \draw (N\i) -- (N\j);
        }
    \end{pgfonlayer}{0}

    % Layer 2
    \begin{pgfonlayer}{2}
        \node[shape=circle, draw=black, fill=blue] (D) at (0, 10) {};
    \end{pgfonlayer}{2}

    % Layer 1
    \begin{pgfonlayer}{1}
        \draw[draw, rounded corners, fill=red, opacity=0.5] (A.center) to (B.center) to (C.center) to (D.center) -- cycle;
    \end{pgfonlayer}{1}

    % Layer 10
    \begin{pgfonlayer}{10}
        \node (node4) at (0.5, 0.5) {Cube};
    \end{pgfonlayer}{10}
\end{tikzpicture}