Adding color to tables

Texblog had an interesting post on creating tables with alternating colors. See the pdf for the final output. I thought that it will be interesting to see how to reproduce the same effect in ConTeXt. This is the table that I am going to use for my tests:

\startbuffer[contents]
\NC       \NC Col 1 \NC Col 2 \NC Col 3 \NC Col 4 \NC Col 5 \NC \NR
\NC Row 1 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\NC Row 2 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\NC Row 3 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\NC Row 4 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\NC Row 5 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\NC Row 6 \NC 1     \NC 2     \NC 3     \NC 4     \NC 5     \NC \NR
\stopbuffer

First lets get the frames right. The example typesets each table with a rules around the first row, a vertical rule after the first column, and a rule after the last column. To get that, use

\startsetups table:frame
\setupTABLE[each][each][frame=off, align=middle, background=color, rulethickness=1bp]
\setupTABLE[row][first][bottomframe=on,topframe=on]
\setupTABLE[row][last] [bottomframe=on]
\setupTABLE[column][first][rightframe=on]
\stopsetups

That is pretty self explanatory. By default, all cells in a table have a frame, so I switch that off with frame=off; each cell should be middle aligned, so I use align=middle; background=color is for later when I will set the color for the table; rulethickness sets the thickness of the line. This setup gives the following result:

\startTABLE[setups=table:frame]
  \getbuffer[contents]
\stopTABLE

Now, lets try the different color setups.

Rows with alternative colors

This one is simple. Set

\startsetups table:rows
\setupTABLE[row][odd] [backgroundcolor=yellow:1]
\setupTABLE[row][even][backgroundcolor=blue:1]
\stopsetups

The colors yellow:1 and blue:1 mean 10% yellow and blue. Then add this setup to the table to get the following result:

\startTABLE[setups={table:frame,table:rows}]
  \getbuffer[contents]
\stopTABLE

Columns with alternative colors

This is almost identical to the row setup.

\startsetups table:columns
\setupTABLE[column][odd] [backgroundcolor=yellow:1]
\setupTABLE[column][even][backgroundcolor=blue:1]
\stopsetups

\startTABLE[setups={table:frame,table:columns}]
  \getbuffer[contents]
\stopTABLE

Chessboard coloring

And now for the most interesting setup: coloring the table cells like a chessboard.

\startsetups table:chessboard
\setupTABLE[each][each][backgroundcolor=yellow:1]
\setupTABLE[odd][odd]  [backgroundcolor=blue:1]
\setupTABLE[even][even][backgroundcolor=blue:1]
\stopsetups

\startTABLE[setups={table:frame,table:chessboard}]
  \getbuffer[contents]
\stopTABLE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s