Home | | Help | Contribute | Plug-ins | About | Contact | Search
Extracting environments to another buffer
Team | Miguel's home
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:


(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"))


(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.


(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.


(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)))


(define (pick-answers from)

"Creates a new buffer with all cuestions"

(with u (new-buffer)

(pick-env 'answer from u)

(switch-to-buffer u)))


(pick-questions (current-buffer))


(pick-answers (current-buffer))

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


(menu-bind extract-menu

("Questions" (choose-file pick-questions

"Choose questions file" "texmacs"))

("Answers" (choose-file pick-answers

"Choose answers file" "texmacs")))


(menu-bind tools-menu

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


This webpage is part of GNU TeXmacs and the larger GNU project. Verbatim copying and distribution of it is permitted in any medium, provided this notice is preserved. For more information or questions, please contact Joris van der Hoeven.

Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA