Add color to a table (cell, row and column) in LaTeX
In this tutorial, I will show you how to add color to a single cell, row, or column of a table in LaTeX.
We use the xcolor
package for normal colors but to add colors to the table you need to use the [table]
option of this package. So load this package using
\usepackage[table]{xcolor}
Add color to a cell of a table in LaTeX
To color a particular single cell in a table, you can use the \cellcolor{<color-name>}
command before the text in that cell.
This way you can highlight important text or data in the table by using different colors in different cells.
\documentclass{article} \usepackage[table]{xcolor} \begin{document} \begin{table}[h] \centering \begin{tabular}{|c|c|c|} \hline \textbf{Heading1} & \textbf{Heading1} & \textbf{Heading1}\\ \hline 1 & \cellcolor{red}2 & 3\\ \hline \cellcolor{green}A & B & \cellcolor{yellow}C\\ \hline \end{tabular} \caption{Table with colored cells} \end{table} \end{document}
Output:
Add color to a row of a table in LaTeX
In order to add color to a row of a table you can use the \rowcolor{<color-name>}
command before starting the row. This allows you to add color to the entire row. Take a look.
\documentclass{article} \usepackage[table]{xcolor} \begin{document} \begin{table}[h] \centering \begin{tabular}{|c|c|c|} \hline \rowcolor{green}\textbf{Heading1} & \textbf{Heading1} & \textbf{Heading1}\\ \hline \rowcolor{green!50}1 & 2 & 3\\ \hline \rowcolor{green!30} A & B & C\\ \hline \end{tabular} \caption{Table with colored rows} \end{table} \end{document}
Output:
Even and odd color combinations for the rows
If you use even and odd color combinations instead of manually adding colors to each row repeatedly, you can create a nicer table with less effort.
For this, you need to use this syntax before the tabular environment:
\rowcolors{starting-row}{odd-color}{even-color}
Here,
- starting-row: From which number row the color will start to be added to the table.
- odd-color: Which color will be added first.
- even-color: Which color will be added in seconds, and so on in even and odd combinations continuously.
\documentclass{article} \usepackage[table]{xcolor} \begin{document} \begin{table}[h] \centering % \rowcolor{starting-row}{odd-color}{even-color} \rowcolors{2}{gray!10}{gray!30} \begin{tabular}{|c|c|c|} \hline \rowcolor{green}\textbf{Heading1} & \textbf{Heading1} & \textbf{Heading1}\\ \hline 1 & 2 & 3\\ \hline A & B & C\\ \hline 1 & 2 & 3\\ \hline A & B & C\\ \hline 1 & 2 & 3\\ \hline A & B & C\\ \hline \end{tabular} \caption{Even and odd color combinations for the rows} \end{table} \end{document}
Output:
Add color to a column of a table in LaTeX
In this case, you have to use the >{\columncolor{<color-name>}}
command at the point before we set the alignment settings. If you want to add color in the first column then you can write like this:
\begin{tabular}{|>{\columncolor{<color-name>}}c |c| c |}
An example will make it easy to understand.
\documentclass{article} \usepackage[table]{xcolor} \begin{document} \begin{table}[h] \centering \begin{tabular}{|>{\columncolor{green}}c|c|>{\columncolor{yellow}}c|} \hline \textbf{Heading1} & \textbf{Heading1} & \textbf{Heading1}\\ \hline 1 & 2 & 3\\ \hline A & B & C\\ \hline 1 & 2 & 3\\ \hline \end{tabular} \caption{Table with colored columns} \end{table} \end{document}
Output:
Leave a Reply