Convert ndarray to Tex matrix string (Feature Suggestion)
See original GitHub issueAs a linear algebra student I find myself checking my answers and computing busy work with NumPy; I could imagine that thousands of others who work with linear algebra and write up their work in papers/articles/problem sets could also face this. One consistent source of pain has been manually typing out arrays I obtain within NumPy into my LaTeX write-ups. To automatically convert 2-dimensional NumPy ndarrays to a string that LaTeX would accept I wrote the following function
def np2tex(np_arr, latex_type='bmatrix'):
"""
Convert a NumPy array to a LaTeX matrix string.
"""
if len(np_arr.shape) != 2:
raise ValueError("incorrect shape")
inner_str = ' \\\\ '.join([' & '.join(str(row)[1:-1].split()) for row in np_arr])
latex_out = '\\begin{' + latex_type + '} ' + inner_str + ' \\end{' + latex_type + '}'
return latex_out
Is this something that could be added in some way? Please advise as to whether a PR could be possible (this would be very useful for me, as I wouldn’t have to edit my own NumPy source code or add a custom module to my own system). Maybe it could be rolled into array2string in some way with a parameter?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
I think IPython let’s you define
_repr_latex_for types you don’t own - you might want to look into doing that.Cool, thanks for being helpful!