Open main menu

Changes

6,724 bytes added ,  13:24, 9 August 2020
m
Text replacement - "<cmd>" to "{{cmd|"
< [[Tables Overview]]| [[Tabulate]] | [[Tables]] >
This is ConTeXts oldest table module. It uses the same formatting as [[tabulateTabulate]]. (See see [[Tables Overview]] for an overview).)
This mode is based on Michael Wichura's TaBlE package for PlainTeX. The official manual for it is commercial (about 40 USD), see [http://www.pctex.com/books.html PCTeX] -- but note that the TaBlE manual only talks about the original syntax, which does not use {{cmd|NC}}, {{cmd|HL}} cum suis.
 
The only ConTeXt docs are in [[manual:ms-cb-en.pdf|ConTeXt - an excursion]]. There is also two introductory articles in tugboat [http://tug.org/TUGboat/Articles/tb28-3/tb90mahajan.pdf ConTeXt basics for users: Table macros] [http://www.tug.org/TUGboat/Articles/tb29-1/tb91mahajan.pdf Table macros II] by Aditya Mahajan (2007 and 2008).
 
== Basic Commands ==
 
<table cols="2"><tr valign="top"><td>
<texcode>
\placetable[here][tab:sample]{sample table}
\starttable[|l|l|]
\HL
\NC Command \NC VL Meaning \SR % or \NC\SRAR
\HL
\NC \tex{NC} \NC VL next column \NC\FRAR\NC \tex{HL} \NC VL horizontal line \NC\MRAR\NC \tex{VL} \NC VL vertical line \NC\MRAR\NC \tex{NR} \NC VL next row \NC\LR
\HL
\NC \tex{SR} \NC VL single row \NC\FRAR\NC \tex{FR} \NC VL first row \NC\MRAR\NC \tex{MR} \NC VL middle row \NC\MRAR\NC \tex{LR} \NC VL last row \LR % or \NC\LRAR
\HL
\NC \tex{AR} \NC VL automatic row \SR % or \NC\SRAR
\HL
\stoptable
</texcode>
</td><td>
<context>
\switchtobodyfont[ss, 8pt]
\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
</context>
</td></tr></table>
* You get vertical lines (rules), if you use {{cmd|VL}} instead of {{cmd|NC}}.* Better use <{{cmd>|SR</cmd>}}, <{{cmd>|FR</cmd>}}, <{{cmd>|MR</cmd>}}, <{{cmd>|LR</cmd> }} instead of <{{cmd>|NR</cmd>}}. * You can also use <{{cmd>|AR</cmd> }} instead of <{{cmd>|SR</cmd>}}, <{{cmd>|FR</cmd>}}, <{{cmd>|MR</cmd> }} and <{{cmd>|LR</cmd> }} (AR for automatic row).* You can leave out the {{cmd|NC}} before the "row" command, but not if you use {{cmd|AR}} in a last or single row (see example).* You can influence the table with {{cmd|setuptables}}.
If you need information about <cmd>placetable</cmd> look after <cmd>placefloat</cmd> in the manual!If you'd like to leave out the table's caption, you can't simply leave the braces empty but must write <tt>none</tt>!==Column Definition==
This mode The table is based on Michael Wichura's TaBlE package for PlainTeXdefined by the template enclosed in square brackets after {{cmd|starttable}}. The official manual template has the form<tt>|keys for the first column|keys for it is commercial (about 40 USD), see [http://wwwthe second column|..pctex.com PCTeX] -- but note that the TaBlE manual only talks about |keys for the original syntax, which does not use <cmd>NClast column|</cmdtt>, . Please note that each column is surrounded by <cmdtt>HL|</cmdtt> cum suissigns.These are necessary. The formatting keys for each column can be a choice of
The table is defined by the template enclosed in square brackets after <cmdcontext>\switchtobodyfont[ss, 8pt]\starttable</cmd>. The template has the form<tt>[|keys for the first column|keys for the second columnl|lp(...|keys for the last column|</tt>. Please note that the each column is surrounded by <tt>5\textwidth)|</tt> signs. These are a necessary. The formatting keys for each column can be a choice of]{|\HL! \NC \bf Key || \VL \bf Meaning\SR|-\HL! colspan="2" | \NC \NC Primitive keysKeys \SR|-\HL|\NC \type{a{<i>tokens</i>} || }\VL Adds <i>\type{tokens</i> '''} {\em after''' } the column content\AR|-|\NC \type{b{<i>tokens</i>} || }\VL Adds <i>\type{tokens</i> } '''before''' the column content\AR|-|\NC {\tt \backslash\{ || } \VL Enclose the column in braces(grouping)\AR|-|\NC \type{*{<i>n</i>}{<i>keys</i>} || }\VL Equivalent to repeating the formatting keys <i>\type{keys</i> <i>} \type{n</i> } times\NC\LR|-\HL! colspan="2" | \NC \NC Positioning keysKeys\SR|-\HL|\NC \type{\LeftGlue || } \VL Specifies the left glue to be used before the column\AR|-|\NC \type{\RightGlue || } \VL Specifies the right glue to be used after the column\AR|-|\NC \type{l || Left} \VL left-align the aligned column\AR|-|\NC \type{c || Center the } \VL centered column\AR|\NC \type{r} \VL right-aligned column\AR|r || Right\NC \type{p(width)} \VL Set each cell as a paragraph\AR\NC \type{s(width)} \VL Specify the inter-align the columnwidth\AR|-\NC \type{w} \VL Set minimum column width\AR|\NC \type{k || } \VL Insert a kern both left and right of the column\AR|-|\NC \type{i || } \VL Add a kern to the left of the column\AR|-|\NC \type{j || } \VL Add a kern to the right of the column\LR|-\HL! colspan="2" | \NC \NC Numeric item keysand Math Item Keys \SR|-\HL|\NC \type{n || } \VL Numeric item not in math mode\AR|-|\NC \type{N || } \VL Numeric item in math mode\AR|-! colspan="2" | Attribute keys|-|\NC \type{m || } \VL Each cell is in (inline) math mode. Equivalent to <tt>\type{b$ a$</tt>}\AR|-|\NC \type{M || } \VL Each cell is in display math mode. Equivalent to <tt>\type{\{b{$\displaystyle}a$</tt>}} \AR|-|\NC \type{\m || } \VL Equivalent to <tt>\type{l b{{}}m</tt>}\AR|-|\NC \type{\M || } \VL Equivalent to <tt>\type{l b{{}}M</tt>}\LR\HL\NC \NC Style Keys \SR|-\HL|\NC \type{f || \command} \VL Set font according to following <cmd>\tex{command</cmd>}\AR|-|\NC \type{B || } \VL Bold. Equivalent to <tt>\type{f\bf</tt>}\AR|-|\NC \type{I || } \VL Italic. Equivalent to <tt>\type{f\it</tt>}\AR|-|\NC \type{S || } \VL Slanted. Equivalent to <tt>\type{f\sl</tt>}\AR|-|\NC \type{R || } \VL Roman. Equivalent to <tt>\type{f\rm</tt>}\AR|-|\NC \type{T || } \VL Teletype. Equivalent to <tt>\type{f\tt</tt>}\AR|-\NC \type{C} \VL Color. Use it in combination with \backslash\{ (e.g. \backslash\{C\{red\} )\LR|p || Set each cell as a paragraph\HL|-! colspan="2" | Minimum column width key|-|w || Set minimum column width|-! colspan="2" | \NC \NC Tabskip keysKeys \SR|-\HL|\NC \type{s || } \VL Set the tabskip to the right of this column and of all following columns until up to the next <tt>\type{s</tt> } or <tt>\type{o</tt> } key\AR|-|\NC \type{o || } \VL Set the tabskip to the right of this column only.\LR\HL\stoptable|}</context>
=== Column definition examples===
; <code>|l|</code> : a left aligned column, as wide as necessary
; <code>|lw(2cm)|</code> : a left aligned column of at least 2 cm width
; <code>|p(2cm)|</code> : a centered(!) paragraph of 2 cm width
; <code>|lp(.5\textwidth)|</code> : a left aligned paragraph of specified width
; <code>|rp(.5\textwidth)|</code> : a right aligned paragraph of specified width
; <code>|cp(.5\textwidth)|</code> : a center aligned paragraph of specified width
; <code>|xp(.5\textwidth)|</code> : a justified paragraph of specified width
; <code>...</code> : Please add more
; <code>...</code> :
It's possible to create columnspans (i.e. cells that span {{todo|add more than one examples of column) with the command <cmd>use[<i>N</i>]</cmd> where <i>N</i> is the number of columns spanned by the cell. It's often necessary to use <cmd>ReFormat[<i>new keys</i>]</cmd> to reformat this specific cell according to the <i>new keys</i>.definitions}}
The only ConTeXt docs about it are in [http://www.pragma-ade.com/general/manuals/ms-cb-en.pdf ms-cb-en.pdf].==Column Spans==
The sample looks like It's possible to create columnspans (i.e. cells that span more than one column) with the command {{cmd|use{<i>N</i>}}} where ''N'' is the number of columns spanned by the cell. It's often necessary to use {{cmd|ReFormat[<i>new keys</i>]{}}} to reformat this:specific cell according to the ''new keys''.
 
<table cols="2"><tr valign="top"><td>
<texcode>
\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
</texcode>
</td><td>
<context>
\setuppapersize[A5]
\starttable[s(0pt)|ls(10pt)|rs(0pt)|]
\HL
\NC \use{2}\ReFormat[cB]{Spanning head} \SR
\HL
\NC \Use{2}[cB]{Spanning head} \SR
\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
</context>
</td></tr></table>
 
 
({{cmd|ReFormat}} can be abbreviated {{cmd|REF}} for brevity.)
== Row Spans==
 
It's also possible to create rowspans (i.e. cells that span more than one row) with the command {{cmd|Raise(<i>dimen</i>){<i>content</i>}}} or {{cmd|Lower(<i>dimen</i>){</i>content</i>}}} that raise or lower ''content'' by ''dimen''.
 
<table cols="2"><tr valign="top"><td>
<texcode>
\starttable[|c|c|]
\HL
\VL \Lower(.5\lineheight){a} \VL b \VL \AR
\DC \DL[1] \DR
\VL \VL c \VL \AR
\HL
\stoptable
</texcode>
</td><td>
<context>
\placetablestarttable[here][tab:sample|c|c|]\HL\VL \Lower(.5\lineheight){sample tablea}\VL b \VL \AR\starttableDC \DL[|l|l|1]\DR\VL \VL c \VL \AR
\HL
\NC Command stoptable</context></td></tr></table> ({{cmd|Lower(.5\NC Meaning lineheight){a}}} can be abbreviated {{cmd|LOW{a}}} for brevity.) An alternative means of spanning rows by a tall object makes use of a bit of TeX magic:{{cmd|smash{tall object}}}: <table cols="2"><tr valign="top"><td><texcode>\NC\SRstarttable[|M|c|]
\HL
\NC VL \VL a \VL \tex{NC} AR\NC next column DC \NCDL[1] \FRDR\NC VL \texsmash{HL\sum_0^N} \NC horizontal line VL b \NCVL \MRAR\NC DC \tex{VL} \NC vertical line DL[1] \NC\MRDR\NC VL \tex{NR} VL c \NC next row \NCVL \LRAR
\HL
\NC \tex{SR} \NC single row \NC\FRstoptable</texcode>\NC \tex{FR} \NC first row \NC\MR</td><td>\NC \tex{MR} \NC middle row \NC\MR<context>\NC \tex{LR} \NC last row \NC\LRstarttable[|M|c|]
\HL
\NC VL \texVL a \VL \AR\DC \DL[1] \DR\VL \smash{\sum_0^N} \VL b \VL \AR} \DC \DL[1] \DR\VL \NC automatic row VL c \NCVL \SRAR
\HL
\stoptable
</context>
</td></tr></table>
 
==Table as Floating Object==
 
<texcode>
\placetable[here][tab:sample]{sample table}{
\starttable ...
\stoptable
}
</texcode>
 
* See [[Floating Objects]] in general.
* If you need information about {{cmd|placetable}} look after {{cmd|placefloat}} in the manual or texshow!
* If you do not want a caption for your table, to get rid of it altogether you have to add "none" to settings and then leave the braces empty; if you only leave the braces empty, your table will still be numbered ("Table 1" etc.).
<texcode>\placetable[here,none][tab:sample]{}{\starttable ...\stoptable}</texcode> ==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[[Tabulate]]): <table cols="2"><tr valign="top"><td>
<texcode>
\setupcolors[state=start]
\placetable[here][tab:sample]{sample table}
\starttable[|l|l|]
\HL
\BL[21]\SR
\NC Command \NC Meaning \NC\SR
\HL
\NC \tex{NC} \NC next column \NC\FR
\NC \tex{HL} \NC horizontal line \NC\MR
\NC \tex{VL} \NC vertical line \NC\MR
\NC \tex{NR} \NC next row \NC\LR
\HL
\CL[green]\SR
\NC \tex{AR} \NC automatic row \NC\SR
\HL
\stoptable
</texcode>
The commands work something like this: first, you say what background colour you want for the next rowand then you typeset the row. Observe: the line with the colour-command and the row it is supposedto colour should end in the same command (i.e. both \SR, \LR, \FR, ...). If they don't, the backgroundwon't cover the whole cell. * \BL makes a gray background: the optional argument tells BL how many cells it should color* \CL makes a colored line</td><td>
<context>
\setupcolors[state=start]
\placetable[here][tab:sample]{sample table}
\starttable[|l|l|]
\HL
\HL
\NC \tex{NC} \NC next column \NC\FR
\NC \tex{HL} \NC horizontal line \NC\MR
\NC \tex{VL} \NC vertical line \NC\MR
\NC \tex{NR} \NC next row \NC\LR
\HL
\CL[green]\SR
\NC \tex{AR} \NC automatic row \NC\SR
\HL
\stoptable
</context>
</td>
</tr></table>
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.
 
* {{cmd|BL}} makes a gray background: the optional argument tells BL how many cells it should color
* {{cmd|CL}} makes a colored row
 
==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 <code>.45\textwidth</code> settings when setting each cell as a paragraph.
 
<texcode>
\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
</texcode>
<!-- It makes no sense to typeset this here. -->
 
Since table module has been under [http://www.ntg.nl/pipermail/ntg-context/2010/055004.html reconstruction] this approach works only for MKII. In MKIV one can use
 
<texcode>
\starttable[|l|l|][textwidth=max]
\HL
\VL foo foo foo foo foo foo \VL bar bar bar bar bar bar \VL\AR
\HL
\stoptable
</texcode>
 
to change the width of the current table only.
 
<code>\setuptables[textwidth=...]</code> 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).
 
<table cols="2"><tr valign="top"><td>
<texcode>
\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
</texcode>
</td><td>
<context>
\setuppapersize[A5]
\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
</context>
</td></tr></table>
[[Category:Tables]]