numpyarray_to_latex

About

CircleCI

Format numpy arrays as LaTeX arrays. This is a fork of array_to_latex by @josephcslater.

>>> import numpy as np
>>> from numpyarray_to_latex import to_ltx
>>> tex = to_ltx(np.random.randn(2,2))
>>> print(tex)
\left(
\begin{array}
  2.0156 & -0.3230\\
  0.0477 &  0.0184
\end{array}
\right)

Install

pip install numpyarray_to_latex

numpyarray_to_latex was developed and tested for

  • Python 3.6

  • Python 3.7

  • Python 3.8

  • Python 3.9

  • Python 3.10

So far, the package's functionality was tested on Mac OS X and CentOS only.

Dependencies

numpyarray_to_latex directly depends on the following packages which will be installed by pip during the installation process

  • numpy>=1.0

Documentation

The full documentation is available at numpyarray-to-latex.readthedocs.io.

Examples

Default Python

import numpy as np
from numpyarray_to_latex.jupyter import to_jup
from numpyarray_to_latex import to_ltx

tex = to_ltx(np.random.randn(2,2))
print(tex)
\left(
\begin{array}
  2.0156 & -0.3230\\
  0.0477 &  0.0184
\end{array}
\right)
print(to_ltx(np.random.randn(2,2),
       latexarraytype='array',
       is_row_vector=False,
       mark_color='yellow',
       mark_elements=[(1,1)],
       brackets='(]',
       separate_columns=[1,2],
       separate_rows=[1,2],))
\left(
\begin{array}{c|c}
  0.3498 &  0.4393\\
  \hline
 -0.8521 & \colorbox{yellow}{$-0.6412$}
\end{array}
\right]

In Jupyter Notebooks

import numpy as np
from numpyarray_to_latex.jupyter import to_jup

to_jup(np.random.randn(10,10),
       mark_elements=[(1,1),(2,3),(7,0)],
       separate_columns=[1,2], # columns & rows that don't exist will be ignored
       separate_rows=[1,2],
      )
01
to_jup(np.random.randn(2),
       mark_elements=[1,],
       separate_columns=[1,2],
       separate_rows=[1,2],
      )
02
to_jup(np.random.randn(2),
       is_row_vector=False,
       mark_elements=[1,],
       separate_columns=[1,2],
       separate_rows=[1,2,3,4], # columns that don't exist will be ignored
       mark_color='yellow',
       brackets='(]',
      )
03
to_jup(np.random.randn(2,2),
       latexarraytype='Vmatrix',
       is_row_vector=False,
       separate_rows=[1],
      )
04
to_jup(np.random.randn(2,2)+1j*np.random.randn(2,2),
       mark_elements=[(0,1)],
      )
05
to_jup(np.random.randn(2,2)+1j*np.random.randn(2,2),
       mark_elements=[(0,1)],
       fmt='{:4.2e}',
      )
06

Changelog

Changes are logged in a separate file.

License

This project is licensed under the MIT License. Note that this excludes any images/pictures/figures shown here or in the documentation.

Contributing

If you want to contribute to this project, please make sure to read the code of conduct and the contributing guidelines. In case you're wondering about what to contribute, we're always collecting ideas of what we want to implement next in the outlook notes.

Contributor Covenant

Dev notes

Fork this repository, clone it, and install it in dev mode.

git clone git@github.com:YOURUSERNAME/numpyarray_to_latex.git
make

If you want to upload to PyPI, first convert the new README.md to README.rst

make readme

It will give you warnings about bad .rst-syntax. Fix those errors in README.rst. Then wrap the whole thing

make pypi

It will probably give you more warnings about .rst-syntax. Fix those until the warnings disappear. Then do

make upload

Pure Python

import numpy as np
from numpyarray_to_latex.jupyter import to_jup
from numpyarray_to_latex import to_ltx

tex = to_ltx(np.random.randn(2,2))
print(tex)
left(
begin{array}
  2.0156 & -0.3230\
  0.0477 &  0.0184
end{array}
right)
print(to_ltx(np.random.randn(2,2),
       latexarraytype='array',
       is_row_vector=False,
       mark_color='yellow',
       mark_elements=[(1,1)],
       brackets='(]',
       separate_columns=[1,2],
       separate_rows=[1,2],))
left(
begin{array}{c|c}
  0.3498 &  0.4393\
  hline
 -0.8521 & colorbox{yellow}{$-0.6412$}
end{array}
right]

Jupyter Notebooks

import numpy as np
from numpyarray_to_latex.jupyter import to_jup

to_jup(np.random.randn(10,10),
       mark_elements=[(1,1),(2,3),(7,0)],
       separate_columns=[1,2],
       separate_rows=[1,2],
      )
\[\begin{split}\displaystyle \require{color} \left( \begin{array}{c|c|cccccccc} -0.60 & -0.25 & 0.78 & -1.51 & 0.23 & -1.09 & 1.83 & 0.20 & 0.53 & -0.36\\ \hline 1.57 & \colorbox{pink}{$ 0.11$} & 0.10 & -0.01 & 0.32 & 0.63 & -1.29 & -1.51 & 1.70 & -0.23\\ \hline 0.05 & -0.02 & -1.21 & \colorbox{pink}{$ -0.59$} & 1.40 & 1.08 & 0.73 & -0.02 & -1.35 & 0.09\\ 0.12 & -0.33 & -0.77 & -0.21 & 1.72 & 0.27 & 1.71 & 0.22 & 1.84 & -0.33\\ 1.03 & -2.00 & 0.95 & -0.94 & 0.37 & -0.64 & -0.05 & 0.37 & -0.86 & 0.01\\ -2.01 & 0.51 & 1.54 & 0.62 & 0.61 & -0.52 & 0.02 & -1.28 & -0.35 & -0.12\\ 0.38 & -0.06 & 1.03 & -0.35 & 1.54 & -0.20 & -1.75 & -0.39 & 0.30 & 0.66\\ \colorbox{pink}{$ 0.43$} & -1.78 & -0.30 & 1.36 & -0.70 & 0.40 & 0.41 & 0.75 & -1.62 & 0.28\\ 0.49 & 0.83 & -0.55 & 0.62 & -0.37 & -0.12 & -1.42 & 1.97 & -0.28 & -0.16\\ 0.02 & -1.31 & 0.12 & -0.99 & 1.72 & 0.52 & -1.89 & 0.90 & -0.05 & 1.77 \end{array} \right)\end{split}\]
to_jup(np.random.randn(2),
       mark_elements=[1,],
       separate_columns=[1,2],
       separate_rows=[1,2],
      )
\[\displaystyle \require{color} \left( \begin{array}{c|c} 0.03 & \colorbox{pink}{$ 0.30$} \end{array} \right)\]
to_jup(np.random.randn(2),
       is_row_vector=False,
       mark_elements=[1,],
       separate_columns=[1,2],
       separate_rows=[1,2],
       mark_color='yellow',
       brackets='(]',
      )
\[\begin{split}\displaystyle \require{color} \left( \begin{array}{c} -0.73\\ \hline \colorbox{yellow}{$ 0.27$} \end{array} \right]\end{split}\]
to_jup(np.random.randn(2,2),
       latexarraytype='Vmatrix',
       is_row_vector=False,
       separate_rows=[1],
      )
\[\begin{split}\displaystyle \require{color} \begin{Vmatrix} 1.11 & -1.75\\ \hline -0.99 & 0.47 \end{Vmatrix}\end{split}\]
to_jup(np.random.randn(2,2)+1j*np.random.randn(2,2),
       mark_elements=[(0,1)],
      )
\[\begin{split}\displaystyle \require{color} \left( \begin{array} 2.76+ -0.85i & \colorbox{pink}{$ 1.07+ -1.39i$} \\ 0.08+ 1.39i & -0.35+ 0.03 \end{array} \right)\end{split}\]
to_jup(np.random.randn(2,2)+1j*np.random.randn(2,2),
       mark_elements=[(0,1)],
       fmt='{:4.2e}',
      )
\[\begin{split}\displaystyle \require{color} \left( \begin{array} 3.88\times 10^{-1}-1.93\times 10^{-1}i & \colorbox{pink}{$ 1.70-5.96\times 10^{-1}i$} \\ -8.87\times 10^{-1}+1.17i & -1.12+1.53 \end{array} \right)\end{split}\]

Main

Provides to_ltx to convert numpy arrays to LaTeX.

numpyarray_to_latex.main.to_ltx(a, fmt='{:6.4f}', latexarraytype='array', imstring='i', is_row_vector=True, mathform=True, brackets='()', mark_elements=[], mark_color='pink', separate_columns=[], separate_rows=[])[source]

Return a LaTeX array given a numpy array.

Parameters
  • a (numpy.ndarray) --

  • fmt (str, default = '{:6.2f}') -- python 3 formatter, optional- https://mkaz.tech/python-string-format.html

  • latexarraytype (str, default = 'array') --

    Any of

    "array"
    "pmatrix"
    "bmatrix"
    "vmatrix"
    "Vmatrix"
    "Bmatrix"
    

    if "array", you can specifiy the brackets with the keyword brackets.

  • imstring (str, default = 'i') -- Character to use to represent the imaginary unit. Usually 'i' or 'j'

  • is_row_vector (bool, default = True) -- If the array is 1D, should the output be a row (True) or column (False) vector?

  • mathform (bool, default = True) -- whether to convert strings like 1e+05 to 1\times10^{5}.

  • brackets (iterable, default = '()') -- which brackets to use to wrap the matrix (must be two elements long). Use brackets = None if you don't want any brackets around the array.

  • mark_elements (list, default = []) -- list of tuples containing element indices that should be marked with a colorbox.

  • mark_color (str, default = 'pink') -- The color with which to mark matrix elements.

  • separate_columns (list, default = []) -- list of column indices before which a vertical line should be drawn

  • separate_rows (list, default = []) -- list of row indices before which a horizontal line should be drawn

Returns

out -- Formatted LaTeX string

Return type

str

Examples

>>> from numpyarray_to_latex import to_ltx
>>> tex = to_ltx([[2.,2.],[2.,2.]])
>>> print(tex)
\left(
\begin{array}{}
    2.00 &    2.00\\
    2.00 &    2.00
\end{array}
\right)

Jupyter

Utils

Provide helper functions.

numpyarray_to_latex.utils.math_form(number, is_imaginary=False, mathform=True)[source]

Convert a float number formatted in scientific notation to the corrsponding LateX format (e.g. 2\times10^{2}).