Search Locate Previous Next Contents

How to Format a Table

Formatting tables is one of the main strengths of NewLeaf, in particular its ability to wrap text within cells and provide exact control over column spacing and alignment. These are all tasks which have become very much harder now that we must work with proportionally spaced fonts, possibly in a variety of sizes. You can use nltList for simple columnar reports or nltSpread if you need detailed control over cell wrapping and formatting.

Tables can be formatted very precisely, but an easy start is just to take all the defaults and let NewLeaf do it for you:

nlUse ''                 © Default page definition
nltList 3 4½¼12     © Tabulate some data
pg„nlClose
psView pg            © To preview it

This will divide the frame width evenly into 4 columns, border the table, rule vertically and add horizontal hairline rules between rows. Note that there is no need to format the data if you are happy with APL’s default format.

In this example, all columns are left aligned (the default) and there are no headings. To set these properties on the table:

nltAlign 'left' 'right' 'decimal2'    © Columns
nltTitles 'Name' 'Age' 'Salary'

or ...

nltSet ('al' 'le' 'ri' 'd2')('co' 'Name' 'Age' 'Salary')

Note that all column properties are reshaped to match the data, so to alternate left and right, just give two alignments.

Typically, tables are best fitted automatically into the frame width, but you will want to specify exactly the majority of the cell widths. For example:

nltCellWidths  ¯1 72 72

Here the rightmost columns (age, salary) are both 1” wide (remember that all dimensions are in points) and any spare space in the frame is given to the name. If you only give widths for some of the columns, the remainder share the available space evenly. Of course if you specify all the columns explicitly, then the overall size of the table is simply the sum of the cell widths, as long as this is less than or equal to the frame width. If it is greater, you will find that both List and Spread will fold the entire table, using as many pages as necessary.

Note that Spread wraps cell contents automatically into the grid, and that a cell is never broken across a page boundary. List only accepts one line of data per cell, and can be set to clip at cell boundaries if required.

Using tree-structured headings
A common requirement is to span several columns with a top-level heading, then break down into more detail:

  ÚÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÌ
  Û            Salaries Report 1996                 Û
  ÃÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÂÎÎÎÎÎÎÎÎÂÎÎÎÎÎÎÎÎÎÎÝ
  Û Name                        Û    Age Û   Salary Û
  ÃÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÎÎÎÝ
  Û Adrian Smith                Û     42 Û    5,678 Û
  Û And so on                   Û     21 Û  123,000 Û
  ÀÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÁÎÎÎÎÎÎÎÎÁÎÎÎÎÎÎÎÎÎÎÙ

Assuming column widths of 300, 100, 100, this could be set up with the following NewLeaf commands:

nltCellWidths 300 100 100
nltTitles 'Salaries Report 1996' 'Name' 'Age' 'Salary'
nltTDepth 0 1 1 1
nltSpread ‘data
psView nlClose

The TDepth property defines a tree-structure – it should have the same number of entries as there are titles, and (of course) there should be as many ‘leaf’ entries as there are columns. To give multi-line titles, simply enclose the relevant elements ...

nltTitles 'Name' ('Age' 'at entry')('Salary paid' 'to date' '1996')

... and the title row will automatically expand to accommodate 3 lines. You may also want to use soft hyphens in long column names to allow NewLeaf the flexibility to line-break at sensible points.

Controlling the grid lines
There are three line weights associated with the table: the outer border, the vertical rules, and horizontal hairlines. These default to 1pt, 0.8pt and .01 pt (hairline) respectively. To get a heavy border and no horizontal lines:

nltSet 'Grid' 3 1 0 © weight in points

The 1st element is always scalar, but you may need to rule vertically between some columns and not others (or separate some with a heavier line), and similarly for the horizontal separators. To do this, you specify a vector of line weights as the middle or last element of the set:

nltSet 'Grid' 1 (0 .5) .01

... which will alternate on/off between cells.

If you want a heavy horizontal separator, you can simply call the table’s Rule function which rules horizontally between the table edges:

nlUse ''
nltSpread ‘mat95
nltRule 2           © Rule across table
nltSpread ‘mat96
nlClose

Alternatively, you can specify a vector of weights for the horizontal lines as the third element of the Gridlines.

Checking the space available

NewLeaf will report exactly how much space is left in the current frame, so that you can check this and skip manually to a new frame if the space cannot accommodate sufficient rows. See Info in the functions reference for more detail.

Handling wide tables
In NewLeaf you have two approaches to formatting a table which does not fit comfortably on one page:

In general, you will find that List is faster (it has fewer formatting options, for example it cannot wrap text automatically to create multi-line cells) but Spread is more flexible (it allows you to specify font and colour by cell, rather than by column). Both options allow you to hold an arbitrary selection of columns, so that they repeat on every part-page (see the keystyle property) and Spread additionally allows you to specify an upper limit to the number of pages wide (see the fit to page property), in which case the finished report is uniformally reduced to fit on the paper.

For example, to fit a 52-week sales summary across no more than 3 pages, with the first 2 columns repeated on each part-page:

nlUse ''
nltTitles ('Item' 'Name','W',¨•¨¼52)
nltFit 3    
nltCellWidths 72 144,52½56   
nltHeading 'Sales Report for 1999'
nltKeyStyle 1 1,52½0
nltAlign 2 52/'left' 'right'
nltSpread ('X101' 'Y234','Jam' 'Cake',?2 52½1000)
psView PG„nlClose

Note that if you are using the ‘fit to page’ capability you should set the heading as a property of the table so that it repeats at the head of the ‘first’ page and is reduced by the same factor as the table body.



Continue to: How to Design a Form Letter
© Copyright Causeway Graphical Systems Ltd 2001