Building a table with automatically numbered rows.

This example shows some simple macros for building a table whose items are numbered.

Here's what the table looks like

1 Bananas
2 Apples
3 Peaches
4 Oranges
5 Lemons

The table row numbers are generated using a couple of simple macros:

define(`num', `1') define(`incnum', `define(`num', incr(num))') The first of these is easy. It simply defines num as a macro that produces the number 1. The second macro, incnum is far more interesting. When m4 encounters it, its effect is to redefine the num macro to produce the next higher number. This is done using the built-in m4 macro incr which increments a number.

Given these macros, the construction of the table is trivial. Here is the complete htm4l text. Notice that I use another macro my_row to produce each row.

define(`num', `1') define(`incnum', `define(`num', incr(num))') define(`my_row', `<tr> <td align="center"><strong>num</strong></td> <td> $1 </td> </tr> incnum') <center> <table border="1" cellpadding="5"> my_row(`Bananas') my_row(`Apples') my_row(`Peaches') my_row(`Oranges') my_row(`Lemons') </table> </center>

This is a first example of defining a macro that defines another macro. I use this in the example that generates nx1 and 1xn tables and ordered and unordered lists. You can read about it here if you haven't checked it out already.


Back to the htm4l home page. Back to the htm4l examples page.
© Terry Jones (terry <AT> jon.es). Last modified: Mon Oct 2 02:21:32 CEST 2006