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. Continue reading

Advertisements

Would a table by any other name be as useful

Karl Berry recently blogged about creating the following in TeX:
photo table

A 6×6 grid of pictures, with a big picture of size 2×2 in the middle. All pictures should be surrounded by a border.

Karl said that he tried pdfpages package but, in the end, used low-level \halign. Ouch! Use \halign. I don’t know how many people who start with LaTeX or ConTeXt actually know the syntax of \halign. But what I find shocking is that Karl’s next best choice was pdfpages, a package to combine pdf pages. Is the state of affairs with LaTeX tables so bad?

I tried to achieve that same layout in ConTeXt, and it was really simple.

    % Placeholders for figures
    \useexternalfigure[photo] [cow]   [width=2cm, height=1.5cm]
    \useexternalfigure[center][cow]   [width=4cm, height=3cm]

    \setupTABLE[each][each][framecolor=lightred, rulethickness=3bp, frame=on, strut=off]

    \starttext

    \startTEXpage
    \bTABLE
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD[nr=2,nc=2] \externalfigure[center]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
      \bTR
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
        \bTD \externalfigure[photo]\eTD
      \eTR
    \eTABLE
    \stopTEXpage

    \stoptext

Notice the [nr=2,nr=2] for the middle cell. This tells ConTeXt that the middle cell spans 2 rows and 2 columns. And thats it!

For another, more extreme, example see Willi Egger’s My Way on drawing type cases (wooden cases with boxes for storing letter types) used in hand typesetting.

Edit: In a follow-up post, Will Robertson said that he does not like the verbose syntax of ConTeXt tables. Neither do I. As a matter of fact, ConTeXt has multiple table implementations. The oldest table macros, \starttable ... \stoptable were just a wrapper around the TaBlE macros by Michael Wichura. The syntax of this macro is:

\starttable
     \NC .... \NC .... \NC .... \NC \NR
     \NC .... \NC .... \NC .... \NC \NR
     \NC .... \NC .... \NC .... \NC \NR
\stoptable

But this mechanism is not as powerful as natural tables (that is what the mechanism that I posted is called). There is, however, a wrapper around natural tables that uses the syntax of the old table macros but provides most of the functionality of natural tables:

\startTABLE
     \NC .... \NC .... \NC .... \NC \NR
     \NC .... \NC .... \NC .... \NC \NR
     \NC .... \NC .... \NC .... \NC \NR
\stopTABLE

This is meant for simple tables. Therefore, the \NC macro does not accept optional arguments. As a result, we cannot use this simpler interface to get the above result. Unless, of course, we cheat. Note the definition of \TC (tablular cell) below and the deliberate use of \1 and \9 to make the code appear less verbose 🙂

\setupTABLE[each][each][frame=on, rulethickness=3bp, rulecolor=lightred]
\def\TC{\eTD \dobTD}

\def\1{\externalfigure[cow][width=2cm, height=1.5cm]}
\def\9{\externalfigure[cow][width=4cm, height=3cm]}

\starttext

\startTABLE
  \NC \1 \NC \1 \NC \1 \NC \1     \NC \1 \NC \1 \NC \NR
  \NC \1 \NC \1 \NC \1 \NC \1     \NC \1 \NC \1 \NC \NR
  \NC \1 \NC \1 \TC[nc=2,nr=2] \9 \NC \1 \NC \1 \NC \NR
  \NC \1 \NC \1                   \NC \1 \NC \1 \NC \NR
  \NC \1 \NC \1 \NC \1 \NC \1     \NC \1 \NC \1 \NC \NR
  \NC \1 \NC \1 \NC \1 \NC \1     \NC \1 \NC \1 \NC \NR
\stopTABLE

\stoptext