This package contains a class for simplifying representation of NumPy arrays using LaTeX syntax in a Jupyter notebook.
pip install -i https://test.pypi.org/simple/ latex-libThe LatexArray class can be used to handle NumPy arrays that need to be displayed in Jupyter notebooks.
- This class subclasses the
Latexclass from IPython, which is what IPython uses when displaying the object in a Jupyter notebook as LaTeX.
from latex_lib import LatexArray as Array
# Declare an array of 12 integers as a row vector.
array: Array = Array(range(12))
arrayThe string representation of the array is its LaTeX representation.
str(array)'\\begin{pmatrix}\n0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11\n\\end{pmatrix}'
The class supports all arithmetic operations supported by np.ndarray (its superclass),
which covers generally all operations.
# Perform some arithmetic on the original array.
(array + array) + 1 * 2 / 5The class supports all methods supported by np.ndarray as well.
# Reshape the array into a column vector.
array.reshape((-1, 1))# Reshape the array into a 6 x 2 matrix.
array.reshape(-1, 2)Numpy functions and class methods are also generally supported (although, edge cases may exist).
import numpy as np
# NumPy functions work but do not return a LatexArray object.
np.where(array % 2 == 0, "Even", "Odd")array(['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even',
'Odd', 'Even', 'Odd'], dtype='<U4')
# We may, however, cast them to one:
Array(np.where(array % 2 == 0, "Even", "Odd"))This can be used to constructor more complex Latex expressions.
The IPython.display.Latex class can be used to create more complex Latex expressions.
from IPython.display import Latex
from latex_lib import LatexArray as Array
# Declare an array of 12 integers as a row vector.
array_a: Array = Array(range(12)).reshape(-1, 2)
array_b: Array = Array(range(12)).reshape(2, -1) * 5
Latex(rf"""$$
\begin{{align*}}
& \text{{Given matrices A and B, where: }}\\
& A = {array} && B = {array_b}\\
& \text{{The dot product can be obtained as: }}\\\\
& A \cdot B = {array_a} \cdot {array_b} \\
& = {array_a @ array_b}
\end{{align*}}
$$""")If you have any questions or suggestions, please feel free to open an issue or submit a pull request.
This package is released under the MIT License.