How to assign keyboard shortcut to a macro

My naive workaround now is to define commonly used ones with shortcuts
("d x" (insert '(concat "d" (hspace "0pt") "x")))
This has a shortcoming of redundant \hspace in the LaTeX export.

This is not the right way to do it. If you prescribe the rendering then you loose the meaning and the LaTeX translator cannot read in your mind about what you really want. Your markup should prescribe the meaning and then you need to use macro expansion to set up the rendering, the LaTeX converter will see a macro (I believe) and can be instructed to translate it in predefined ways.

Do you mean we can define macro in one way but export it in a different way?

Yes, please look at the manual for conversion to other formats:

2 Likes

There is a disadvantage of specific, or more precisely, that of the TeXmacs GUI: it seems to only support single-line codes. It would be better if it also applies to blocks.

See this bug report: https://savannah.gnu.org/bugs/?62800

When you apply the specific macros to export the LaTeX macro definitions you want in the exported LaTeX preamble, you also need to unselect “Expand TeXmacs macros with no LaTeX equivalent” and “Expand user-defined macros” in the TeXmacs preferences.