I am using TexStudio 2.6.6 and what I need is to create a custom keyboard shortcut macro that will insert _{%|} whenever I type _ in math code only. Or a macro that will insert ^{%|} whenever I type ^ in math code. But these macros should not work in text mode. So that in text mode I can type _.

So I needed to know how to tell the macro to activate only when I am writing math code.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Feb 6, 2014 at 5:38

1 Answer 1


In the TexStudio user mannual I have found the answer of the question. I am sharing it with the hope that it will help others.

  1. Add a New Macro
  2. Give some name to the macro
  3. In Trigger write (?highlighted-as:numbers,math-delimiter,math-keyword)_
  4. Type is Normal
  5. Latex Content is _{%|}

Although replacing ^ with ^{%|} is little bit more tricky, because ^ has special meanings in a regular expression. For that change the third bullet above by

  • In Trigger write (?highlighted-as:numbers,math-delimiter,math-keyword)\^

And change fifth bullet above by

  • Latex Content is ^{%|}

The only drawback of this method is that the macro will also activated by pressing the trigger if the cursor is touching any math code even if it is outside the math code. For example: A$ some math code $B here you can trigger the macro even if the cursor is in between A and $ or in between B and $ because $ is a math code and the cursor is touching $. However, if the cursor is not touching any math code the macro will not be executed by trigger.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .