# Defining new special formatting escapes

**URL:** <https://racket.discourse.group/t/defining-new-special-formatting-escapes/3844>\
**Category:** Questions & Answers\
**Created:** [July 10, 2025, 10:21pm UTC](https://racket.discourse.group/t/defining-new-special-formatting-escapes/3844 "2025-07-10T22:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendrikboom3](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hendrikboom3/32/2748_2.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)\
**Post date:** [July 10, 2025, 10:21pm UTC](https://racket.discourse.group/t/defining-new-special-formatting-escapes/3844/1 "2025-07-10T22:21:48Z")

</div>

Is it possible to define new special formatting escapes without rewriting fprintf and friends from scratch?  
I know it's possible to redefine how particular structures are printed, displayed, or written by adding a method to the struct definition.  
But I'm wanting to specify additional output methods in the the format string (such as ~u, ~d, ~o instead of the usual ~a, ~n, ~s, ~v, etc. ) besides the ones already baked into fprintf. Ones that suit the specifics of the application I'm writing.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [July 11, 2025, 1:03am UTC](https://racket.discourse.group/t/defining-new-special-formatting-escapes/3844/2 "2025-07-11T01:03:51Z")

</div>

If you need more than Racket's bare-bones `format`:

My [slib-format](https://docs.racket-lang.org/slib-format/index.html) library is a port of a [Common Lisp style `format`](https://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm)  
that includes `~u`, `~o`, `~d` and a ton more formatting specifiers :

```scheme
> (require slib/format)
> (printf "hex: ~x octal: ~o decimal: ~d roman: ~@r~%" 42 42 42 42)
hex: 2a octal: 52 decimal: 42 roman: XLII

```

There's also a [SRFI-48 Intermediate Format Strings](https://docs.racket-lang.org/srfi/srfi-48.html) library that comes with Racket that includes the usual different base numeric formats and some others.

And of course there's the core [`~r` function](https://docs.racket-lang.org/reference/strings.html#%2528def._%2528%2528lib._racket%252Fformat..rkt%2529._~7er%2529%2529) for formatting individual numbers.

---

<div class="post-metadata">

**Author:** ![notjack](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@notjack](https://racket.discourse.group/u/notjack)\
**Post date:** [July 11, 2025, 8:29pm UTC](https://racket.discourse.group/t/defining-new-special-formatting-escapes/3844/3 "2025-07-11T20:29:16Z")

</div>

What are you trying to do? I find that customizing the printed output is more than suitable for this, since you can stick any custom formatting you want on a wrapper struct.
