Include a texmacs file in my master document and edit it directly

I want to include in my texmacs master document texmacs files in which I have exercises (file 1 with exercise 1, file 2 with exercise 2,…) that I would then like to modify in the master document.

My problem is that by using INCLUDE then file, it is not possible to directly modify the included file in my master document. The text included in the master document is in a blue rectangle and it is not editable.
I could do this in lyx. I think it must be possible in texmacs.
I would like some help from you.
Thank you so much.

Hi @bpascal40. I don’t think this is possible in TeXmacs.

@jeroen is correct, I guess. In general TeXmacs tries to keep all the data in a single file. Split up a document in multiple files it usually only useful if the typesetting speed is not great. Or if one would like to reuse different files. In your use-case you can keep the master document open and then edit the single exercises in they own window, separately. If you want to edit them all together then put them in a single file. What is the reason to keep them apart?

In fact, I would like to reuse several times either exercises (which I have in different files) or statements(properties, definitions…) that I often use and need to remind my students often.
I was thinking of initially using “includes” in a personalized menu which would have allowed me to fetch “small files” texmacs which contain a few sentences and possibly a few equations…
I know that it is possible to use several windows of texmacs with different files.

Do you expect that if you edit the “pasted-in” document, then also the original has to be updated, or are you ok just to have a way to put in your document some “snippets” which then can evolve independently of their “template”?

You have understood what I would like:
in effect, a way to put in my document some “snippets” which then can evolve independently of their “template”…

Ok, this seems easier to achieve, even if I do not think TeXmacs has already some functionality for it. Essentially one has to “paste in” some material from another document in a programmatic way. Should not be difficult to do via scheme scripting. What is not clear to me is how you want to organise these snippets. In my opinion having them in different files is not very ergonomic. Ideally one would like to keep them in a single file so that they can be easily edited, and then add some mechanism to “label” them. At this point should be easy for TeXmacs to read the document, find a requested snipped and copy it in the current document. Would that be ok for you?

What you are suggesting would suit me.
To me, that seems complicated to do.
I discovered scheme recently and I’m just beginning to understand the basics.

TeXmacs defines a lot of scheme functions to manipulate documents. Reading a document can be done in this way, it is then matter to find the right snippet there, this would depend very much how we choose to identify snippets in such a document. E.g. we can imagine a “snippet” environment which associate a label to a piece of document. It is then just matter to look for such environment and find the one with the right label. Looks fun to code. :slight_smile:

1 Like

Seems also nice for obtaining documents composed out of “random snippets”, maybe that is what I will do, but I will be slow.

Some hints to get things started. I defined a document like this



      [<arg|lab>] <hrule>


      dska dkasnkd nask dnas nd knas klndlasn kld naslk ndl asnd naslkndl nas


      There are infinitely many primes.

      <math|\<pi\>> is irrational.



which I called “”, then in a scheme session you can start to play with it. For example

(define snippets-url "/Users/mgubi/Desktop/")
(buffer-load snippets-url)

load the buffer from disk into TeXmacs’ memory. You can check that it is there:


Then you can convert it to a scheme expression by

(define t (tree->stree (buffer-get-body snippets-url)))

which gives the list

(document (hide-preamble (document (assign "snippet" (macro "lab" "body" (document (concat "[" (arg "lab") "] " (hrule)) (arg "body")))))) 
(snippet "snip1" (document (theorem (document "dska dkasnkd nask dnas nd knas klndlasn kld naslk ndl asnd naslkndl nas" (equation* (document (concat "x" (rsup "2") "+y" (rsup "2") "<leqslant>2"))))))) 
(snippet "snip2" (document (theorem (document "There are infinitely many primes.")))) 
(snippet "snip3" (document (theorem (document (concat (math "<pi>") " is irrational."))))) "")

Now all you have to do is to look for the snippet you want, get the scheme expression for it and then put in your current document.

To learn how to use TeXMacs scheme I find useful to go around in the $TEXMACS_PATH/progs directory and look for keywords like load-buffer, etc… and look how things are done there, after a while you discover useful functions and then you try to do your own cooking in a scheme shell.


As exercise: write a function extract-snippet which take the above list are returns the following list:

(("snip1" (document (theorem (document "dska dkasnkd nask dnas nd knas klndlasn kld naslk ndl asnd naslkndl nas" (equation* (document (concat "x" (rsup "2") "+y" (rsup "2") "<leqslant>2"))))))) ("snip2" (document (theorem (document "There are infinitely many primes.")))) ("snip3" (document (theorem (document (concat (math "<pi>") " is irrational."))))))

if we save it in a variable snip-table by

(define snip-table (extract-snippets t))

then you can reobtain the associated piece of document by

(stree->tree (cadr (assoc "snip1" snip-table)))

assoc is a standard scheme function which lookup for specific keys in an association table and stree->tree convert the scheme representation of a document into the “internal” tree representation. In a scheme shell the output of that command will be a correctly typeset snippet. A further step would be to construct a user interface to do all this automatically and insert the snippet at the current cursor point in the current document (i.e. not in the scheme shell).


thank you for the time spent answering me, I will try the codes you suggested and I may come back to you for additional help.

If these are your first experiences with Scheme, it may be that you will discover into Scheme a lot of fun!

I haven’t played around with this yet, but TeXmacs also supports editable user databases, as are used by the built-in bibliography. A ‘snippets’ database could be created to contain snippets to be pasted in.

This is what I’ve discovered so far:

;; create a database
(tm-define (snip-database) (user-database "snippets"))

(smart-table db-kind-table
  ("snippets" ("snippet")))

(smart-table db-format-table
   (and "label" "body")))

(tm-define snip-types-list
  (smart-ref db-kind-table "snippets"))

(tm-define (snip-load)
  (db-load-types snip-types-list))

(tm-define (snip-save t)
  (db-save-types t snip-types-list))

;; create an entry
(with-database (user-database "snippets")
    (with l `(("label" "snip1")
              ("body" "the snippet body")
      (when (null? (db-search l))
        (db-create-entry l))))

;; retrieve an entry
(with-database (user-database "snippets")
    (let* ((ids (db-search `(("label" "snip1"))))
           (get (lambda (id) (db-get-field-first id "body" ""))))
           (map get ids)
1 Like

the following code works for me.

(menu-bind texmacs-extra-menu
(=> “Mon-menu” (link premier-menu)))
(menu-bind premier-menu
(“Liste-noire” (begin (make-script-input* “scheme” “default”) (insert " (begin (buffer-load “/home/biz/Snippets/listes/”)(buffer-get-body “/home/biz/Snippets/listes/” ))") (alternate-toggle (cursor-tree))))

It’s a beginning. I did not understand everything in the last codes of the discussion.

The “blacklist” file is correctly placed in a “pliage” scheme session and then by pressing the SUPPR (or DEL) key twice in a row, I have the content of my “blacklist” file which appears well in the file fluent. Thus, the session “folding” scheme disappears and I can optionally modify the text or the equations included in the current file
This is exactly what I wanted at first.
I used your code snippets.
Also, I noticed that it can work several times in a row and then it can crash.
Can this be improved? I think…
Thank you for your return.