Table

From ConTeXt wiki
Jump to: navigation, search

< Tables Overview | Tabulate | Tables >

This is ConTeXts oldest table module. It uses the same formatting as Tabulate (see Tables Overview).

This mode is based on Michael Wichura's TaBlE package for PlainTeX. The official manual for it is commercial (about 40 USD), see PCTeX -- but note that the TaBlE manual only talks about the original syntax, which does not use \NC, \HL cum suis.

The only ConTeXt docs are in ConTeXt - an excursion. There is also two introductory articles in tugboat ConTeXt basics for users: Table macros Table macros II by Aditya Mahajan (2007 and 2008).

Contents

Basic Commands

\starttable[|l|l|]
\HL
\NC Command	\VL Meaning	\SR % or \NC\AR
\HL
\NC \tex{NC}	\VL next column	\AR
\NC \tex{HL}	\VL horizontal line	\AR
\NC \tex{VL}	\VL vertical line	\AR
\NC \tex{NR}	\VL next row	\LR
\HL
\NC \tex{SR}	\VL single row	\AR
\NC \tex{FR}	\VL first row	\AR
\NC \tex{MR}	\VL middle row	\AR
\NC \tex{LR}	\VL last row \LR % or \NC\AR
\HL
\NC \tex{AR}	\VL automatic row	\SR % or \NC\AR
\HL
\stoptable

Column Definition

The table is defined by the template enclosed in square brackets after \starttable. The template has the form |keys for the first column|keys for the second column|...|keys for the last column|. Please note that each column is surrounded by | signs. These are necessary. The formatting keys for each column can be a choice of

Column definition examples

|l| 
a left aligned column, as wide as necessary
|lw(2cm)| 
a left aligned column of at least 2 cm width
|p(2cm)| 
a centered(!) paragraph of 2 cm width
|lp(.5\textwidth)| 
a left aligned paragraph of specified width
|rp(.5\textwidth)| 
a right aligned paragraph of specified width
|cp(.5\textwidth)| 
a center aligned paragraph of specified width
|xp(.5\textwidth)| 
a justified paragraph of specified width
... 
Please add more
... 

TODO: add more examples of column definitions (See: To-Do List)


Column Spans

It's possible to create columnspans (i.e. cells that span more than one column) with the command \use{N} where N is the number of columns spanned by the cell. It's often necessary to use \ReFormat[new keys]{} to reformat this specific cell according to the new keys.


\starttable[s(0pt)|ls(10pt)|rs(0pt)|]
\HL
\NC \use{2}\ReFormat[cB]{Spanning head} \SR
\HL
\NC \Use{2}[cB]{Spanning head} \SR % slightly shorted
\HL
\NC left text	\VL right column text         \NC \AR
\NC new row     \VL new row                   \NC \AR
\NC left text	\VL \ReFormat[l]{reformatted} \NC \AR
\HL
\NC \use{2}Spanning entry \SR 
\HL
\stoptable


(\ReFormat can be abbreviated \REF for brevity.)

Row Spans

It's also possible to create rowspans (i.e. cells that span more than one row) with the command \Raise(dimen){content} or \Lower(dimen){content} that raise or lower content by dimen.

\starttable[|c|c|]
\HL
\VL \Lower(.5\lineheight){a} \VL b \VL \AR
\DC \DL[1] \DR
\VL \VL c \VL \AR
\HL
\stoptable

(\Lower(.5\lineheight){a} can be abbreviated \LOW{a} for brevity.)

An alternative means of spanning rows by a tall object makes use of a bit of TeX magic: \smash{tall objext}:

\starttable[|M|c|]
\HL
\VL \VL a \VL \AR
\DC \DL[1] \DR
\VL \smash{\sum_0^N} \VL b \VL \AR
\DC \DL[1] \DR
\VL \VL c \VL \AR
\HL
\stoptable

Table as Floating Object

\placetable[here][tab:sample]{sample table}{
\starttable ...
\stoptable
}
\placetable[here,none][tab:sample]{}{
\starttable ...
\stoptable
}

Background Colors

Note: Adding color to tables using the \CL and \BL commands appears to be deprecated in MKIV; see: http://wiki.contextgarden.net/Tabulate

A very nice application in table are background colors for rows/cells (a feature that doesn't work in Tabulate):

\setupcolors[state=start]
\starttable[|l|l|]
\HL
\BL[1]\SR
\NC Command	\NC Meaning	\NC\SR
\HL
\NC \tex{NC}	\NC next column	\NC\FR
\NC \tex{NR}	\NC next row	\NC\LR
\HL
\CL[green]\SR
\NC \tex{AR}	\NC automatic row\NC\SR
\HL
\stoptable

The commands work something like this: first, you say what background colour you want for the next row and then you typeset the row. Observe: the line with the colour-command and the row it is supposed to colour should end in the same command (i.e. both \SR, \LR, \FR, ...). If they don't, the background won't cover the whole cell.

Fit Table Width

Hans posted a solution to the list for fitting a wide table (with paragraphs and vertical lines) to the page width. The key to his solution is the .45\textwidth settings when setting each cell as a paragraph.

\SetTableToWidth{\textwidth}

\starttable[|p(.45\textwidth)|p(.45\textwidth)|]
\HL
\VL foo foo foo foo foo foo \VL bar bar bar bar bar bar \VL\AR
\HL
\stoptable

Since table module has been under reconstruction this approach works only for MKII. In MKIV one can use

\starttable[|l|l|][textwidth=max]
\HL
\VL foo foo foo foo foo foo \VL bar bar bar bar bar bar \VL\AR
\HL
\stoptable

to change the width of the current table only.

\setuptables[textwidth=...] will affect the behavior of every table.

Booktabs

Latex has an excellent package called booktabs for typesetting tables. The main features of that package is that you can have top, mid, and bottom rules of different thickness. It is possible to achieve similar effects using tables. For example, to match the default settings of booktabs (Well almost, this gives a top and bottom rules of 0.09em while booktabs sets it to 0.08em).

\setuptables[rulethickness=0.03em]

\starttable[s0|l|i2l|i2r|]
  \HL[3]
  \NC \Use2[c]{Item}             \NC            \NC \AR
  \DL[2]                         \DC                \DR
  \NC Animal    \NC Description  \NC Price (\$) \NC \AR
  \HL[2]
  \NC Gnat      \NC per gram     \NC 13.65      \NC \AR
  \NC           \NC each         \NC  0.01      \NC \AR
  \NC Gnu       \NC stuffed      \NC 92.50      \NC \AR
  \NC Emu       \NC stuffed      \NC 33.33      \NC \AR
  \NC Armadillo \NC frozen       \NC  8.99      \NC \AR
  \HL[3]
\stoptable

C O N T E X T G A R D E N

Personal tools
Namespaces
Variants
Actions
Help
External Help
Installation
Navigation
Toolbox