# Text-table overhaul

**URL:** <https://racket.discourse.group/t/text-table-overhaul/777>\
**Category:** Show & Tell\
**Created:** [March 8, 2022, 5:49pm UTC](https://racket.discourse.group/t/text-table-overhaul/777 "2022-03-08T17:49:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [March 8, 2022, 5:49pm UTC](https://racket.discourse.group/t/text-table-overhaul/777/1 "2022-03-08T17:49:42Z")

</div>

Just because I needed a simple way to display various numbers (non-rationals, large integers, floats, etc.) in a table, I ended up rewriting [`text-table`](https://pkgd.racket-lang.org/pkgn/package/text-table) almost entirely, except for frame styles (which were also enhanced relatively recently).

What's new:

- Rows can be aligned 'top, 'center, 'bottom for when cells are multiline.
- `#:->string`, `#:align` and `#:row-align` arguments use the rather flexible `pattern-list->list`:
  - `(pattern-list->list '(a B C ... ... d) 10)`  
→`(a B C B C B C B C d)`

- `~r*` can be used with `->string` to easily format columns of numbers (possibly including text)
- The source code is way more readable, factored and tested, which should help contributing.

(The online [docs](https://docs.racket-lang.org/text-table/index.html) may take some time to reflect the changes. Download/update the package to get the latest docs.)

Feedback most welcome. **Let me know in particular if I've broken your code in any way.** I will allow for backward incompatible changes _to these new features_ until the end of March, waiting for feedback.

Some examples:

```nohighlight
(let ()
    (define l (build-list 10 (λ (i) (expt (random) -4))))
    (print-table
     #:row-sep? #f
     #:align 'right
     #:->string (list (~r* #:precision '(= 2))
                      (~r* #:precision '(= 2) #:notation 'exponential))
     (transpose (list (list* 'Speed "" l) (list* 'Height "" (reverse l))))))
┌────────┬────────┐
│ Speed│ Height│
│ │ │
│ 20.36│2.65e+00│
│44887.08│3.58e+03│
│ 2.99│1.14e+00│
│ 4.82│3.30e+01│
│ 4181.24│4.42e+01│
│ 44.19│4.18e+03│
│ 33.03│4.82e+00│
│ 1.14│2.99e+00│
│ 3579.13│4.49e+04│
│ 2.65│2.04e+01│
└────────┴────────┘

(print-simple-table
   #:border-style 'empty
   #:align '(right left ... ...)
  (list (make-list 10 '*)
        (make-list 10 '**)
        (make-list 10 '***)
        (make-list 10 ' **** )
        (make-list 10 ' ***** )
        (make-list 10 "|")))
    ****  **** **    
   ********  ********  ****   
  ************  ************  ******  
 ****************  ****************  ******** 
**************************************************
    || || || || ||    

```

Make sure to also check out [uni-table](https://pkgd.racket-lang.org/pkgn/package/uni-table) which has many options and can use ansi colours.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [March 9, 2022, 11:18am UTC](https://racket.discourse.group/t/text-table-overhaul/777/2 "2022-03-09T11:18:49Z")

</div>

More new:

- `#:row-sep?` and the new `#:col-sep?` are pattern-lists, allowing (finally!) to have a separator just for the header (and more)
- Fixed the `'latex` style to use frames and separators correctly. This may be a somewhat backward-incompatible change as previous latex tables could not handle frames and separators. (But I consider this a bug fix.)
- Added an `'empty` style

```nohighlight
> (print-table
  (for/list ((i 5)) (for/list ((j 6)) (* (+ i 1) (+ j 1))))
  #:align 'right
  #:framed? #f
  #:row-sep? '(#t #f)
  #:col-sep? '(#t #f)
  #:border-style 'heavy)
1┃ 2 3 4 5 6
━╋━━━━━━━━━━━━━━
2┃ 4 6 8 10 12
3┃ 6 9 12 15 18
4┃ 8 12 16 20 24
5┃10 15 20 25 30

> (print-table
  (for/list ((i 5)) (for/list ((j 10)) (* (+ i 1) (+ j 1))))
  #:align 'right
  #:framed? #t
  #:row-sep? '(#t #f ... ...)
  #:col-sep? '(#t #f ... ...)
  #:border-style 'heavy)
┏━┳━━━━━┳━━━━━┳━━━━━┳━━━━━┳━━┓
┃1┃ 2 3┃ 4 5┃ 6 7┃ 8 9┃10┃
┣━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━┫
┃2┃ 4 6┃ 8 10┃12 14┃16 18┃20┃
┃3┃ 6 9┃12 15┃18 21┃24 27┃30┃
┣━╋━━━━━╋━━━━━╋━━━━━╋━━━━━╋━━┫
┃4┃ 8 12┃16 20┃24 28┃32 36┃40┃
┃5┃10 15┃20 25┃30 35┃40 45┃50┃
┗━┻━━━━━┻━━━━━┻━━━━━┻━━━━━┻━━┛

> (print-table
  (for/list ((i 5)) (for/list ((j 6)) (* (+ i 1) (+ j 1))))
  #:align 'right
  #:framed? #t
  #:row-sep? '(#t #f)
  #:col-sep? '(#t #f)
  #:border-style 'latex)
\begin{tabular}{|r|rrrrr|}
\hline
1 & 2 & 3 & 4 & 5 & 6 \\
\hline
2 & 4 & 6 & 8 & 10 & 12 \\
3 & 6 & 9 & 12 & 15 & 18 \\
4 & 8 & 12 & 16 & 20 & 24 \\
5 & 10 & 15 & 20 & 25 & 30 \\
\hline
\end{tabular}

```
