# Why are my tables running off the edge of the page in ConTeXt?

I need to make a table which fits to the \textwidth, or essentially, the width generally available to text on the page. I have created columns of 20%, 60%, and 20%, which should fit the width, however, when I compile it, the final column runs off the edge of the page:

\setuppapersize             [A5]
\setuplayout                [backspace=40mm, topspace=10mm, width=98mm, height=190mm]
\starttext
\starttable[|xp(0.2\textwidth)|xp(0.6\textwidth)|xp(0.2\textwidth)|]
\HL
\NC A \NC \input knuth \NC Notes \NR
\NC B \NC \input knuth \NC \NR
\HL
\stoptable
\stoptext
• Why is my table running off the edge of the page?
• How can I create tables which fit to the \textwidth?
-
It's not running off the edge of the page by a whole lot, is it? I will bet a cookie that your problem is you haven't taken into account the space between columns. I don't remember the names of the length registers that control that spacing, unfortunately. –  zwol Oct 20 '12 at 2:37

As @Zack correctly says, context adds extra space between the columns. By default this is 0.5em. You can kill this space completely by starting the format prescription with s0, but that is probably not what you want.

To specify the width of tables use the \setuptables command and set the parameter textwidth. Thus to get tables exactly the width of the page you write:

\setuptables[textwidth=\textwidth]

and make sure that your total width specifications (including column spaces) does not exceed this value. context will then expand the table to fit the width.

Note, however, that there is a column space before the first column and some glue after the last column. To kill these use o0 in the format (it kills the space to the right of the current column). Below is an example document, without and with that adjustment.

\setuppapersize             [A5]
\setuplayout                [backspace=30mm, topspace=10mm,
width=fit, height=190mm]
\setuptables[textwidth=\textwidth]

\starttext
\hrule

\strut Text before tables.

\starttable[|xp(0.1\textwidth)|xp(0.6\textwidth)|xp(0.2\textwidth)|]
\HL
\NC A \NC Some text to put in the middle column. \NC Notes material \NC \AR
\HL
\stoptable

Text between tables.

\starttable[o0|xp(0.1\textwidth)|xp(0.6\textwidth)|o0xp(0.2\textwidth)|]
\HL
\NC A \NC Some text to put in the middle column \NC Notes material \NC \AR
\HL
\stoptable

\strut Text after tables.

\hrule

\stoptext

I have chosen a slightly different page layout for this demonstration. The glue to right of the final column is not visible here, but you will notice it if the total specified width is significantly less than the text width. Thanks to help from @Aditya spacing in the table is improved by ending the rows with \NC \AR. Note that Context Garden/Table tells us

You can leave out the \NC before the "row" command, but not if you use \AR in a last or single row

-
You do not need to manually add the struts. Rather use \AR to end the rows instead of \NR. –  Aditya Oct 20 '12 at 13:56
@Aditya Thanks for the reminder. Unfortunately using \AR is not quite equivalent to putting in the strut. In the above example removing b{\strut} and replacing \NR by \AR leaves the tops of the capital letters almost touching the horizontal rule. –  Andrew Swann Oct 20 '12 at 14:14
That is because you are missing an \NC. The table syntax is \NC ... \NC ... \NC \NR (note the \NC\NR in the end). So, you need to end with \NC\AR to get automatic row height. There is also \SR (for single row), \FR (for first row after a \HL and \LR (for last row before \HL). –  Aditya Oct 20 '12 at 16:54
@Aditya Great. I had tried all those commands, but without the \NC! I have updated my answer to include this. –  Andrew Swann Oct 20 '12 at 17:53

@Zack has already explained why this is happening and @Andrew Swann has presented a workaround. I'd like to point to an alternative. Use natural tables instead of simple tables. Natural tables are more flexible, and provide a better separation between content and presentation. Below is the same example with Natural Tables:

\startsetups table:width
\setupTABLE[align={hyphenated,normal}]
\setupTABLE[column][1][width=0.2\textwidth]
\setupTABLE[column][2][width=0.6\textwidth]
\setupTABLE[column][3][width=0.2\textwidth]
\stopsetups

\startsetups table:rules
\setupTABLE[frame=off]
\setupTABLE[row][first][topframe=on]
\setupTABLE[row][last][bottomframe=on]
\stopsetups

\starttext

\startTABLE[setups={table:width, table:rules}]
\NC A \NC Some text to put in the middle column. \NC Notes material \NC \NR
\NC A \NC Some text to put in the middle column. \NC Notes material \NC \NR
\NC A \NC Some text to put in the middle column. \NC Notes material \NC \NR
\stopTABLE

\stoptext
-