# Get redex traces result as picture?

**URL:** <https://racket.discourse.group/t/get-redex-traces-result-as-picture/458>\
**Category:** Questions & Answers\
**Created:** [December 21, 2021, 4:03am UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458 "2021-12-21T04:03:30Z")\
**Posts on this page:** 9\
**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 21, 2021, 4:03am UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/1 "2021-12-21T04:03:30Z")

</div>

[traces](https://docs.racket-lang.org/redex/reference.html#%28def._%28%28lib._redex%2Fgui..rkt%29._traces%29%29) function in redex given a nice GUI result, but what if I want to put it into scribble manual? How can I do that?

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [December 23, 2021, 10:02pm UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/2 "2021-12-23T22:02:28Z")

</div>

I'm not sure there is a very nice way, sadly. In order to get it into scribble, it would need to be a pict, but `traces` is operating at the `pasteboard%` layer and that doesn't convert to picts.

But just to be sure this is the right answer, are you looking for a particular layout of a graph, or just the layout of a particular term?

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [December 23, 2021, 10:47pm UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/3 "2021-12-23T22:47:57Z")

</div>

I think one possible approach is to use `traces/ps` to generate a Postscript file, then use an external tool to convert the Postscript file to a png, and then include this png in the doc.

There’s also [https://docs.racket-lang.org/scriblib/gui-eval.html](https://docs.racket-lang.org/scriblib/gui-eval.html), but I’m not sure if it can actually be used here.

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [December 23, 2021, 10:50pm UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/4 "2021-12-23T22:50:20Z")

</div>

An approach like that can be made to work, and now that you mention it we did something like that for the redex book.

One small tweak: if you go to PDF and then use the latex backend to drop the PDF in, you won't have to rasterize.

---

<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 24, 2021, 1:33am UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/5 "2021-12-24T01:33:51Z")

</div>

This is output from `trace`

 ![圖片](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/66ae8952e804ed592ce12aed6d16a5d16ad08525.png)  
This is output from `trace/ps`  
 ![圖片](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/3326a558cdaa0a2e0cb30f0042fa81240ef7ebdc.png)

then for gui-eval, I have tried but also cannot figure out how to use it XD

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [December 24, 2021, 4:04am UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/6 "2021-12-24T04:04:05Z")

</div>

The arrows don't show in the second one because the terms are too close together. Is that the difference you're asking about?

---

<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 24, 2021, 5:22am UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/7 "2021-12-24T05:22:36Z")

</div>

yes, the problem is I cannot get adjusted picture

---

<div class="post-metadata">

**Author:** ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)\
**Post date:** [December 24, 2021, 3:33pm UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/8 "2021-12-24T15:33:59Z")

</div>

This is code that does some layout for you. It isn't the prettiest layout but maybe it will get you started?

```scheme
#lang racket
(require redex)

(define-language L
  (e ::= (op e e) tt ff)
  (op ::= ∧ ∨)
  (C ::= (op hole e) (op e hole) hole))

(define-judgment-form L
  #:contract (--> e e)
  #:mode (--> I O)

  [------------------------- "∧ tt"
   (--> (in-hole C (∧ tt e))
        (in-hole C e))]

  [------------------------- "∧ ff"
   (--> (in-hole C (∧ ff e))
        (in-hole C ff))]

  [------------------------- "∨ tt"
   (--> (in-hole C (∨ tt e))
        (in-hole C tt))]

  [------------------------- "∨ ff"
   (--> (in-hole C (∨ ff e))
        (in-hole C e))])

(define (layout terms)
  (define root
    (for/or ([term (in-list terms)])
      (and (null? (term-node-parents term))
           term)))
  
  (define visited (make-hash))
  (let loop ([depth 0]
             [width 0]
             [node root])
    (cond
      [(hash-ref visited node #f) (void)]
      [else
       (hash-set! visited node #t)
       (term-node-set-position! node (* width 200) (* depth 100))
       (for ([child (in-list (term-node-children node))]
             [width (in-naturals)])
         (loop (+ depth 1)
               width
               child))])))

(traces -->
        (term (∨ (∧ tt ff) (∧ ff tt)))
        #:layout layout)

```

---

<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 26, 2021, 1:22pm UTC](https://racket.discourse.group/t/get-redex-traces-result-as-picture/458/9 "2021-12-26T13:22:07Z")

</div>

Another approach might be to use `traces` with a custom `#:graph-pasteboard-mixin` that uses all the incoming information to draw a pict. I am unsure how easy/difficult that is in practice, but if you get it to work it may be the most flexible in allowing you to change the output however you want it to be.

* * *

_The following is speculation and details may be wrong, because I would have to actually start to try to implement it, to see whether certain details need to be changed / adapted, so this is just a rough idea of a plan._

Looking at [1] I think this is the method which ends up rendering the graphics for the pasteboard.  
One neat thing about rackets [dc\<%\>](https://docs.racket-lang.org/draw/dc___.html) is that there are many different implementations, so what if we were to create a modified/derived version of the mixin in [1] that basically wraps the original mixin (to reuse all the drawing implementation details), but then does the following:

1. the new mixin is parameterized/constructed with a `bitmap-dc%` or maybe even a `record-dc%`
2. `on-paint` is overridden to call the original mixins implementation with the custom dc
3. convert the custom dc to a pict via:
  - bitmap-dc%: [bitmap pict constructor](https://docs.racket-lang.org/pict/Basic_Pict_Constructors.html#%28def._%28%28lib._pict%2Fmain..rkt%29._bitmap%29%29) + bitmap that was used to construct the bitmap-dc
  - record-dc%: [dc pict constructor](https://docs.racket-lang.org/pict/Basic_Pict_Constructors.html#%28def._%28%28lib._pict%2Fmain..rkt%29._dc%29%29) + [get-recorded-procedure](https://docs.racket-lang.org/draw/record-dc_.html#%28meth._%28%28%28lib._racket%2Fdraw..rkt%29._record-dc~25%29._get-recorded-procedure%29%29)

[1] [gui/graph.rkt at d1fef7a43a482c0fdd5672be9a6e713f16d8be5c · racket/gui · GitHub](https://github.com/racket/gui/blob/d1fef7a43a482c0fdd5672be9a6e713f16d8be5c/gui-lib/mrlib/private/graph.rkt#L454)
