Latex

Latex#

This tutorial is for the Latex class available in TFC utilities. As of right now, Latex only provides a table class that can be used to transform numpy arrays into a latex table. Latex may be expanded in the future to include other convenience classes/functions. Let’s begin by importing table from Latex.

[1]:
import numpy as np
from tfc.utils.Latex import table

The table class has one public, static method called SimpleTable that can be used to create latex tables. This method has one required argument, a NumPy array, whose elements are added to the associated cells of the table. In addition, SimpleTable takes in three optional keyword arguments:

  • form - Number format. Default is %.4E. See float_kind options numpy.array_to_string for more details.

  • colHeader - Column header to be added to the table, specified as a list. Default is blank.

  • rowHeader - Row header to be added to the table, specified as a list. Default is blank.

As an example, suppose we want to print out some array A with a precision of four decimal places.

[2]:
A = np.eye(3)
tab = table.SimpleTable(A,form='%.6f')
print(tab)
\begin{center}
\begin{tabular}{|c|c|c|}
\hline
1.000000 & 0.000000 & 0.000000\\
\hline
0.000000 & 1.000000 & 0.000000\\
\hline
0.000000 & 0.000000 & 1.000000\\
\hline
\end{tabular}
\end{center}

which looks like

\[\begin{split}\begin{array}{|c|c|c|} \hline 1.000000 & 0.000000 & 0.000000\\ \hline 0.000000 & 1.000000 & 0.000000\\ \hline 0.000000 & 0.000000 & 1.000000\\ \hline \end{array}\end{split}\]

when imported into LaTeX. Column and row headers can be added to the table as well.

[3]:
colHeader = ['','Column 1','Column 2','Column 3']
rowHeader = ['Row 1','Row 2','Row 3']
tab = table.SimpleTable(A,form='%.6f',colHeader=colHeader,rowHeader=rowHeader)
print(tab)
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
 & Column 1 & Column 2 & Column 3\\
\hline
Row 1 & 1.000000 & 0.000000 & 0.000000\\
\hline
Row 2 & 0.000000 & 1.000000 & 0.000000\\
\hline
Row 3 & 0.000000 & 0.000000 & 1.000000\\
\hline
\end{tabular}
\end{center}

This table looks like

\[\begin{split}\begin{array}{|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3}\\ \hline \text{Row 1} & 1.000000 & 0.000000 & 0.000000\\ \hline \text{Row 2} & 0.000000 & 1.000000 & 0.000000\\ \hline \text{Row 3} & 0.000000 & 0.000000 & 1.000000\\ \hline \end{array}\end{split}\]

when imported into Latex.