Some brief examples on using array_to_latex to get nicely formatted latex versions of your arrays.


In [1]:
import pandas as pd
import numpy as np
import array_to_latex as a2l
%load_ext autoreload
%autoreload 2

Let's create an array and output it as $\LaTeX$. We are going to use Python 3.0 string formatting

The following shows a float style output with 2 decimal places.


In [2]:
A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])
a2l.to_ltx(A, frmt = '{:.2f}', arraytype = 'array', mathform = True)


\begin{array}
  1.23 + 0.00j &  23.46 + 0.00j\\
  456.23 + 1.00j &  8.24 + 0.00j
\end{array}

Design is to print results to the screen with no output being available. However, new usages have highlighted the need to enable outputs and hide printing. Thus the addition of the print_out boolean to turn off printing but instead return an output.


In [3]:
A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])
latex_code = a2l.to_ltx(A, frmt = '{:.2f}', arraytype = 'array', mathform = True, print_out=False)

We can still print the returned formatted latex code:


In [4]:
print(latex_code)


\begin{array}
  1.23 + 0.00j &  23.46 + 0.00j\\
  456.23 + 1.00j &  8.24 + 0.00j
\end{array}

One can use a number before the decimal place. This defines the minimum width to use for the number, padding with spaces at the beginning.

Since the largest number needs 6 characters (3 before the decimal, the decimal, and 2 after), putting a 6 in this location makes everything line up nicely. This would also be a nice default to code up.


In [5]:
A = np.array([[1.23456, 23.45678],[456.23+1j, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array', mathform = True)


\begin{array}
    1.23 +   0.00j &   23.46 +   0.00j\\
  456.23 +   1.00j &    8.24 +   0.00j
\end{array}

Let's put it in exponential form.


In [6]:
a2l.to_ltx(A, frmt = '{:.2e}', arraytype = 'array', mathform=False)


\begin{array}
  1.23e+00 + 0.00e+00j &  2.35e+01 + 0.00e+00j\\
  4.56e+02 + 1.00e+00j &  8.24e+00 + 0.00e+00j
\end{array}

That's not how humans/textbooks write exponential form. Let's use mathform=True (which is the default).


In [7]:
a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'array', mathform=True)


\begin{array}
  1.23\times 10^{+00} + 0.00\times 10^{+00}j &  2.35\times 10^{+01} + 0.00\times 10^{+00}j\\
  4.56\times 10^{+02} + 1.00\times 10^{+00}j &  8.24\times 10^{+00} + 0.00\times 10^{+00}j
\end{array}

It's easier to make these columns line up than when using f format styling- so I believe it is working.

Of course, the typeset $\LaTeX$ will look better than the raw $\LaTeX$.

One can also capture the string in the output.

It will also do column and row-vectors. It's the array is 1-D, the default is a row.


In [8]:
A = np.array([1.23456, 23.45678, 456.23, 8.239521])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')


\begin{array}
    1.23 &   23.46 &  456.23 &    8.24
\end{array}

In [9]:
A = np.array([[1.23456, 23.45678, 456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')


\begin{array}
    1.23 &   23.46 &  456.23 &    8.24
\end{array}

In [10]:
A = np.array([[1.23456, 23.45678, 456.23, 8.239521]]).T
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')


\begin{array}
    1.23\\
   23.46\\
  456.23\\
    8.24
\end{array}

We can use the lambda function method to create a function with personalized defaults. This makes for a much more compact call, and one that can be adjusted for an entire session.


In [11]:
to_tex = lambda A : a2l.to_ltx(A, frmt = '{:6.2e}', arraytype = 'array', mathform=True)
to_tex(A)


\begin{array}
  1.23\times 10^{+00}\\
  2.35\times 10^{+01}\\
  4.56\times 10^{+02}\\
  8.24\times 10^{+00}
\end{array}

In [12]:
to_tex = lambda A : a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array', mathform=True)
to_tex(A)


\begin{array}
    1.23\\
   23.46\\
  456.23\\
    8.24
\end{array}

Panda DataFrames

You can also produce tables or math arrays from Panda DataFrames.


In [13]:
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
...                    columns=['a', 'b', 'c', 'd', 'e'])

In [14]:
df


Out[14]:
a b c d e
0 7 3 1 2 3
1 1 5 7 4 0
2 5 4 2 1 9
3 7 7 2 8 4
4 5 0 3 7 4

In [15]:
np.array(df)


Out[15]:
array([[7, 3, 1, 2, 3],
       [1, 5, 7, 4, 0],
       [5, 4, 2, 1, 9],
       [7, 7, 2, 8, 4],
       [5, 0, 3, 7, 4]])

In [16]:
a2l.to_ltx(df, arraytype='bmatrix')


\begin{bmatrix}
 0 &  7.00 &  3.00 &  1.00 &  2.00 &  3.00\\
 1 &  1.00 &  5.00 &  7.00 &  4.00 &  0.00\\
 2 &  5.00 &  4.00 &  2.00 &  1.00 &  9.00\\
 3 &  7.00 &  7.00 &  2.00 &  8.00 &  4.00\\
 4 &  5.00 &  0.00 &  3.00 &  7.00 &  4.00
\end{bmatrix}

In [17]:
a2l.to_ltx(df, arraytype='tabular')


\begin{tabular}{lrrrrr}
\toprule
     & a & b & c & d & e \\\n\midrule
 0 &  7.00 &  3.00 &  1.00 &  2.00 &  3.00\\
 1 &  1.00 &  5.00 &  7.00 &  4.00 &  0.00\\
 2 &  5.00 &  4.00 &  2.00 &  1.00 &  9.00\\
 3 &  7.00 &  7.00 &  2.00 &  8.00 &  4.00\\
 4 &  5.00 &  0.00 &  3.00 &  7.00 &  4.00\\
\bottomrule
\end{tabular}

In [18]:
df2 = pd.DataFrame(['cat', 'dog', 'bird', 'snake', 'honey badger'], columns=['pets'])
df2


Out[18]:
pets
0 cat
1 dog
2 bird
3 snake
4 honey badger

In [19]:
df_mixed = df.join(df2)
df_mixed


Out[19]:
a b c d e pets
0 7 3 1 2 3 cat
1 1 5 7 4 0 dog
2 5 4 2 1 9 bird
3 7 7 2 8 4 snake
4 5 0 3 7 4 honey badger

In [20]:
a2l.to_ltx(df_mixed, arraytype='tabular')


\begin{tabular}{lrrrrrr}
\toprule
     & a & b & c & d & e & pets \\\n\midrule
 0 &  7.00 &  3.00 &  1.00 &  2.00 &  3.00 &  cat         \\
 1 &  1.00 &  5.00 &  7.00 &  4.00 &  0.00 &  dog         \\
 2 &  5.00 &  4.00 &  2.00 &  1.00 &  9.00 &  bird        \\
 3 &  7.00 &  7.00 &  2.00 &  8.00 &  4.00 &  snake       \\
 4 &  5.00 &  0.00 &  3.00 &  7.00 &  4.00 &  honey badger\\
\bottomrule
\end{tabular}

In [21]:
A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')


\begin{array}
    1.23 &   23.46\\
  456.23 &    8.24
\end{array}

In [22]:
A = np.array([[1.23456, 23.45678],[456.72+392.71j, 8.239521]])