Skip to content

Commit

Permalink
Add option for replacing zeros in matrix with \cdot, common in mathem…
Browse files Browse the repository at this point in the history
…atics.
  • Loading branch information
Kylie MacFarquharson committed Nov 4, 2022
1 parent 1a73261 commit af4fe63
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pylatex/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand All @@ -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
----------
Expand All @@ -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)

Expand All @@ -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'
Expand Down

0 comments on commit af4fe63

Please sign in to comment.