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)
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)
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)
Let's put it in exponential form.
In [6]:
a2l.to_ltx(A, frmt = '{:.2e}', arraytype = 'array', mathform=False)
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)
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')
In [9]:
A = np.array([[1.23456, 23.45678, 456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')
In [10]:
A = np.array([[1.23456, 23.45678, 456.23, 8.239521]]).T
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = '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)
In [12]:
to_tex = lambda A : a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array', mathform=True)
to_tex(A)
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]:
In [15]:
np.array(df)
Out[15]:
In [16]:
a2l.to_ltx(df, arraytype='bmatrix')
In [17]:
a2l.to_ltx(df, arraytype='tabular')
In [18]:
df2 = pd.DataFrame(['cat', 'dog', 'bird', 'snake', 'honey badger'], columns=['pets'])
df2
Out[18]:
In [19]:
df_mixed = df.join(df2)
df_mixed
Out[19]:
In [20]:
a2l.to_ltx(df_mixed, arraytype='tabular')
In [21]:
A = np.array([[1.23456, 23.45678],[456.23, 8.239521]])
a2l.to_ltx(A, frmt = '{:6.2f}', arraytype = 'array')
In [22]:
A = np.array([[1.23456, 23.45678],[456.72+392.71j, 8.239521]])