2

I'm using LyX, and I've set quite a few custom macros which contain arguments. For example for a 3x3 matrix I use \Mthree{...} where the ... stands for the 9 arguments for each matrix element.

But in default LyX positions the cursor right after the macro, and I need to go back and position the cursor on the first argument.

This is pretty inconvenient and not at all natural. Is there a way to make Lyx position the cursor on the first argument of the macro?

EDIT: Indeed G.M.'s answer works, but I finally realized where I was incorrect in phrasing the question, as I was already using the suggested typing method. What I meant to ask is not how to finish typing a macro, but how to make LyX position the cursor on the first argument when the macro is given a keyboard shortcut! Because on a daily basis almost all of my macros have assigned shortcuts to make the writing more fluent. An it is then when the cursor automatically gets positioned after the macro and not within.

My guess is that it can't be done and needs to be implemented in LyX, but I wonder if anyone is familiar with a way to do it.

2
2

To finish entering a macro, type Space, or autocomplete it with Tab, e.g.:

  • \MthreeSpace
  • \MthrTab
0

You can do this using a command-sequence that inserts the macro followed by various key-presses. The straightforward way:

command-sequence math-insert \Mthree{}{}{}{}{}{}{}{}{}; repeat 9 char-left

inserts your macro followed by 9 left key presses. The more flexible version:

command-sequence math-insert \Mthree{}{}{}{}{}{}{}{}{}; char-left-select; char-right

should end in the first argument position no matter the number of arguments to your macro, abusing the fact that char-left-select (=Shift-left) selects the whole macro hence placing the cursor in front of it.

Note the {}:

For custom macros with at least one argument,

math-insert \foo

will absorb a possible selection as its first argument and any following symbols as further arguments. If there is no selection, it will absorb following symbols as arguments. On the other hand:

math-insert \foo{}{}

(in case of two arguments) will replace a possible selection, deleting it, but in turn not absorb following symbols.

None of the two options are ideal, but I prefer the second, so I added the {}.

For LyX-native commands like \root or \sqrt that take at least one argument,

math-insert \root

will take possibly selected elements as its first argument, but not absorb following symbols. It will also automatically place the cursor in the first argument, as you want for your macro.

I have not been able to completely recreate the native behaviour using command sequences. This:

command-sequence cut; math-insert \foo{}{}; repeat 2 char-left; paste

will take a selection into the first argument, and will not be executed when there is no selection. However you will lose the clipboard contents.

I have tried combining both versions using command-alternatives, but using command-sequence after command-alternatives does not seem to work.

A possible workaround would be to create two separate shortcuts for selection or no selection.

Alternative without macro

A better alternative might be to use a shortcut that enters the whole construct directly without using a macro. In your example, there are LyX commands for that:

math-matrix 3 3

for an array, or

math-ams-matrix 3 3 pmatrix

if you want a pmatrix or similar.

This avoids the issues with the arguments I described above and also makes your source code more compatible when transferred to other users or documents.

For other constructs that don't have dedicated LyX commands, you can again use command sequences. In your example, you can manually build the above like this:

command-sequence math-insert \array; repeat 2 tabular-feature append-row; repeat 2 tabular-feature append-column

or even:

command-sequence math-insert \begin{array}{ccc} \\ \\ \\ \end{array}; up

In the same way as pressing right when the cursor is left of a 3x3 array, after the math-insert part, the cursor ends up in the first field of the second row, so "up" places it in the first field of the first row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.