# How to add new escape sequence?

**URL:** <https://racket.discourse.group/t/how-to-add-new-escape-sequence/418>\
**Category:** Questions & Answers\
**Created:** [December 13, 2021, 7:34am UTC](https://racket.discourse.group/t/how-to-add-new-escape-sequence/418 "2021-12-13T07:34:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 13, 2021, 7:34am UTC](https://racket.discourse.group/t/how-to-add-new-escape-sequence/418/1 "2021-12-13T07:34:19Z")

</div>

If we `read` a string like `"\()"`, we will get

```scheme
read: unknown escape sequence `\(` in string

```

can we change this behavior?

---

<div class="post-metadata">

**Author:** ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)\
**Post date:** [December 13, 2021, 9:22am UTC](https://racket.discourse.group/t/how-to-add-new-escape-sequence/418/2 "2021-12-13T09:22:48Z")

</div>

I think you can use a custom readtable to make this possible, but so far I had no reason to do so, so I don't have practical details in that area, have a look at:  
[17.2.2 Readtables (Guide)](https://docs.racket-lang.org/guide/hash-reader.html#%28part._readtable%29)  
[13.7.1 Readtables (Reference)](https://docs.racket-lang.org/reference/readtables.html)  
[1.3.1 Delimiters and Dispatch (Default Read Table)](https://docs.racket-lang.org/reference/reader.html#%28part._default-readtable-dispatch%29)

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 13, 2021, 1:07pm UTC](https://racket.discourse.group/t/how-to-add-new-escape-sequence/418/3 "2021-12-13T13:07:36Z")

</div>

After some works, here is an example for escaping `"\$"` as `"$"` since `"$expr"` produces `(format "~a" expr)`

```scheme
(define (action c in src line col pos)
    (define (conv src in)
      (define args '())
      (define s
        (let loop ([l '()])
          (define c (peek-char in))
          (cond
            [(eof-object? c) l]
            [(char=? c #\") (read-char in) l]
            [(char=? c #\$) (read-char in)
                            (set! args (append args (list (read-syntax src in))))
                            (loop (append l (list #\~ #\a)))]
            [(char=? c #\\) (read-char in)
                            (cond
                              [(char=? (peek-char in) #\$)
                               (loop (append l (list (read-char in))))]
                              [else (loop (append l (list #\\ c)))])]
            [else (read-char in)
                  (loop (append l (list c)))])))
      (if (= (length args) 0)
          (list->string s)
          (with-syntax ([fmt (list->string s)]
                        [(arg ...) args])
            (syntax/loc (srcloc src line col pos #f)
              (format fmt arg ...)))))
    (conv src in))

```
