Extracting environments to another buffer
February 15th, 2014

Note. It is best to read the original TeXmacs file with interactive Scheme sessions where one may experiment with the code. It can be found in the project's source code, inside the directory web/miguel.

We'd like to extract the following content to different files, one with the question another with the answer:

Question 1. Let be measurable with and for some . Show that

for all .

Answer. Notice that if , and if . Therefore, for all we have no matter what is. This leads to the rough estimate . We may improve this with…

First we define a helper routine to create the contents of the new buffers:

Scheme]

(define (new-document from doc)

"Returns a document with the same style as the given one"

(with l (select (buffer-get from) '(:* style 0))

(with tup (if (nnull? l) (tree->stree (car l)) '(tuple "generic"))

‘(document

(TeXmacs ,(texmacs-version))

(style ,tup)

(body ,doc)))))

Now, the routine which will actually extract the contents. Notice how we use the selector :* to indicate that we want subtrees at any depth in the document.

Scheme]

(define (pick-env env from to)

"Selects all subtrees of type @env in @from and sets @to's contents"

(with l (select (buffer-get from) ‘(:* ,env))

(with doc (if (nnull? l)

(append '(document) l)

'(document "No subtrees found of the type given"))

(buffer-set to (new-document from doc)))))

Here are two examples of how we may use the preceding stuff. A more general solution, maybe with a nice dialog box would be better.

Scheme]

(define (pick-questions from)

"Creates a new buffer with all questions from a given one"

(with u (new-buffer)

(pick-env 'question from u)

(switch-to-buffer u)))

Scheme]

(define (pick-answers from)

"Creates a new buffer with all cuestions"

(with u (new-buffer)

(pick-env 'answer from u)

(switch-to-buffer u)))

Scheme]

(pick-questions (current-buffer))

Scheme]

(pick-answers (current-buffer))

And finally here's an example inserting those two commands in a new menu ToolsExtract:

Scheme]

(menu-bind extract-menu

("Questions" (choose-file pick-questions

"Choose questions file" "texmacs"))

("Answers" (choose-file pick-answers

"Choose answers file" "texmacs")))

Scheme]

(menu-bind tools-menu

(-> "Extract" (link extract-menu))

(former))