Creating Automatic Subscripting Using my-init-texmacs.scm

Hello fellow TeXmacsers, I am trying to make it so that in math mode, putting a number after a letter makes it so that the number becomes the subscript of the letter. I have this script here that I pasted into the my-init-texmacs.scm file and restarted the program, but it is not doing anything.


(tm-define (auto-subscript-digit key)
;; Only apply in math mode
(if (equal? (tree-label (current-env)) 'math)
(let ((prev (get-env-before-cursor 1)))
(if (and (not (null? prev))
(char-alphabetic? (tree->char (car prev))))
(begin
;; Delete the previous character (the letter)
(delete-env-before-cursor 1)
;; Insert subscripted version
(insert (make-tree 'sub
(car prev)
(string->tree key))))
(insert key)))
(insert key)))

;; Bind digits 0–9 to this behavior in math mode
(for-each (lambda (digit)
(bind-key (string-append " " digit)
(lambda () (auto-subscript-digit digit))))
'(“0” “1” “2” “3” “4” “5” “6” “7” “8” “9”))


Any help is appreciated comrades. I am new to modifying the program, though I’ve used it for a while. This is the only code in my my-init-texmacs.scm file, do I need something else?

Hi @faironway and welcome to the forum. I like the idea of binding the number keys to a function that makes them as subscript if they come immediately after a letter; the code at a very brief examination seems not to be TeXmacs code, containing several names that do not correspond to functions available in the Scheme interface, which I think is the reason why it does not work.

I think it is possible to get it to work with some modifications, but as a first step I would like to know where did you find out all of these (I think) not-existing-functions names, that is for example get-env-before-cursor, delete-env-before-cursor and so on (there are others too I think).

Thank you for pointing that out - I actually naively used ChatGPT to write a script, as I didn’t have time to look into scripting using the TeXmacs Scheme interface, but I will study up on this and come back in a week or so with (hopefully) a working prototype to share. I actually tried having it revise the script it generated by feeding it the official documentation and it got closer (at least used functions available to the TeXmacs interface), but the script was still not functioning correctly, as expected.