From af4fe63f66f26566e83ca832895b528236050548 Mon Sep 17 00:00:00 2001 From: Kylie MacFarquharson Date: Fri, 4 Nov 2022 21:08:37 +0000 Subject: [PATCH 1/2] Add option for replacing zeros in matrix with \cdot, common in mathematics. --- pylatex/math.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pylatex/math.py b/pylatex/math.py index ebe52696..272682e0 100644 --- a/pylatex/math.py +++ b/pylatex/math.py @@ -100,7 +100,7 @@ class Matrix(Environment): 'alignment': 'arguments', } - def __init__(self, matrix, *, mtype='p', alignment=None): + def __init__(self, matrix, *, mtype='p', alignment=None, replace_zeros=False): r""" Args ---- @@ -113,6 +113,8 @@ def __init__(self, matrix, *, mtype='p', alignment=None): alignment: str How to align the content of the cells in the matrix. This is ``c`` by default. + replace_zeros: bool + Should zeros in the matrix be replaced with a cdot. References ---------- @@ -127,6 +129,7 @@ def __init__(self, matrix, *, mtype='p', alignment=None): self._mtype = mtype if alignment is not None: self.latex_name += '*' + self._replace_zeros = replace_zeros super().__init__(arguments=alignment) @@ -146,7 +149,7 @@ def dumps_content(self): for (y, x), value in np.ndenumerate(self.matrix): if x: string += '&' - string += str(value) + string += str(value) if not (value == 0 and self._replace_zeros) else '\cdot' if x == shape[1] - 1 and y != shape[0] - 1: string += r'\\' + '%\n' From 6eb087a110a23fe597f1f511a632b5f89dc09e99 Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Tue, 23 Jul 2024 23:18:16 +0200 Subject: [PATCH 2/2] Fix merge --- pylatex/math.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pylatex/math.py b/pylatex/math.py index 233d4363..f7c26fdb 100644 --- a/pylatex/math.py +++ b/pylatex/math.py @@ -128,6 +128,7 @@ def __init__(self, matrix, *, mtype="p", alignment=None, replace_zeros=False): self.latex_name = mtype + "matrix" self._mtype = mtype if alignment is not None: + self.latex_name += "*" self._replace_zeros = replace_zeros super().__init__(arguments=alignment) @@ -147,8 +148,10 @@ def dumps_content(self): for (y, x), value in np.ndenumerate(self.matrix): if x: - string += '&' - string += str(value) if not (value == 0 and self._replace_zeros) else '\cdot' + string += "&" + string += ( + str(value) if not (value == 0 and self._replace_zeros) else "\cdot" + ) if x == shape[1] - 1 and y != shape[0] - 1: string += r"\\" + "%\n"