Skip to content

Commit edef12c

Browse files
authored
Fix package dependency propagation in tables (#371)
Fix #331 by flattening the tree and adding the packages of all descendants.
1 parent 3b19b9e commit edef12c

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

pylatex/table.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,14 @@ def add_row(self, *cells, color=None, escape=None, mapper=None, strict=True):
242242
escape = self.escape
243243

244244
# Propagate packages used in cells
245-
for c in cells:
245+
def flatten(x):
246+
if _is_iterable(x):
247+
return [a for i in x for a in flatten(i)]
248+
else:
249+
return [x]
250+
251+
flat_list = [c for c in cells] + flatten(cells)
252+
for c in flat_list:
246253
if isinstance(c, LatexObject):
247254
for p in c.packages:
248255
self.packages.add(p)

tests/test_tabular.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
3+
from pylatex import Document, Section, Tabular, MultiColumn, StandAloneGraphic
4+
5+
# This file contains function that test several Tabular related functionality.
6+
7+
def test_tabular_can_add_row_passing_many_arguments(sample_logo_path):
8+
"""
9+
Test that Tabular can add a row as described in the function body:
10+
The first method is to pass the content of each cell as a separate argument.
11+
12+
Returns
13+
-------
14+
None.
15+
16+
"""
17+
doc = Document()
18+
19+
with doc.create(Section('Can Add Row Passing Many Arguments')):
20+
with doc.create(Tabular("|c|c|", booktabs=True)) as table:
21+
22+
mc1 = MultiColumn(1, align='l', data=StandAloneGraphic(
23+
filename=sample_logo_path))
24+
mc2 = MultiColumn(1, align='l', data=StandAloneGraphic(
25+
filename=sample_logo_path))
26+
27+
28+
table.add_row(mc1, mc2)
29+
doc.generate_pdf(clean_tex=False)
30+
31+
32+
def test_tabular_can_add_row_passing_iterable(sample_logo_path):
33+
"""
34+
Test that Tabular can add a row as described in the function body:
35+
The second method
36+
is to pass a single argument that is an iterable that contains each
37+
contents.
38+
39+
Returns
40+
-------
41+
None.
42+
43+
"""
44+
doc = Document()
45+
46+
with doc.create(Section('Can Add Row Passing Iterable')):
47+
with doc.create(Tabular("|c|c|", booktabs=True)) as table:
48+
multi_columns_array = [
49+
MultiColumn(1, align='l', data=StandAloneGraphic(
50+
filename=sample_logo_path)),
51+
MultiColumn(1, align='l', data=StandAloneGraphic(
52+
filename=sample_logo_path))
53+
]
54+
55+
table.add_row(multi_columns_array)
56+
doc.generate_pdf()
57+
58+
if __name__ == '__main__':
59+
60+
import os.path as osp
61+
62+
sample_logo_path = osp.abspath(osp.join(
63+
__file__[0:-15], "..", "examples", "sample-logo.png"))
64+
65+
test_tabular_can_add_row_passing_many_arguments(
66+
sample_logo_path=sample_logo_path)
67+
test_tabular_can_add_row_passing_iterable(
68+
sample_logo_path=sample_logo_path)
69+

0 commit comments

Comments
 (0)