Created
May 5, 2020 13:57
-
-
Save lukeolson/183a37627a139a7e5899a670baead77f to your computer and use it in GitHub Desktop.
convert a numpy array to latex array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def totex(A): | |
m, n = A.shape | |
rstr = "".join(['r' for i in range(n)]) | |
print('\\left[\\begin{array}{' + rstr + r'}') | |
for i in range(m): | |
for j in range(n): | |
if A[i,j] == 0: | |
print(' ', end='') | |
else: | |
a = A[i,j] | |
if float(a) == float(int(a)): | |
a = int(a) | |
print(f'{a}', end='') | |
if j != (n-1): | |
print(' & ', end='') | |
else: | |
if i != (m-1): | |
print(' \\\\') | |
else: | |
print('') | |
print('\\end{array}\\right]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment