4

Background

Set up zebra striping using extreme tables. The ConTeXt tables are generated by converting an ASCII Markdown table via pandoc.

Minimum Example

\starttext
  \startplacetable[]
  \startxtable
  \startxtablehead[head]
  \startxrow
  \startxcell Item \stopxcell
  \startxcell[align=left] Cost (\$) \stopxcell
  \startxcell[align=left] Description \stopxcell
  \stopxrow
  \stopxtablehead
  \startxtablebody[body]
  \dorecurse{4}{
  \startxrow
  \startxcell Item \stopxcell
  \startxcell[align=left] 100 \stopxcell
  \startxcell[align=left] Cow text \stopxcell
  \stopxrow
  }
  \stopxtablebody
  \startxtablefoot[foot]
  \startxrow
  \startxcell {\bf Total} \stopxcell
  \startxcell[align=left] {\bf 400} \stopxcell
  \startxcell[align=left]  \stopxcell
  \stopxrow
  \stopxtablefoot
  \stopxtable
  \stopplacetable
\stoptext

Problem

ConTeXt, which is rather consistent with its setup macros, does not appear to define a \setupxrow or \setupxcell. I had anticipated something like:

\definecolor[lime][r=0.75, g=1, b=0] 
\definecolor[transparentred][r=1,t=.5,a=1]
\setupxrow[odd][background=lime]
\setupxrow[even][background=transparentred]

This did not compile.

Question

Without using definextable to create a custom xtable definition, how would you apply zebra striping so that every other non-header row has a coloured background?

7

With natural tables this would have been easy because the environment let you set different values for even and odd rows.

\startsetups [tablebackground]
  \setupTABLE [row] [odd] [background=color,backgroundcolor=gray]
\stopsetups

\starttext

\bTABLE[setups=tablebackground]
\dorecurse{9}{\bTR \expanded{\bTD Row \recurselevel \eTD} \eTR}
\eTABLE

\stoptext

Extreme tables on the other hand don’t support this kind of setup and you have to use the overlay mechanism to create your own backgrounds for the cells.

In the overlay setting you can check the value of the \currentxtablerow counter to apply a different command for odd and even rows.

\startuseMPgraphic {tablebackground}
    fill OverlayBox withcolor \MPcolor{gray} ;
\stopuseMPgraphic

\defineoverlay
  [tablebackground]
  [\ifodd\currentxtablerow
     \useMPgraphic{tablebackground}%
   \fi]

\starttext

\startxtable[background=tablebackground]
\dorecurse{9}{\startxrow \expanded{\startxcell Row \recurselevel \stopxcell} \stopxrow}
\stopxtable

\stoptext

To skip the table header:

\defineoverlay
  [tablebackground]
  [\ifnum\currentxtablerow>1
     \ifodd\currentxtablerow\useMPgraphic{tablebackground}\fi
   \fi]

To apply shading against even rows:

\startuseMPgraphic {tablebackground}
  fill OverlayBox withcolor \MPcolor{gray} ;
\stopuseMPgraphic

\defineoverlay
  [tablebackground]
  [\ifnum\currentxtablerow>1
     \ifodd\currentxtablerow \else
       \useMPgraphic{tablebackground}%
     \fi
   \fi]

\setupxtable[background=tablebackground]

To apply the background against all tables in the document, use:

\setupxtable[background=tablebackground]

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.