Alternate Row Shading in LaTeX Table
by Alessandro
![]()
This 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!).
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?
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:
- we use the xcolor package;
- we invoke the [table] option, otherwise we won’t be able to use the table color features;
- 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}
Related posts:









Comments
I recently came across your presentation of (“Sublinear and Locally Sublinear Prices” [http://aleplasmati.comuv.com/doc/sublinear_slides.pdf]) via a google LaTeX beamer search. I am new to LaTeX and and am learning as I am making a presentation via Beamer.
I noticed that your theme and font layouts in your presentation are different (and prettier) from the standard ones mentioned via the beamer documentation package. I was wondering if you could share the tex code of your presentation along with the pdf in your blog?
Hi,
I am afraid the code for that presentation was lost when my previous computer abandoned me.
The layout for these slides, however, is reproduced very easily:
the font is called Fontin (search for it on Google or on this website);
in order to use the font, you need to compile with XeLaTeX and use of the fontspec package (just like I did in this document);
I then removed the top sidebar from the theme. The predefined theme i used was MALMOE, removed the sidebar, see page 146 of the Beamer User Guide
Other than that, there were very few modifications I made.
Hope it helps
Alessandro
Thanks again for the all information in your blog and the pointers too!
This was incredibly useful! Muchas gracias!