Custom keymaps in math mode

I’m a big fan of using simple keymaps for commands that I use frequently. I would like to add a custom keymaps to type maths quickly. By default, to type a vector, the keyboard short cut is Ctrl+Alt+v. I would like to map that to vv, and this should only be triggered in math mode.

I was looking at the documentation and tried something like this:

(kbd-map
    (:mode in-math?)
    ("v v" (insert "\\vec"))
)

This got triggered inside math mode, but typed the literal text \vec instead of considering it as LaTex. How do I get it to consider it as LaTex and not text?

To enter LaTeX-style commands you have to invoke the Scheme function make-hybrid:

(kbd-map (:mode in-math?) ("v v" (begin (make-hybrid) (insert "vect")) ))

(vect not vec)

In this case you still need to press return—I do not know how to send a return with Scheme code.
You can also do

(kbd-map (:mode in-math?) ("v v" (make-wide "<vect>")  ))

—I got this from /progs/texmacs/keyboard/latex-kbd.scm in the TeXmacs github account—
and you get the slot where you can type the vector’s name.

1 Like

Thanks for pointing me to the source. Yes, it would be nice if I don’t also have to type ENTER.

I wonder if you wouldn’t mind if I take this opportunity to ask other related questions. I used to create my LaTex documents in vim and used a snippet plugin called ultisnips. You could create all sorts of powerful keymaps very easily (and also use Python if you want to go wild). I like texmacs because it allows you to see the equations as you type and I find that much more gratifying than seeing LaTex syntax. I am hoping to configure texmacs in a similar way that makes typing maths faster.

Consider the code snippet below:

(kbd-map
    ("c p" (make 'python-code))
)

Typing this puts me in a python-code block. This code is triggered as soon as I type cp. How do I make this mapping only trigger if it is at the beginning of a line or at the start of a word boundary. That is, it shouldn’t trigger if it is part of a word like macpaint?

How do I make keyboard shortcuts for things like \int\limits? This creates an integral where the limits are placed over and below instead of slightly to the right?

Maybe there is a way to insert complex conditionals into the definition, but I have to investigate more for that and I postpone. For now, here is an easy way to do it that might work:

(kbd-map ("c p space" (make 'python-code)) )

I tried also

(kbd-map ("space c p space" (make 'python-code)) )

but in this case I obtain the typeset string “spacec” if I press “space c” and then I do not complete with “p” (maybe it is a bug).

Of course one can also use modifier keys (example "C-c C-p"); you have to make sure you do not overwrite other keyboard shortcuts (I need to figure out how to get a list of keyboard shortcuts).

For your second question

try

(kbd-map ("i l i m" (begin  (make-above)  (make-below)  (math-big-operator "int") )))

where I got (make-above) and (make-below) from TeXmacs/progs/math/math-menu.scm

I think that it is possible to insert placeholder letters for the limits as well, but I did not yet understand how to do it (I think one needs to apply commands for the cursor movement).

In /progs/math/math-kbd.scm I found also

("math a" (make-above))

but I did not figure out what keys "math a" corresponds to.

Thanks for the effort. I appreciate it. I just wish the documentation was more thorough.

Me too :slight_smile:

Perhaps with a bit of time I will be able to write some small tutorial-style guides. So far I have contributed a bit to https://texmacs.github.io/notes/docs/main.html. I have to pick topics on which to write though, choosing the ones where I know enough to write something reasonably complete.

Having used texmacs for short time, I am really loving it and have even recommended it to some of my friends. I think a couple of well-structured youtube videos that highlight the essential parts and goes into some details into how one might customize it would be ideal for newcomers.

One last question if you haven’t yet gone to bed. Is it possible to have equation templates ? Here’s what I mean. There are some equations that I type a lot, such as the Taylor series. So, in vim, using ultisnips, I would have something like this:

snippet taylor "taylor" w
\sum_{${1:k}=${2:0}}^{${3:\infty}} ${4:c_$1} (x-a)^$1 $0
endsnippet

This types Taylor series using LaTex. The ${number:default_value} is what you use to jump (when you press TAB) between parts of the equation that you want to change. For example, ${2:0} is the second tab stop and the default value is 0 (which you can change if you want).

A beginning of an answer.

(kbd-map ("A-p 1"
          (begin
          (insert-go-to 
           `(concat "test " (snipPiece "a") " test " (snipPiece "b")) 
           '(1 0 0))
          )))

,together with

<assign|snipPiece|<macro|body|<arg|body>>>

in the preamble, inserts an expression which contains text placed into several instances of the tag snipPiece and sets the cursor inside the first snipPiece tag. You can then use the standard TeXmacs structured movement arrows (“go to next similar tag” and “go to previous similar tag”) to move between tagged texts.

I think that it is possible to refine the code till it has the same functionality of the ultisnips snippet that you posted. One would need to find programmatically the first instance of snipPiece to place the cursor there (there is a Scheme function tree-search, I did not yet understand how to apply it only to the tree that I am inserting) and one could also set cursor positions using position-set (it is mentioned in the Scheme developer manual without examples, I did not yet try it) so that one could cycle through the snipPiece tags—one also would need to keep track of the snippet one is in, perhaps it is possible with a tree of snippets.

There are videos linked from the TeXmacs webpage, but I never watched them :slight_smile:
As far as I know they do not cover the customization of the editor.

Also, the manual is fairly complete; Help->Manual and F1 to search in the integrated documentation and look also at http://texmacs.org/tmweb/help/learn.en.html

There is also a web version of the manual (e.g. https://www.texmacs.org/tmweb/manual/webman-links.en.html) but its pages are not linked to from the texmacs.org website; https://www.texmacs.org/tmweb/manual/webman.html, tested today, does not link to anything, neither does https://www.texmacs.org/tmweb/manual/webman.en.html

On the other hand, I find the documentation for the more sophisticated topics (e.g. how to control the editor, how to control the document) to be written in a synthetic way so that it is challenging at a first read. In general, too, the discoverability of the documentation is low.

1 Like