How to Write a Styler to a file, buffer or string in LaTeX?

How To Style A Data Frame Into A LaTeX Table

Styler to LaTeX is easy with the Pandas library’s method- Styler.to_Latex. This method takes a pandas object as an input, styles it, and then renders a LaTeX object out of it.

The newly created LaTeX output can be processed in a LaTeX editor and used further.

LaTeX is a plain text format used in scientific research, paper writing, and report formatting. We can use LaTeX editors to create templates for presentations, cover letters, and so on.

In this post, we are going to look at transforming a data frame into latex format with the help of the Styler class.

Related: Render a LaTeX object from a Data Frame.

What Is a Styler?

The Styler class is mainly used to visualize or style any data that is in the form of a table.

It can be used to add colors to a data frame to make it look beautiful. The styled data frame can be exported as an HTML file, a LaTeX file, an Excel file, and so on.

Please visit this post to learn about Pandas Data Frame.

The Styler class can be implemented by using an attribute called .style.

One point to note while using the Styler object is that it should be applied after a data frame is finalized. Because the Styler object is not updated if any changes are made to the data frame after initiating the class.

There are many things you can do with Styler. It can make your dull-looking data frame transform into something beautiful by adding colored borders and cells.

Apart from the decorations, the styler class is also used to visualize your data frame. With Styler, you can add bar charts, plots, etc.

Let us see an example of how to use the Styler class.

import pandas as pd
res = {'Name': ['Arun','Arjun','Bhavya','Chaitra','Deeksha','Fathima','Ganga'],
          'Subject': ['Physics', 'Maths', 'Chemistry', 'History', 'Maths', 'English', 'Civics'],
      'Marks': [82, 93, 84, 68, 91, 45, 56]}
df = pd.DataFrame(res)
df

The panda’s library imported in the very first line is used to convert the dictionary called res to a data frame called df.

This data frame has the names, subjects, and marks of seven students.

Let us see the data frame first before styling.

Data Frame
Data Frame

Let us add the background color and the color of the cells to make it more interesting.

df.style.set_properties(**{'background-color': 'yellow',
                           'color': 'red'})

We are using the df.style attribute to add colors to the background and also each cell.

Styled Data Frame
Styled Data Frame

Now let us see how we can use this Styler class and style a data frame before converting it to LaTeX format.

Styler to LaTeX Method Explained

The syntax of the method is as follows.

Styler.to_latex(buf=None, *, column_format=None, position=None, position_float=None, hrules=None, clines=None, label=None, caption=None, sparse_index=None, sparse_columns=None, multirow_align=None, multicol_align=None, siunitx=False, environment=None, encoding=None, convert_css=False)

Argument DescriptionDefault Value/ TypeNecessity
bufJust like hrules
Used to add vertical lines at the beginning of the columns, at the end of the columns, and even between the indices
NoneRequired
column_formatIf the column is a column of numbers, ‘r’ is used to format it
For every other column, ‘l’ is used
strOptional
positionThis argument is to be placed after the \begin() in your LaTeX document
It is a positional argument for tables
strOptional
position_floatSpecifies the position of the table
Can be anyone from {“centering”, “raggedleft”, “raggedright” }
But cannot be used when the environment is set to longtable
strOptional
hrulesFlag variable of the Styler class which is used to give a horizontal line in the LaTeX document using the toprule, midrule, and bottomrule of the booktabs package
bool/ FalseOptional
clinesJust like hrules
Used to add vertical lines at the beginning of the columns, at the end of the columns and even between the indices
strOptional
labelThe label which you want to be placed inside the \label() of your LaTeX documentstrOptional
captionUsed to give a caption to the output
The syntax is \caption[short_caption]{full_caption} so if only one string is passed, no short caption will be set
str,tupleOptional
sparse_indexThis argument decides if the hierarchical data should be sparsed
When set to True(the default), the indices which are repetitive will be only displayed once per group
bool/TrueOptional
multirow_alignJust like hrules
Used to add vertical lines at the beginning of the columns, at the end of the columns, and even between the indices
cOptional
multicol_alignUsed to align the columns that are spread across multiple horizontal linesrOptional
siunitxThis argument is used to structure or format the LaTeX table according to the siunitx packagebool/FalseRequired
encodingThis field is used when buf parameter has a file path to write the output to
In encoding, the original message or string is written in some other form to avoid misuse
utf-8Optional
environmentWhen this field is used, it replaces the starting of the \\begin{table}
When None is set, doesn’t replace anything
str/NoneOptional
convert_cssUsed to convert any CSS code to LaTeX format
When no CSS is found, the table is dropped
bool/FalseOptional
Arguments of Styler to LaTeX

Let us see a few examples of this method.

Writing a Simple Styler to LaTeX

In this example, we are going to create a data frame from a CSV file, style it and then obtain a LaTeX table from it.

Refer to this article to learn how to read a CSV file into a data frame.

The dataset we are going to use is a Diabetes dataset, which has 8 labels and one predicting variable to classify if a person has diabetes or not based on their BMI, glucose levels, and so on. We are going to look at this dataset in a while.

Let us see the code and the data frame before it is styled.

import pandas as pd
df = pd.read_csv("diabetes.csv")
df.head(10)

Here, the Pandas library is imported to be able to read the CSV file into a data frame. In the next line, we are initializing an object to store the data frame obtained by pd.read_csv. This object is named df.

The next line is quite interesting. df.head() is used to print the first five rows of a large dataset by default. But it is customizable; here we are trying to print the first 10 rows of the data frame. Likewise, you can also experiment with df.tail() which prints the last rows of the data frame.

Let us see the data frame.

Data Frame
Data Frame

Now let us see how we can style this boring data frame.

print(df
  .head(10).style.set_properties(
     **{"tiny": "--latex--rwrap"}
  ).to_latex(label="Diabetes dataset"))

In the above code, we are trying to apply styling for the first 10 rows of the data frame. The style is an instantiation of the Styler class. We are trying to set the font size to tiny using the set_properties method.

In the .to_latex method, we have also provided the label to be included in the LaTeX table.

First, let us see the output of this code.

Styler Tto LaTeX
Styler to LaTeX

Worried after seeing the output? Let us try to put this output in a LaTeX editor and see what happens.

LaTeX Table
LaTeX Table

There might be some warnings when you try to incorporate the LaTeX code generated via styler in the editor. We need to include any package that is necessary to get the desired output in the editor.

Writing a Styler to LaTeX and Highlighting the Minimum and Maximum Values

In this example, we are going to take a data frame that has different types of numerical data types(integers and floats) and highlight the maximum and minimum values of each column.

import pandas as pd
df = pd.DataFrame([[1, 2.2], [3, 4.4], [2, 6.6]],
                  index=["i1", "i2", "i3"],
                  columns=["Integers", "Floats"])
s = df.style\
    .highlight_max(props='background-color: #F5B041; color: #FFFFFF; font-weight: bold;')\
    .highlight_min(props='background-color: #3498DB; color: #FFFFFF; font-weight: bold;')
s.to_latex(caption="Types of Numbers")

Here in the first two lines, we are importing the Pandas library and creating a data frame out of a dictionary of values of numbers, indices and column names. This data frame is named df.

Next, we are creating a variable s to store the styled data frame created with the help of .style. We are trying to color the maximum number in the table with orange color and the minimum number with blue. The color of the elements of the table is white. We also write the elements in bold. The colors are specified with their respective color codes.

This was using the Styler class. Now we need to use this styled data frame to produce a latex table. We have also included the caption of the table.

Styler To LaTeX With Caption
Styler To LaTeX With Caption

Let us see the LaTeX table.

Color Coded Latex Table
Color-Coded Latex Table

So cool isn’t it? You can even customize the colors, add more data and make it even more visually appealing.

Writing the Styler to LaTeX and Storing It in a File

This example is also going to be the same. But instead of displaying the output in the form of a cell in the notebook, we are going to try and store it in a .tex file.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
s = df.style.highlight_max(axis=None,
                           props='cellcolor:{red}; bfseries: ;')
with open('output1.tex', 'w') as f:
    f.write(s.to_latex())

As usual, we have imported the pandas’ library to start working with the data frames. The data frame we have taken consists of 9 values and the styler code is supposed to highlight the maximum value with red.

Next, we are creating a new file called output1.tex to write the output into it. This file is named as f for short.This file is created inside your environment with the name output1.tex.

Let us see the file.

Styler To LaTeX File
Styler To LaTeX File

Now let us try to write this output in a LaTeX editor.

LaTeX Table
LaTeX Table

Since the highest number is 9, it is marked in red. All the other numbers are left untouched.

Summary

To sum up this post, we have seen how we can beautify the same old boring data frame into something more interesting with the help of the Styler class. The Styler class is a tool of the Pandas Library used to style pandas’ objects. We can not use it directly but can instantiate an object called style to implement its properties. We have understood the usage of the Styler class with an example.

In this example, we have used a dull-looking data frame to create a data frame that is colored and structured.

In this post, we learned how to render a LaTeX table from the styler data frame using the method- Styler.to_latex. LaTeX on its own can be a very powerful writing tool. Unlike your average writing tools like MSWord and other text editors, you can code to develop a document with LaTeX editors! It is also a very sophisticated approach to writing research papers in science and mathematics, university-level documentation, and even cover letters.

Up next, we have understood the syntax and arguments of the method – Styler.to_latex. In this section, we have talked about every parameter in the syntax and understood its default values.

Next, we have seen a few examples of the method.

In the first example, we have taken a CSV file, read it into a data frame, and styled this data frame. This data frame is then rendered into a LaTeX table. We have styled this data frame with a tiny font size.

In the second example, we have taken a data frame that has the types of numeric data (integer and float). Using this data frame, we have created a Styler object which highlights the maximum and minimum values of the data frame. With the output obtained here, we have created a LaTeX document.

In the last example, we have taken a simple data frame, styled in such a way that the maximum value is highlighted. But for a change, we have stored this output in a file. One thing to remember is when we are trying to create a file for the LaTeX document, it should be saved with a .tex extension. We have used this output in a LaTeX editor and obtained a table out of it.

References

To know more about the Styler class, refer to official Pandas documentation.

You can find the dataset used in this post here.

You can find more about the method discussed in this post here.

Want to try the above examples yourself?

Also, do check out this website for the standard color codes.