Skip to content

Commit a32587c

Browse files
committed
Added layers to nodes and paths
1 parent e1fdbc3 commit a32587c

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/maxplotlib/subfigure/tikz_figure.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from maxplotlib.linestyle.linestyle import Linestyle
1212

1313
class Node:
14-
def __init__(self, x, y, label="", content="",**kwargs):
14+
def __init__(self, x, y, label="", content="", layer=0, **kwargs):
1515
"""
1616
Represents a TikZ node.
1717
@@ -25,6 +25,7 @@ def __init__(self, x, y, label="", content="",**kwargs):
2525
self.y = y
2626
self.label = label
2727
self.content = content
28+
self.layer = layer
2829
self.options = kwargs
2930

3031
def to_tikz(self):
@@ -34,13 +35,13 @@ def to_tikz(self):
3435
Returns:
3536
- tikz_str (str): TikZ code string for the node.
3637
"""
37-
options = ', '.join(f"{k.replace('_', ' ')}={{{v}}}" for k, v in self.options.items())
38+
options = ', '.join(f"{k.replace('_', ' ')}={v}" for k, v in self.options.items())
3839
if options:
3940
options = f"[{options}]"
4041
return f" \\node{options} ({self.label}) at ({self.x}, {self.y}) {{{self.content}}};\n"
4142

4243
class Path:
43-
def __init__(self, nodes, **kwargs):
44+
def __init__(self, nodes, path_actions=[], cycle=False, layer=0, **kwargs):
4445
"""
4546
Represents a path (line) connecting multiple nodes.
4647
@@ -49,6 +50,9 @@ def __init__(self, nodes, **kwargs):
4950
- **kwargs: Additional TikZ path options (e.g., style, color).
5051
"""
5152
self.nodes = nodes
53+
self.path_actions = path_actions
54+
self.cycle = cycle
55+
self.layer = layer
5256
self.options = kwargs
5357

5458
def to_tikz(self):
@@ -58,10 +62,14 @@ def to_tikz(self):
5862
Returns:
5963
- tikz_str (str): TikZ code string for the path.
6064
"""
61-
options = ', '.join(f"{k.replace('_', ' ')}={{{v}}}" for k, v in self.options.items())
65+
options = ', '.join(f"{k.replace('_', ' ')}={v}" for k, v in self.options.items())
66+
if len(self.path_actions) > 0:
67+
options = ', '.join(self.path_actions) + ', ' + options
6268
if options:
6369
options = f"[{options}]"
64-
path_str = ' -- '.join(f"({node_label})" for node_label in self.nodes)
70+
path_str = ' -- '.join(f"({node_label}.center)" for node_label in self.nodes)
71+
if self.cycle:
72+
path_str += ' -- cycle'
6573
return f" \\draw{options} {path_str};\n"
6674

6775
class TikzFigure:
@@ -88,11 +96,12 @@ def __init__(self, **kwargs):
8896
# Initialize lists to hold Node and Path objects
8997
self.nodes = []
9098
self.paths = []
99+
self.layers = {}
91100

92101
# Counter for unnamed nodes
93102
self._node_counter = 0
94103

95-
def add_node(self, x, y, label=None, content="", **kwargs):
104+
def add_node(self, x, y, label=None, content="", layer = 0, **kwargs):
96105
"""
97106
Add a node to the TikZ figure.
98107
@@ -109,10 +118,14 @@ def add_node(self, x, y, label=None, content="", **kwargs):
109118
label = f"node{self._node_counter}"
110119
node = Node(x=x, y=y, label=label, content=content, **kwargs)
111120
self.nodes.append(node)
121+
if layer in self.layers:
122+
self.layers[layer].append(node)
123+
else:
124+
self.layers[layer] = [node]
112125
self._node_counter += 1
113126
return node
114127

115-
def add_path(self, nodes, **kwargs):
128+
def add_path(self, nodes, layer=0, **kwargs):
116129
"""
117130
Add a line or path connecting multiple nodes.
118131
@@ -136,6 +149,10 @@ def add_path(self, nodes, **kwargs):
136149

137150
path = Path(nodes, **kwargs)
138151
self.paths.append(path)
152+
if layer in self.layers:
153+
self.layers[layer].append(path)
154+
else:
155+
self.layers[layer] = [path]
139156
return path
140157

141158
def generate_tikz(self):
@@ -147,17 +164,22 @@ def generate_tikz(self):
147164
"""
148165
tikz_script = "\\begin{tikzpicture}\n"
149166

167+
150168
# Add grid if enabled
151169
if self._grid:
152170
tikz_script += " \\draw[step=1cm, gray, very thin] (-10,-10) grid (10,10);\n"
153171

154-
# Add nodes
155-
for node in self.nodes:
156-
tikz_script += node.to_tikz()
157-
158-
# Add paths
159-
for path in self.paths:
160-
tikz_script += path.to_tikz()
172+
for key, layer_items in self.layers.items():
173+
tikz_script += f"\n % Layer {key}\n"
174+
for item in layer_items:
175+
tikz_script += item.to_tikz()
176+
# # Add nodes
177+
# for node in self.nodes:
178+
# tikz_script += node.to_tikz()
179+
180+
# # Add paths
181+
# for path in self.paths:
182+
# tikz_script += path.to_tikz()
161183

162184
tikz_script += "\\end{tikzpicture}"
163185

0 commit comments

Comments
 (0)