Series overview
1. Introduction to colors
Colors constitute the human visual interpretation of light waves of different wavelengths.
Color models
There exist several models to represent colors. We will examine the three that are provided by the <a href="https://ctan.org/pkg/color" target="_blank" rel="noopener">color</a>
package. These are rgb
, cymk
and gray
.
rgb
(Red-Green-Blue) is an additive model where a mix of red
, green
and blue
results in white. Each of those three colors is represented by a decimal number from 0
to 1
and the three numbers are separated with a comma. In this model, 0,0,0
is the code for black and 1,1,1
is the code for white.
cmyk
(Cyan-Magenta-Yellow-Key/Black) is a subtractive model based on cyan
, magenta
, yellow
and black
(called key
). Hence, 0,0,0,0
is the code for white and 1,1,1,X
or X,X,X,1
(with X
any value between 0 and 1) are codes for black.
gray
is a grayscale model where 0
is black and 1
is white.
The color package
The color package brings colors to LaTeX. Colors are not part of TeX and are brought instead by drivers (PDFTeX, LuaTeX, VTeX, etc.). The color
package provides driver-independant commands and therefore makes it easy to add colors to your document.
The color package implements eight colors. These are black
, white
, red
, green
, blue
, cyan
, magenta
and yellow
. You can define other colors with \definecolor{<i><name></i>}{<i><model></i>}{<i><color-spec></i>}
. For instance, the following code defines the color orange
:
\usepackage{color} \definecolor{orange}{rgb}{1,0.647,0}
Now, you can use the textcolor
macro to color text. The macro has the following signature: \textcolor{<i><declared-color></i>}{<i><text></i>}
or \textcolor[<i><model></i>]{<i><color-spec></i>}{<i><text></i>}
. The two lines below are equivalent, as the color red
is defined by 1,0,0
in the rgb
color model.
\textcolor{red}{An red text.} \textcolor[rgb]{1,0,0}{A red text.}
Color | Name (color) | Name (xcolor) | rgb | cymk | gray |
white | white | 1,1,1 | 0,0,0,0 | 1 | |
— | gray | 0.5,0.5,0.5 | 0,0,0,0.5 | 0.5 | |
black | black | 0,0,0 | 0,0,0,1 | 0 | |
red | red | 1,0,0 | 0,1,1,0 | — | |
green | green | 0,1,0 | 1,0,1,0 | — | |
blue | blue | 0,0,1 | 1,1,0,0 | — | |
cyan | cyan | 0,1,1 | 1,0,0,0 | — | |
magenta | magenta | 1,0,1 | 0,1,0,0 | — | |
yellow | yellow | 1,1,0 | 0,0,1,0 | — | |
— | orange | 1,0.647,0 | 0,0.353,1,0 | — |
Colors in tables (tabular)
There are three main packages to color tables : colortbl, xcolor and colortab.
So which package is the most appropriate? The answer is: it depends. By default, you should stick to colortbl. However, in some cases another package might be more appropriate. The table below provides an overview of the main advantages of each of the three packages over the others. More details are given in their respective articles.
colortbl | xcolor | colortab |
|
|
|
2. The colortbl package
The colortbl package provides three commands that are directly used to color cells: \columncolor
, \rowcolor
and \cellcolor
. Other commands are used for fine-tuning, including \arrayrulecolor
and \doublerulesepcolor
.
Command | Description |
\columncolor[<i><color model></i>]{<i><color></i>}[<i><left></i>][<i><right></i>] |
Color a column. This command must be in a tabular preamble, within the argument of a >{} . [<i><left></i>] is the left margin (default to \tabcolsep ) and [<i><right></i>] is the right margin (default to \tabcolsep ). |
\rowcolor[<i><color model></i>]{<i><color></i>}[<i><left></i>][<i><right></i>] |
Color a row. This command must be before the first cell of a row. [<i><left></i>] is the left margin (default to \tabcolsep ) and [<i><right></i>] is the right margin (default to \tabcolsep ). |
\cellcolor[<i><color model></i>]{<i><color></i>} |
Color a cell. Use this command anywhere inside the cell. |
For example, if you wanted a cell with a cyan
background, use \cellcolor[cymk]{1,0,0,0}
. If you wanted a column to be red background, use >{\columncolor{red}}
or >{\columncolor[rgb]{1,0,0}}
in your table preamble.
Below is an example that shows all three commands in action. Colors are overwritten sequentially.
% Preamble \usepackage{colortbl} % Document \begin{tabular}{|>{\columncolor{red}}lll|} \hline 1& 2& 3\\ \cellcolor{yellow}4 & 5& 6\\ \rowcolor{blue} \multicolumn{1}{|>{\columncolor{yellow}}l}{7}& 8 & \cellcolor{yellow}9\\ \hline \end{tabular}
In the figure, we see that \cellcolor
overwrites \rowcolor
, which overwrites \columncolor
. We also notice that using \columncolor
in a \multicolumn
does not overwrite any command (except \columncolor
in the preamble). Therefore, it is better to use \cellcolor
to color one cell.
Colortbl and partial horizontal borders
With colortbl
, you can’t use partial horizontal borders with \cline
because the ones on top of coloured cell will be hidden behind the background. There are two possible approaches to overcome this limitation.
- The arydshln package provides the
\ADLnullwidehline
macro that allows normal usage of\cline
. However,arydshln
has compatibility issues with other packages (e.g.<a href="https://ctan.org/pkg/hhline" target="_blank" rel="noopener">hhline</a>
) as well as performance problems. - The hhline package implements partial horizontal borders. An example is given below.
% Preamble \usepackage{colortbl,hhline} % Document \begin{tabular}{ccc} \hline 1 & 2 & 3 \\ \hhline{~-~} 4 & \cellcolor{red}5 & 6 \\ \hhline{~-~} 7 & 8 & 9 \\ \hline \end{tabular}
Token | Description |
~ | No horizontal border |
– | Horizontal border |
= | Horizontal double border |
| | Vertical border |
|| | Vertical double border |
There are other tokens you can read about in hhline
‘s documentation, mostly to make double borders more beautiful.
colortbl without left and right margins
As you can see in the figure, when you remove left and right margins of a table with @{}
, it is not applied to the background color.
% Preamble \usepackage{colortbl} % Document \begin{tabular}{@{} l c r @{}} \hline 1 & 2 & 3 \\ \cellcolor{yellow}4 & 5 & 6 \\ \rowcolor{blue}7 & 8 & \cellcolor{yellow}9 \\ \hline \end{tabular}
The easiest fix is to include >{\columncolor{white}[0pt][\tabcolsep]}
right after your first @{}
and >{\columncolor{white}[\tabcolsep][0pt]}
right before your last column declaration. \tabcolsep
is the length of the default margin. You may have to change white
to another color if the background color of your page is not white. With that you can use any commands from colortbl
package without having to worry about their optional arguments. The figure below illustrates this.
\begin{tabular}{@{}>{\columncolor{white}[0pt][\tabcolsep]} l c >{\columncolor{white}[\tabcolsep][0pt]} r @{} } \hline 1 & 2 & 3 \\ \cellcolor{yellow}4 & 5 & 6 \\ \rowcolor{blue}7 & 8 & \cellcolor{yellow}9 \\ \hline \end{tabular}
colortbl and booktabs
You can use colortbl
and booktabs together. Although, using borders and background color is a poor typographical choice.
By default, booktabs
produces a vertical space before and after the borders. We have to remove this space and, instead, increase the height of each cell. To do this, we use the following code before the tabular
environment:
\extrarowheight=\aboverulesep \addtolength{\extrarowheight}{\belowrulesep} \aboverulesep=0pt \belowrulesep=0pt
Place it inside a group (i.e. a table
environment or {}
), such that the effect is not global.
% Preamble \usepackage{colortbl,booktabs} % Document { \extrarowheight=\aboverulesep \addtolength{\extrarowheight}{\belowrulesep} \aboverulesep=0pt \belowrulesep=0pt \begin{tabular}{ccc} \toprule \rowcolor{gray}1 & 2 & 3 \\ \midrule \cellcolor{yellow}4 & 5 & 6 \\ 7 & 8 & \cellcolor{yellow}9\\ \bottomrule \end{tabular} }
colortbl and multirow
When you are using multirow with colortbl
, you have to change some things in the structure of your table. First, you have to use a background color declaration in each row that is spanned by your multi-row cell. Importantly, the \multirow
macro has to be placed in the last row of your multi-row cell. Therefore, the first argument of \multirow
needs to be a negative number. See the multirow package documentation for more details. An example is given below.
% Preample \usepackage{colortbl,multirow} % Document \begin{tabular}{ccc} \cellcolor{yellow} & 2 & 3 \\ \multirow{-2}{*}{\cellcolor{yellow}1} & 4 & 5 \\ 6 & 7 & 8 \end{tabular}
colortbl, multirow and partial horizontal borders
If you want to use hhline
to draw a partial horizontal rule beside a cell with multirow
, you can’t use ~
(the token that produces no line). This would produce a white line through your cell. Instead, you have to use -
(the token that produces a normal line) but to change its color to your cell’s background color with \arrayrulecolor
. Then, you can reset the color of the line to black with \arrayrulecolor{black}
. See next figure.
% Preample \usepackage{colortbl,multirow,hhline} % Document \begin{tabular}{ccc} \hline \cellcolor{yellow} & 2 & 3 \\ \hhline{>{\arrayrulecolor{yellow}}->{\arrayrulecolor{black}}--} \multirow{-2}{*}{\cellcolor{yellow}1} & 4 & 5 \\ 6 & 7 & 8 \\ \hline \end{tabular}
colortbl, multirow and partial horizontal double borders
Similarly to the example above, if you want to use hhline
to draw a partial horizontal double rule in a row with a multi-row cell, you can’t use ~
(the token that produces no line). Again, this would lead to a thick white line through your multi-row cell. Instead, you have to use =
(the token that produces a double line) and change the color of the rule
as well as the separation to the background color of the multi-row cell. The commands are again \arrayrulecolor
and \doublerulesepcolor
. For the remaining cells of the row, you have to reset the colors using \arrayrulecolor{black}
and \doublerulesepcolor\{white}
. An example is provided below.
% Preample \usepackage{colortbl,multirow,hhline} % Document \begin{tabular}{ccc} \hline\hline \cellcolor{yellow} & 2 & 3 \\ \hhline{>{\arrayrulecolor{yellow}\doublerulesepcolor{yellow}}=>{\arrayrulecolor{black}\doublerulesepcolor{white}}==} \multirow{-2}{*}{\cellcolor{yellow}1} & 4 & 5 \\ 6 & 7 & 8 \\ \hline\hline \end{tabular}
colortbl and makecell
If you want to use the <a href="https://ctan.org/pkg/makecell" target="_blank" rel="noopener">makecell</a>
package instead of multirow
, the background color declaration has to be placed before \multirowcell
or \multirowhead
macros. To reproduce the first multirow
example, you would use the following code:
% Preample \usepackage{colortbl,makecell} % Document \begin{tabular}{ccc} \cellcolor{yellow} & 2 & 3 \\ \cellcolor{yellow}\multirowcell{-2}{1} & 4 & 5 \\ 6 & 7 & 8 \end{tabular}
colortbl and arydshln
There are some compatibility issues with colortbl
and arydshln. The easiest way to fix them (and to merge your borders inside your cells to remove white gaps) is just to put the following code in your preamble:
\usepackage{colortbl,arydshln} \ADLnullwidehline \ADLnullwide
colortbl and hvdashln
colortbl
and hvdashln are not compatible. You should use <a href="https://ctan.org/pkg/arydshln" target="_blank" rel="noopener">arydshln</a>
instead.
The next article will be on coloring cells using the xcolor package.
The author is the main developer behind the LaTeX Complex Tables Editor.
See also: Original Source by Tom
Note: The copyright belongs to the blog author and the blog. For the license, please see the linked original source blog.
Leave a Reply
You must be logged in to post a comment.