From 9ebd11ba49e2385a036833f953a537f203af5514 Mon Sep 17 00:00:00 2001 From: EmoBurrito Date: Wed, 18 Apr 2018 15:17:52 -0600 Subject: [PATCH 1/2] Added row height modifier to both normal and empty rows. Added colour option empty row. Commented a suggestion to consider. --- pylatex/table.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/pylatex/table.py b/pylatex/table.py index 0aada0ee..324c3724 100644 --- a/pylatex/table.py +++ b/pylatex/table.py @@ -198,13 +198,36 @@ def add_hline(self, start=None, end=None, *, color=None, self.append(Command(cline, dumps_list([start, NoEscape('-'), end]))) - def add_empty_row(self): - """Add an empty row to the table.""" + # To prevent repeating code, is there some way that we can just + # have calls to this method in turn call add_row but the cells + # are all just empty? + def add_empty_row(self, color=None, height_modifier=None): + """Add an empty row to the table. - self.append(NoEscape((self.width - 1) * '&' + r'\\')) + Args + ---- + color: str + The name of the color used to highlight the row + height_modifier: str + The height modifier appended to the end of a table row. + Example: `Some&Cells&Here\\\\[-0.5em]` + """ + + if color: + if not self.color: + self.packages.append(Package("xcolor", options='table')) + self.color = True + color_command = Command(command="rowcolor", arguments=color) + self.append(color_command) - def add_row(self, *cells, color=None, escape=None, mapper=None, - strict=True): + to_append = NoEscape((self.width - 1) * '&' + r'\\') + if height_modifier: + to_append += NoEscape('[{}]'.format(height_modifier)) + + self.append(to_append) + + def add_row(self, *cells, color=None, escape=None, height_modifier=None, + mapper=None, strict=True): """Add a row of cells to the table. Args @@ -216,6 +239,9 @@ def add_row(self, *cells, color=None, escape=None, mapper=None, contents. color: str The name of the color used to highlight the row + height_modifier: str + The height modifier appended to the end of a table row. + Example: `Some&Cells&Here\\\\[-0.5em]` mapper: callable or `list` A function or a list of functions that should be called on all entries of the list after converting them to a string, @@ -223,8 +249,8 @@ def add_row(self, *cells, color=None, escape=None, mapper=None, strict: bool Check for correct count of cells in row or not. """ - - if len(cells) == 1 and _is_iterable(cells): + + if len(cells) == 1 and _is_iterable(cells): cells = cells[0] if escape is None: @@ -257,9 +283,12 @@ def add_row(self, *cells, color=None, escape=None, mapper=None, color_command = Command(command="rowcolor", arguments=color) self.append(color_command) - self.append(dumps_list(cells, escape=escape, token='&', - mapper=mapper) + NoEscape(r'\\')) + to_append = dumps_list(cells, escape=escape, token='&', + mapper=mapper) + NoEscape(r'\\') + if height_modifier: + to_append += NoEscape('[{}]'.format(height_modifier)) + self.append(to_append) class Tabularx(Tabular): """A class that represents a tabularx environment.""" From 9366ed4b8b98d8b8a29fafdfa8172685a56bf769 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Wed, 18 Apr 2018 16:09:07 -0600 Subject: [PATCH 2/2] Four missing spaces caused error --- pylatex/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylatex/table.py b/pylatex/table.py index 324c3724..0f32bde0 100644 --- a/pylatex/table.py +++ b/pylatex/table.py @@ -250,7 +250,7 @@ def add_row(self, *cells, color=None, escape=None, height_modifier=None, Check for correct count of cells in row or not. """ - if len(cells) == 1 and _is_iterable(cells): + if len(cells) == 1 and _is_iterable(cells): cells = cells[0] if escape is None: