Arrays with Style
Vector 8:3, page 140
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 140
Introduction¶
This is by way of a response to Richard Nabavi’s thought provoking piece on array formatting with real (i.e. proportional) typefaces. I don’t pretend for a moment that I can offer him a complete solution, but I think he should start by buying two outstanding pieces of current software (Microsoft Word for DOS and Microsoft Excel) and understanding the concept of style sheets.
Incidentally, this could lead to the implementation in APL of the STYLE ERROR, first proposed (not entirely seriously) by Robert Bittlestone somewhere in Vector Vol.1.
What is a Style Sheet?¶
Let’s start with MS Word, mainly because I know it better than Excel, and because I can illustrate what I mean as I type. Word attaches something called a style sheet to every document; this defines the appearance of text for:
- each section of the document (of course there may be only one section, even for a long document like VECTOR). This covers things like page-layout (single/double column, margins), running heads, page numbering and so on.
- each paragraph. A paragraph is defined simply as anything between two carriage-returns. The paragraph style defines indents, line spacing, a default character font and size, and various special attributes like ‘keep together’.
- each character. Characters normally inherit the style of their parent paragraph, but can be changed individually if required. Oddly, Word has no concept of a word as such; it is simply a collection of contiguous characters.
Let’s have a look at a selection of paragraph styles by way of illustration:
6 T Paragraph Heading level 1 centred title - top of page
Palatino (roman k) 24/28. Centered, space after 1 li (keep in one
column, keep with following paragraph).
7 N Paragraph 8 Author's name in headings
Palatino (roman k) 15/18 Italic. Centered, space after 2 li.
8 S Paragraph Heading level 2 subheading
Palatino (roman k) 15/18 Bold. Flush left, space before 1 li, space
after 0.5 li (keep with following paragraph).
9 P Paragraph Standard std para
Palatino (roman k) 13/16. Justified, space after 1 li.
ADOBE-PL.STY
The style code (T, P etc.) is shown in the left margin of the document, and you ‘apply’ a style simple by hitting alt+style, e.g. if I were to hit alt+S now, I would get this paragraph in ‘subheading’ style. If you scan down the first page of this note, you will spot the ‘T’ and ‘N’ styles at the top, and also the use of an ‘I2’ indented block in a slightly smaller type size on reduced spacing. Typical character styles are used to set things like Bold, italic and (of course) APL:
45 B Character 2 boldface
Palatino (roman k) 13 Bold.
46 IT Character 3 Italic
Palatino (roman k) 13 Italic.
47 A Character 4 APL chars are Courier-Obliq.
Courier (modern a) 14 Italic.
48 J Character 7 J is Courier Bold
Courier (modern a) 13 Bold.
49 Z Character 5 Dingbats
ZapfDingbats (symbol e) 13.
50 E Character 6 Exponentials (superscripts)
Palatino (roman k) 8 Bold Superscript.
ADOBE-PL.STY
To set things like x², I simply put the cursor over the ‘2’ and hit alt+E. Of course, you can format text ‘directly’ in any font you like, but one of the things you soon learn is that this is a very bad idea! The whole point about formatting with styles is that the appearance of a document is independent of the content. If I want to switch Vector to New Century Schoolbook, all I have to do is define a new style-sheet (obviously being totally consistent in my use of style names) and attach it to the document. The same would apply to a switch from the current A5 format to (say) double-column A4. As long as everything has been set with style-codes, changes like this are trivial. If the formatting is all convolved with the text (see any Bindweed plant for the meaning of the word convolved) as it typically is in WordStar and WordPurrfect, life is much harder.
What has this to do with APL?¶
In many ways, the Excel model is closer to what we need in APL; here you apply a style to a cell (or a group of cells), with a special style (called ‘Normal’) applied by default to the whole spreadsheet. As well as the character font and size, the cell style specifies things like formatting (£9.99 etc.), colours and borders.
How about this for an idea:
- let’s start with a Workspace Style, which specifies the default type style, and includes basic formatting instructions currently scattered about in
⎕PP,⎕PWetc. Let’s call this style ‘Normal’ and have it apply to everything we display. It should include a default ‘cell width’ (in inches, cm or points) for display of numeric scalars or character vectors. Numbers get right aligned in the cell; text is left aligned. - let’s allow the user to edit a style sheet (similar to the examples above) to define a consistent set of display attributes for text and numbers. Styles have simple mnemonic names, and are maintained by APL as a file. Style sheets can be loaded and saved (just like workspaces), and can be attached to workspaces with a command such as:
⎕STYLE←'POSTSCRP.STY' ⍝ Pick up an existing style sheet
⎕STYLE
POSTSCRP
⎕STYLE←'JUNK.STY' ⍝ This does not exist
2+2
STYLE ERROR ⍝ Default output fails
- now, we need an extra slot in APL’s array descriptor: as well as attributes like type, rank and shape, an array also has style (in reality simply a pointer to an entry in the current style sheet). To set the style of our data, we need something that looks and feels a lot like reshape, but instead of modifying the shape elements of the descriptor, it modifies the style:
DATA←2 6⍴⍳12 ⍝ Here is an array
DATA←'BOXED'⍢DATA ⍝ Attach the 'boxed' style
DATA
┌──────────────────────────────────────────┐
│ 1.0 2.0 3.0 4.0 5.0 6.0 │
│ 7.0 8.0 9.0 10.0 11.0 12.0 │
└──────────────────────────────────────────┘
'currency' ⍢ ⍳5
£0.00 £1.00 £2.00 £3.00 £4.00 £5.00 ⍝ These are numbers!
… and so on. With nested arrays, you have all the tools you need, as long as you are willing to enclose your data at as low a level as necessary to make each piece of formatting specific to a single array:

I don’t suppose for a moment that this would work as simply as my examples suggest, but it would be fascinating to see some prototypes. Over to you Richard (and John and Dave and Paul); perhaps it will turn up in J for the Archimedes??