# Usage of code-examples in scribble

**URL:** <https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628>\
**Category:** Questions & Answers\
**Tags:** scribble\
**Created:** [January 8, 2023, 10:25pm UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628 "2023-01-08T22:25:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Panadestein](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/panadestein/32/949_2.png) [@Panadestein](https://racket.discourse.group/u/Panadestein)\
**Post date:** [January 8, 2023, 10:25pm UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628/1 "2023-01-08T22:25:15Z")

</div>

Hello everyone,

I have been recently working on SICP with Chez Scheme. I was using Emacs with org-mode to organize my solutions using literate programming, and then exporting them to HTML. Then I read about Racket, DrRacket, scribble, which I think is really awesome software. So my goal is to have a similar workflow in DrRacket + scribble, as that of Emacs + org-mode.

Here is a simple example that doesn't quite work:

```scheme
#lang scribble/base

@(require scribble/manual
          scribble-code-examples)

A minimal example, I would like to use the A procedure in the code-example
block.
 
@racketblock[
   (define (A x y)
    (cond ((= y 0) 0)
          ((= x 0) (* 2 y))
          ((= y 1) 2)
          (else (A (- x 1) (A x (- y 1))))))
 ]

@code-examples[#:lang "sicp" #:context #'here]|{
  (A 1 10)
}|

```

which gives the error:

```scheme
racket-8.7/share/racket/collects/racket/private/more-scheme.rkt:148:2: A: undefined; cannot reference an identifier before its definition

```

How can this code be fixed?

Also, is there any way in which I could evaluate a particular (or several) S-expression when writing a scribble document? For instance, testing the `A` procedure without creating a new file or tab?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [January 8, 2023, 10:51pm UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628/2 "2023-01-08T22:51:35Z")

</div>

Sounds like you are attempting to something similar to what Jay did in his blog posts.

Here is an example:  
[2013-06-03: Towers of Hanoi in Racket](https://jeapostrophe.github.io/2013-06-03-hanoi-post.html)

And here is the source used to produce that blog post:  
[jeapostrophe.github.com/2013-06-03-hanoi.rkt at source · jeapostrophe/jeapostrophe.github.com · GitHub](https://github.com/jeapostrophe/jeapostrophe.github.com/blob/source/posts/2013-06-03-hanoi.rkt)

It uses `scribble/lp` where `lp` is short for literate programming.

---

<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:** [January 8, 2023, 11:09pm UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628/3 "2023-01-08T23:09:48Z")

</div>

This works for me:

```scheme
#lang scribble/manual

@(require scribble/examples)

A minimal example, I would like to use the A procedure in the example block.

@(define my-eval
   (make-base-eval #:lang 'sicp))

@examples[#:eval my-eval #:hidden
  (define (A x y)
    (cond ((= y 0) 0)
          ((= x 0) (* 2 y))
          ((= y 1) 2)
          (else (A (- x 1) (A x (- y 1))))))
]

@examples[#:eval my-eval
  (A 1 10)
  inc ;; test that sicp is loaded
]

```

Roughly, for the evaluations that you want them to be displayed, use `(examples #:eval <evaluator> <code> ...)`. For the evaluations that you don’t want them to be displayed, use `(examples #:eval <evaluator> #:hidden <code> ...)`. The evaluator specified in these `examples` should be the same so that the state gets carried over.

Note that I use `scribble/examples`, which is from core Scribble. I don’t think you _need_ to use `scribble-code-examples`, as that package's intended use-case is for showing non S-exp syntax examples.

---

<div class="post-metadata">

**Author:** ![Panadestein](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/panadestein/32/949_2.png) [@Panadestein](https://racket.discourse.group/u/Panadestein)\
**Post date:** [January 8, 2023, 11:28pm UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628/4 "2023-01-08T23:28:54Z")

</div>

Thanks a lot for your reply! That is what I wanted. Quick question, how can I remove (or modify) the "Example:" header that `@example` generates?

---

<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:** [January 9, 2023, 12:13am UTC](https://racket.discourse.group/t/usage-of-code-examples-in-scribble/1628/5 "2023-01-09T00:13:56Z")

</div>

Specify `#:label #f` after `#:eval <evaluator>`. The full documentation of `examples` is [here](https://docs.racket-lang.org/scribble/eval.html).
