diff --git a/pylatex/table.py b/pylatex/table.py index 2c898cc0..76689884 100644 --- a/pylatex/table.py +++ b/pylatex/table.py @@ -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) diff --git a/tests/test_tabular.py b/tests/test_tabular.py new file mode 100644 index 00000000..dc214e81 --- /dev/null +++ b/tests/test_tabular.py @@ -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) +