Numpy 2d and 1d array to latex bmatrix
I am not satisfied with using the printed output from Python. The matrix may be too big, which causes wrapping. This is code for to print the LaTeX text for a 2d matrix.
def bmatrix(a):
text = r'$\left[\begin{array}{*{'
text += str(len(a[0]))
text += r'}c}'
text += '\n'
for x in range(len(a)):
for y in range(len(a[x])):
text += str(a[x][y])
text += r' & '
text = text[:-2]
text += r'\\'
text += '\n'
text += r'\end{array}\right]$'
print text
Which gives this
$\left[\begin{array}{*{16}c}
2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 \\
0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 \\
0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \\
-1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 & -1 \\
-1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 & 0 \\
0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 & 0 \\
0 & 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 & 2.01 \\
\end{array}\right]$
Try array_to_latex (pip install)
. I wrote it for this very reason. Please provide your feedback where it falls short.
It has defaults, but also lets you customize formats (exponential, number of decimal places) and handles complex numbers as well and can "pop" the results right into your clipboard (no need to copy text dumped to the screen).
Some examples in the github repository. https://github.com/josephcslater/array_to_latex
The __str__
method of the numpy array already does most of the formatting for you. Let's exploit that;
import numpy as np
def bmatrix(a):
"""Returns a LaTeX bmatrix
:a: numpy array
:returns: LaTeX bmatrix as a string
"""
if len(a.shape) > 2:
raise ValueError('bmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{bmatrix}']
return '\n'.join(rv)
A = np.array([[12, 5, 2], [20, 4, 8], [ 2, 4, 3], [ 7, 1, 10]])
print bmatrix(A) + '\n'
B = np.array([[1.2], [3.7], [0.2]])
print bmatrix(B) + '\n'
C = np.array([1.2, 9.3, 0.6, -2.1])
print bmatrix(C) + '\n'
This returns:
\begin{bmatrix}
12 & 5 & 2\\
20 & 4 & 8\\
2 & 4 & 3\\
7 & 1 & 10\\
\end{bmatrix}
\begin{bmatrix}
1.2\\
3.7\\
0.2\\
\end{bmatrix}
\begin{bmatrix}
1.2 & 9.3 & 0.6 & -2.1\\
\end{bmatrix}