Alternate Row Shading in LaTeX Table

by Alessandro

Wikibooks IconWikibooks IconThis tutorial has been contributed to the wikibooks project. You can find it at this link (currently accepted)

Table Data are notoriously hard to read line by line.

In the very same way, grades in a Curriculum can be hard to read.

A nifty and quick workaround is to shade alternate rows with different colours, preferably one white and the other a little darker.

Confused?

Apple will give you a hand: just look at your iTunes — and I will make your job easier by posting a screenshot here (click on them!).

iTunes. Notice the difference with Finder (next Screenshot)Finder. Different shading from iTunes

So usual question: how do we go from a normal table to a table with shaded rows, in LaTeX & Friends?

In images, how do we make the transition from the first to the second screenshot?

Table Without ShadingTable Shaded with Finder CombinationTable Shaded with iTunes Color Combination

The answer is simple and it is all a matter of looking into the xcolor package documentation, which provides you with the command which we will be using in order to get the shaded rows.

The package documentation is available, as always, on CTAN.

Let us start with the preamble of the document:

\documentclass{article}
 
\usepackage[table]{xcolor}
\definecolor{tableShade}{HTML}{F1F5FA}   %iTunes
\definecolor{tableShade2}{HTML}{ECF3FE} %Finder
 
\begin{document}

things to note down are:

  1. we use the xcolor package;
  2. we invoke the [table] option, otherwise we won’t be able to use the table color features;
  3. we define a new color which will be used to shade odd (or even) rows.

For our examples we use the following two colours, grabbed from iTunes and Finder on Mac OS X.

All you need to get your table now, besides the data, is ONE command (!).

I repeat: a single command will produce a table with odd rows and even rows of alternate colours.

The command in question is \rowcolors{1}{tableShade}{white}, to be inserted right before your table.

The syntax is pretty easy: \rowcolors{<starting row>}{<odd rows colour>}{<even rows colour>}

and we are done!

The code follows

\begin{center}
\rowcolors{1}{tableShade}{white}
%SOURCE http://en.wikipedia.org/wiki/List_of_craters_on_the_Moon,_T-Z#X
\begin{tabular}{l|c|l}
\scshape Crater		&	\scshape Diameter	 & \scshape Eponym\\ \hline
Zach		&	70 km	&	Franz Xaver von Zach (1754-1832)\\
Zagut		&	84 km	&	Abraham Ben Samuel Zagut (circa 1450-circa 1522)\\
Zähringer	&	11 km	&	Joseph Zahringer (1929-1970)\\
Zanstra		&	42 km	&	Herman Zanstra (1894-1972)\\
Zasyadko	&	11 km	&	Alexander Dmitrievich Zasyadko (1779-1837)\\
Zeeman		&	190 km	&	Pieter Zeeman (1865-1943)\\
Zelinskiy	&	53 km	&	Nikolay Dimitrievich Zelinskiy (1860-1953)\\
Zeno		&	65 km	&	Zeno of Citium (circa 335-263 B.C.)\\
Zernike		&	48 km	&	Frits Zernike (1888-1966)\\
Zhiritskiy	&	35 km	&	Georgiy Sergeevich Zhiritskiy (1893-1966)\\
Zhukovskiy	&	81 km	&	Nikolay Egorovich Zhukovskiy (1847-1921)\\
Zinner		&	4 km	&	Ernst Zinner (1886-1970)\\
Zöllner		&	47 km	&	Johann Karl Friedrich Zöllner (1834-1882)\\
Zsigmondy	&	65 km	&	Richard Adolf Zsigmondy (1865-1929)\\
Zucchius	&	64 km	&	Niccolo Zucchi (1586-1670)\\
Zupus		&	38 km	&	Giovanni Battista Zupi (circa 1590-1650)\\
Zwicky		&	150 km	&	Fritz Zwicky (1898-1974)
\end{tabular}
\end{center}
 
\end{document}

which produces

While this is a useful example, you should consider leaving table headings and the first row white, and using the shade colour from the third row onwards (by specifying 3 instead of 1 in the starting row argument.

Moreover, if you for any reason want to eliminate shading from a particular row, this can be easily done via the \hiderowcolors command, and reactivate shading via the \showrowcolors command.

this second table with custom end line is typeset by the following code:

\begin{center}
\rowcolors{3}{tableShade}{white}
\begin{tabular}{lcc}
\multicolumn{1}{c}{\textsc{Exam}}
		&\textsc{Grade}	&	\textsc{Grade Points}\\ \hline
Corporate Financial Strategy	&	A	&	4\\
Derivatives			&	A	&	4\\
Money, Credit, and Banking	&	A	&	4\\
Business Strategy 		& 	A-	&	3.5\\
				& 		&	 \\
\hiderowcolors			&\textsc{Gpa}	&\textbf{3.875}
\end{tabular}
\end{center}
LaTeX Source

XeLaTeX Source

PDF Output

PDF Output

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Live
  • PDF
  • Reddit
  • StumbleUpon
  • Twitter
  • Design Float
  • FriendFeed
  • Upnews
  • RSS

Related posts:

  1. CV Tip: Align Columns of Different LaTeX Tables with \settowidth
  2. Wrapping Text in Rounded Corners ColorBox in LaTeX with TikZ and PGF
  3. Advanced two-column LaTeX CV Template | Part 2 + TeX Source
  4. CurrVita & ClassicThesis LaTeX CV Template – 100 lines of code
  5. Writing Your Professional CV with LaTeX