Generating Scheme graphics with external files

Some month ago, I managed to follow the guides on generating scheme graphics by putting the code into a Scheme session in TeXmacs, but the following code no longer gives me any output:

(define pi (acos -1))

(define (pt x y)
  '(point ,(number->string x) ,(number->string y)))

(define pA (pt -2 0))
(define pB (pt 2 0))
(define pC (pt xC yC))

(stree->tree
 '(with "gr-geometry" 
     (tuple "geometry" "400px" "300px" "center")
     (graphics
        (with "color" "red"   (cline ,pA ,pB ,pC)))))

Anyway, https://texmacs.github.io/notes/docs/modular-scheme-graphics.html says

we also plan to write a post on how to generate Scheme graphics with external files.

This would help me a lot, but I’m afraid no one has found time for this so far.
It would be great if someone could quickly sketch the necessary steps here.

Let me first post the working code

(begin
  (define pi (acos -1))

(define (pt x y)
  `(point ,(number->string x) ,(number->string y)))

(define pA (pt -2 0))
(define pB (pt 2 0))
(define xC (- (* 2 (cos (/ pi 3))))); x-coordinate for point C
(define yC (* 2 (sin (/ pi 3)))); y-coordinate for point C
(define pC (pt xC yC))

(stree->tree
 `(with "gr-geometry"
     (tuple "geometry" "400px" "300px" "center")
     (graphics
        (with "color" "red" (cline ,pA ,pB ,pC))))))

In place of the quotes ' you need the backquotes

`

Quotes in Scheme direct the program to return the quoted code literally. Backquoted code, on the other hand, directs the program to interpret any part that is preceded by commas, and to return literally the rest.
In the code you posted Scheme needs to do some calculations (calculate the value of pA for example) so you need backquotes.

Let me return later to the Scheme graphics with external files (I wrote the posts on Scheme graphics, then never completed the series).

1 Like

Thanks a lot!
Stupid me, copying the code from the articles messed up the backquotes.

Please try “Paste from -> Verbatim”, it might preserve all of the characters.

1 Like

Thanks, I hadn’t seen this option so far!

Here is a short discussion of Scheme graphics with external files. It is also incomplete. I’ll explain at the end.

  • Make a Scheme file with graphics functions. The file content may be this (as an example)
(texmacs-module (graphics-functions))

(tm-define pi (acos -1))

(tm-define (pt x y)
  `(point ,(number->string x) ,(number->string y)))
  • Put it in the progs directory of your personal initialization directory, call it graphics-functions.scm
  • In a Scheme executable switch, write
(begin

(use-modules (graphics-functions))

(define pA (pt -2 0))
(define pB (pt 2 0))
(define xC (- (* 2 (cos (/ pi 3))))); x-coordinate for point C
(define yC (* 2 (sin (/ pi 3)))); y-coordinate for point C
(define pC (pt xC yC))

(stree->tree
 `(with "gr-geometry"
     (tuple "geometry" "400px" "300px" "center")
     (graphics
        (with "color" "red" (cline ,pA ,pB ,pC))))))

and you will be using the pi and pt functions you defined in the module.

In the code:

  • (use-modules (graphics-functions)) tells Guile that it has to load the module, which is located in its search path
  • The tm-defines inside the module will make the corresponding definitions globally available

It is also possible to link to the module in the preamble, with

<use-module|(graphics-functions)>

and you will not need the (use-modules (graphics-functions)) form in the executable switch.

I hope this helps.

I did not finish the post on graphics with external code because (the explanations tell you also where the present post is incomplete in my opinion)

  1. I wanted to be exhaustive in explaining where files can be put, and I got into a tangle
  2. With external files, you might not know what functions you are executing. Scheme with full access to your computer’s resources can wipe them out ;-). I did not yet write about security in a way that satisfies me.

I think it is still possible for me to finish it but it might require a bit of sustained work. It would be also good to make it plain why one would like to use the function denestify-conditional: as I myself, that wrote it, do not see now the details of how it helps. I don’t like the name of the function anymore, too :wink:

1 Like

Thanks a lot for the helpful hints @pireddag.

The combination of programmatically generating graphics, which can then in turn be changed visually inside a GUI is a quite unique feature of TeXmacs with plenty of use cases.

Initially, I was struggling a bit with Scheme, but the Racket language and the 2htdp/image library of the excellent How to design programs textbook inside the DrRacket IDE provided an easy entry towards the topics for me.

1 Like