Skip to content
Vince Buffalo edited this page Aug 27, 2017 · 5 revisions

Numpy Array to LaTeX Matrix

Sypya's sympy.latex(sympy.Matrix(ary)) function will take a numpy array ary and convert it to LaTeX. You can use:

from sympy import init_printing
init_printing()

to turn on automatic printing in a notebook.

If you don't want to turn on autoprinting globally, you can use:

from IPython.display import display, Latex
Latex(display(sympy.latex(sympy.Matrix(ary))))
def ppa(ary, name=None):
    css = """.output { align-items: center; }"""
    if len(ary.shape) == 1:
        latex = sympy.latex(sympy.Array(ary))
    if len(ary.shape) == 2:
        latex = sympy.latex(sympy.Matrix(ary))
    else:
        raise ValueError("numpy arrays with dimension > 2 not supported.")
    if name is None:
        display(HTML(f'<style>{latex}</style>'.format(CSS)))
    else:
        display(HTML(f'{name} = <style>{latex}</style>'.format(CSS)))
Clone this wiki locally