TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When using the math-insert LyX function to create an element that can contains other elements, the caret ends up inside that cell, and if some text is selected when the function is called, that text will also get inside that cell.

Now, this is usually a blessed behavior, but there are some occasions when I want it to behave like a math-insert command that inserts an element that can't contain other elements - that is, I want the inserted TeX code to replace - not contain - whatever is selected, and the caret to end up after the the element, not inside it.

For example, when I want to bind some keys to math-insert \mathbb{R} - a command that adds the symbol of the real numbers field. When I run this command, I don't want my caret to get inside the mathbb element, and I don't want anything selected to get inside either - I just want to add the real numbers field symbol, the same way I add a Greek letter.

So, is there any other function that can help me, or an argument I can send to math-insert, or anything else to solve that problem?

share|improve this question
up vote 4 down vote accepted

This is perhaps not very elegant, but you can use the following function for the shortcut:

command-sequence math-insert \mathbb{R}; char-forward; char-forward;

math-insert \mathbb{R} inserts a math inset with an ℝ, and the cursor to the left of the ℝ. The first char-forward moves the cursor to the right of ℝ, but still within the \mathbb{}. The second char-forward moves the cursor outside the \mathbb{}, but it is still within the math inset. Adding a third char-forward will move the cursor outside the math inset.

share|improve this answer
Our Lyx-Guru ;-( – Marco Daniel Jan 3 '12 at 19:32
Thanks! Two char-forwards work for me, since I want the cursor to remain in the math area. Is it possible to emulate pressing the spacebar? If so, it should work much better, as a single press on the spacebar takes you directly out of the inset, so I can put longer things in there and won't have to count how many char-forward I need for each command... Also, is there a way to delete the selected text(if text is selected. If no text is selected I don't want nothing to be deleted)? If so, this could solve my second problem. – Idan Arye Jan 4 '12 at 21:13
@IdanArye Ah, see I misread the question a little (a common occurrence ...). No idea if one can emulate pressing space, I stumbled upon char-forward in this .bind-file on the LyX wiki. As for the last question, that is the default behaviour here (LyX 2) -- if I select some text and then press the keyboard shortcut, the selected text is replaced with ℝ. – Torbjørn T. Jan 4 '12 at 21:42
@Torb that might be the default behavior if you select normal text and run the command, but if you select part of an equation and run the command, that part gets inside the \mathbb – Idan Arye Jan 4 '12 at 23:47
@IdanArye Yes, I see. Sorry. No idea I'm afraid, haven't found any "replace" functions. – Torbjørn T. Jan 5 '12 at 7:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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