Site icon texblog

Color Table Series Part 3 : The Colortab package

Series overview

  1. Introduction to colors
  2. The colortbl package
  3. The xcolor package
  4. The colortab package

 

4. The colortab package

The colortab package is another package to color table cells and rules (lines). It is the only alternative to colortbl/xcolor, as xcolor requires colortbl. It is a somewhat complicated package that tends to be buggy. Also, the package is not compatible with longtable. However, you may need it sometimes because the colortbl package has problems with glue. Whenever you need to color a cell with \dotfill or \hrulefill, you may want to use colortab instead of colortbl.

The colortab package does not load the color package by default. This means that no color is defined in your document. You would either have to load the color package yourself or define your own colors. The first option is the recommended approach. However, as colortab is also the only option that does not depend on the color package, we are going to define our color ourselves with all we learned in the first part of this series of articles.

First, we implement the \Color macro to define RGB colors:

\usepackage{colortab}

\newcommand{\Color}[1]{%
	\pdfcolorstack\pdfcolorstackinit page direct{0 g}%
	 push {#1 rg}%
}

Using the macro, the color red would then be defined as: \Color{1 0 0}.

colortab works with normal tabular environments. It is heavily inspired by \halign, the TeX primitive behind LaTeX tables.

Whenever you have a row (or multiples rows) with colored cells, they must be enclosed by \LCC and \ECC. Inside this group and before the first row, another row must be inserted with the color declarations. An example is given below.

 


\begin{tabular}{|c|c|}
\hline%
	\LCC%
		\Color{0 0 1}& \Color{1 1 0}\\ % "Fake" row with color definitions
		Blue & Yellow \\
	\ECC\hline
	Normal & Normal\\ \hline
\end{tabular}

Note the percent (%) character at the end of lines. This is necessary because colortab misinterprets end of lines and may display white spaces instead.

You quickly realize the problem: the text is yellow. This means that we must reset the color at the beginning of the first cell that follows the color declaration. Here is what the code should look like.

 


\begin{tabular}{|c|c|}
\hline%
	\LCC%
		\Color{0 0 1}& \Color{1 1 0}\\ % "Fake" row with color definitions
		\Color{0 0 0} Blue & Yellow \\
	\ECC\hline
	Normal & Normal\\ \hline
\end{tabular}

When you are using \multicolumn, you still have to define the color for each column. On the other hand, this setup allows usage of vertical double borders. I will talk more on that topic below.

 


\begin{tabular}{|c||c|}
\hline%
	\LCC%
		\Color{0 0 1}&\Color{0 0 1}\\
		\multicolumn{2}{|c|}{%
			\Color{0 0 0}\dotfill Header\dotfill%
		} \\
	\ECC\hline
	Cell 1 & Cell 2\\\hline
\end{tabular}

 

colortab and array

The colortab and array packages are compatible. However, if you use \multicolumn inside your table, you must take some precautions. Right after your table preamble, put \SP to save your preamble. Then, after each row with a \multicolumn cell, put \RP to restore your preamble. See the following example, adapted from the colortab documentation:

 


\usepackage{array, colortab}

\begin{tabular}{|m{2cm}|m{2cm}|}\SP
\hline%
	\LCC%
		\Color{0 0 1}&\Color{0 0 1}\\
		\multicolumn{2}{|c|}{%
			\Color{0 0 0}\dotfill Header\dotfill%
		} \\ \RP \hline
	\ECC
	Cell 1 & Cell 2\\\hline
\end{tabular}

 

colortab and colortbl

The colortab and colortbl packages are compatible and can both be used in the same document. However, it is not recommended to use both packages in the same table.

 

colortab and booktabs

The colortab and booktabs packages are compatible if you adjust some length variables to remove the space before and after each rule. It is the same thing as with colortbl. Use this code before your table:

 

\extrarowheight=\aboverulesep
\addtolength{\extrarowheight}{\belowrulesep}
\aboverulesep=0pt
\belowrulesep=0pt

 

colortab and double borders

Using colortab with double borders is a challenge because the color will span inside your borders. However, there is a workaround using special rules (lines) and the array package.

Let’s first define some colors:

\def\Red{\Color{1 0 0}}
\def\Blue{\Color{0 0 1}}
\def\Yellow{\Color{1 1 0}}
\def\DarkGrey{\Color{0.66 0.66 0.66}}
\def\Black{\Color{0 0 0}}
\def\White{\Color{1 1 1}}

Here is what would be the result without any correction:

 


\begin{tabular}{|l||c|}
	\hline
	\LCC
		\Blue & \Yellow\\
		\Black First Blue & Yellow \\ \hline\hline
	\ECC
	\LCC
		\Blue & \DarkGrey\\
		\Black Second Blue & \White First Grey\\ \hline
		\Black Third Blue & \White Second Grey\Black\\ \hline
	\ECC
\end{tabular}

Let us first consider vertical rules. We will use !{} from array to fake double borders. In fact, it would be a triple border: a black border, a larger white border and then another black border.

The full code in the preamble will be like this:

!{\vrule\White\vrule width \doublerulesep\Black\vrule}

In this case, \doublerulesep is the width between the two borders of a double border. Here is the result:

 


\begin{tabular}{|l!{\vrule\White\vrule width \doublerulesep\Black\vrule}c|}
	\hline
	\LCC
		\Blue & \Yellow\\
		\Black First Blue & Yellow \\ \hline\hline
	\ECC
	\LCC
		\Blue & \DarkGrey\\
		\Black Second Blue & \White First Grey\\ \hline
		\Black Third Blue & \White Second Grey\Black\\ \hline
	\ECC
\end{tabular}

 

Horizontal double borders are somewhat more tricky. You cannot change the height of an \hline. We will have to use \noalign, a TeX primitive that let us insert \hrule (another TeX primitive) inside our table. To do this, we will use the following code instead of \hline\hline :

\noalign{%
	\hrule%
	\White\hrule height \doublerulesep%
	\Black\hrule%
}

Another option would be to load both hhline and colortbl and to use the line of code below:

\hhline{>{\doublerulesepcolor{white}}==}

However, most colortab users would try to avoid colortbl. For the result with the first solution and its full code, see the figure below.

 


\documentclass[12pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{colortab}
\newcommand{\Color}[1]{%
	\pdfcolorstack\pdfcolorstackinit page direct{0 g}%
	 push {#1 rg}%
}
\begin{document}
	\def\arraystretch{1.5}
	\def\Red{\Color{1 0 0}}
	\def\Blue{\Color{0 0 1}}
	\def\Yellow{\Color{1 1 0}}
	\def\DarkGrey{\Color{0.66 0.66 0.66}}
	\def\Black{\Color{0 0 0}}
	\def\White{\Color{1 1 1}}
	\begin{tabular}{|l!{\vrule\White\vrule width \doublerulesep\Black\vrule}c|}
		\hline
		\LCC
			\Blue & \Yellow\\
			\Black First Blue & Yellow \\ 
			\noalign{%
				\hrule%
				\White\hrule height \doublerulesep%
				\Black\hrule
			}
		\ECC
		\LCC
			\Blue & \DarkGrey\\
			\Black Second Blue & \White First Grey\\ \hline
			\Black Third Blue & \White Second Grey\Black\\ \hline
		\ECC
	\end{tabular}
	
\end{document}

&nbsp

colortab and multirow

The colortab and multirow packages are compatible. You can even use positive values for the number of rows to span (See this post for more details on multirow).

 

colortab, multirow and partial horizontal double borders

If you want to use cells that span multiple rows with partial horizontal borders without using colortbl, it is quite a bit more complex because you cannot use hhline. You have to use more TeX primitives. First, let’s look at the result.

 


\begin{tabular}{lc}
	\hline\hline
	\LCC
		\Blue & \DarkGrey\\
		\multirow{5}{*}[2.5em]{\Black Blue} & \White First Grey\Black\\
		\omit&\omit\hrulefill\\
		\omit&\omit\White\leavevmode%
			\leaders\hrule height \doublerulesep\hfill%
			\Black\kern0pt\\
		\omit&\omit\hrulefill\\
		& \White Second Grey\Black\\
		\noalign{\hrule%
			\White\hrule height \doublerulesep%
			\Black\hrule%
		}
	\ECC
\end{tabular}

&nbsp

Now, let us go over some of the more complex lines of code.

\multirow{5}{*}[2.5em]{\Black Blue}

This cell spans two rows. However, we created three fake rows to fake a double border. We have to adjust the value of the first argument of \multirow. Also, 2.5em is an arbitrary length to vertically center the content of the cell.

\omit&\omit\hrulefill\\

This is the first of the three fake rows. \omit is a TeX primitive that remove the basic style of a cell. It also ensures that the height of the cell is minimal. \hrulefill is the command to draw a line in a cell.

\White\leavevmode\leaders\hrule height \doublerulesep%
\hfill\Black\kern0pt

In the second fake row, we find this code. This is just the TeX definition of \hrulefill. We use its definition instead of the command to change its height to \doublerulesep. Before that, we changed the color to white and we reset it the black after.

Finally, a third fake row is inserted with the other partial horizontal rule.

 

Conclusion

In conclusion, colortab is a really complicated package. You better stick to colortbl if that is an option!


The author of this article is the main developer behind the LaTeX Complex Tables Editor.

Exit mobile version