Skip to content

Commit

Permalink
Fix package dependency propagation in tables (#371)
Browse files Browse the repository at this point in the history
Fix #331 by flattening the tree
and adding the packages of all descendants.
  • Loading branch information
neoglez authored Jul 23, 2024
1 parent 3b19b9e commit edef12c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pylatex/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,14 @@ def add_row(self, *cells, color=None, escape=None, mapper=None, strict=True):
escape = self.escape

# Propagate packages used in cells
for c in cells:
def flatten(x):
if _is_iterable(x):
return [a for i in x for a in flatten(i)]
else:
return [x]

flat_list = [c for c in cells] + flatten(cells)
for c in flat_list:
if isinstance(c, LatexObject):
for p in c.packages:
self.packages.add(p)
Expand Down
69 changes: 69 additions & 0 deletions tests/test_tabular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python

from pylatex import Document, Section, Tabular, MultiColumn, StandAloneGraphic

# This file contains function that test several Tabular related functionality.

def test_tabular_can_add_row_passing_many_arguments(sample_logo_path):
"""
Test that Tabular can add a row as described in the function body:
The first method is to pass the content of each cell as a separate argument.
Returns
-------
None.
"""
doc = Document()

with doc.create(Section('Can Add Row Passing Many Arguments')):
with doc.create(Tabular("|c|c|", booktabs=True)) as table:

mc1 = MultiColumn(1, align='l', data=StandAloneGraphic(
filename=sample_logo_path))
mc2 = MultiColumn(1, align='l', data=StandAloneGraphic(
filename=sample_logo_path))


table.add_row(mc1, mc2)
doc.generate_pdf(clean_tex=False)


def test_tabular_can_add_row_passing_iterable(sample_logo_path):
"""
Test that Tabular can add a row as described in the function body:
The second method
is to pass a single argument that is an iterable that contains each
contents.
Returns
-------
None.
"""
doc = Document()

with doc.create(Section('Can Add Row Passing Iterable')):
with doc.create(Tabular("|c|c|", booktabs=True)) as table:
multi_columns_array = [
MultiColumn(1, align='l', data=StandAloneGraphic(
filename=sample_logo_path)),
MultiColumn(1, align='l', data=StandAloneGraphic(
filename=sample_logo_path))
]

table.add_row(multi_columns_array)
doc.generate_pdf()

if __name__ == '__main__':

import os.path as osp

sample_logo_path = osp.abspath(osp.join(
__file__[0:-15], "..", "examples", "sample-logo.png"))

test_tabular_can_add_row_passing_many_arguments(
sample_logo_path=sample_logo_path)
test_tabular_can_add_row_passing_iterable(
sample_logo_path=sample_logo_path)

0 comments on commit edef12c

Please sign in to comment.