Skip to content

Commit d6108b6

Browse files
committed
Added add_raw via a TikzWrapper to the tikzfigure
1 parent 8a81c65 commit d6108b6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/maxplotlib/subfigure/tikz_figure.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def generate_tikz(self):
3636
tikz_script += f"\\end{{pgfonlayer}}{{{self.label}}}\n"
3737
return tikz_script
3838

39+
class TikzWrapper:
40+
def __init__(self, raw_tikz, label="", content="", layer=0, **kwargs):
41+
self.raw_tikz = raw_tikz
42+
self.label = label
43+
self.content = content
44+
self.layer = layer
45+
self.options = kwargs
46+
47+
def to_tikz(self):
48+
return self.raw_tikz
3949

4050
class Node:
4151
def __init__(self, x, y, label="", content="", layer=0, **kwargs):
@@ -198,6 +208,15 @@ def add_path(self, nodes, layer=0, **kwargs):
198208
self.layers[layer].add(path)
199209
return path
200210

211+
def add_raw(self, raw_tikz, layer=0, **kwargs):
212+
tikz = TikzWrapper(raw_tikz)
213+
if layer in self.layers:
214+
self.layers[layer].add(tikz)
215+
else:
216+
self.layers[layer] = Tikzlayer(layer)
217+
self.layers[layer].add(tikz)
218+
return tikz
219+
201220
def get_node(self, node_label):
202221
for node in self.nodes:
203222
if node.label == node_label:

tutorials/tutorial_04.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Tutorial 4.
3+
4+
Add raw tikz code to the tikz subplot.
5+
"""
6+
7+
import maxplotlib.canvas.canvas as canvas
8+
9+
c = canvas.Canvas(width=800, ratio=0.5)
10+
tikz = c.add_tikzfigure(grid=False)
11+
12+
# Add nodes
13+
tikz.add_node(0, 0, 'A', shape='circle', draw='black', fill='blue', layer=0)
14+
tikz.add_node(10, 0, 'B', shape='circle', draw='black', fill='blue', layer=0)
15+
tikz.add_node(10, 10, 'C', shape='circle', draw='black', fill='blue', layer=0)
16+
tikz.add_node(0, 10, 'D', shape='circle', draw='black', fill='blue', layer=2)
17+
18+
19+
# Add a line between nodes
20+
tikz.add_path(['A', 'B', 'C', 'D'], path_actions=['draw', 'rounded corners'], fill='red', opacity=0.5,cycle=True, layer=1)
21+
22+
raw_tikz = r"""
23+
\foreach \i in {0, 45, 90, 135, 180, 225, 270, 315} {
24+
% Place a node at angle \i
25+
\node[circle, draw, fill=green] at (\i:3) (N\i) {};
26+
}
27+
28+
% Draw lines connecting the nodes
29+
\foreach \i/\j in {0/45, 45/90, 90/135, 135/180, 180/225, 225/270, 270/315, 315/0} {
30+
\draw (N\i) -- (N\j);
31+
}
32+
"""
33+
34+
tikz.add_raw(raw_tikz)
35+
36+
tikz.add_node(0.5, 0.5, content='Cube', layer=10)
37+
38+
# Generate the TikZ script
39+
script = tikz.generate_tikz()
40+
print(script)
41+
#print(tikz.generate_standalone())
42+
tikz.compile_pdf('figures/tutorial_04_figure_01.pdf')
43+
#

0 commit comments

Comments
 (0)