5

I would like to write a macro with a trigger #[a-z] that, dependent on the character in the trigger, writes the corresponding Greek letter. That is, the trigger #a should produce \alpha, the trigger #b should give \beta in the editor, and so on.

The trouble is that the trigger is removed immediately after it is written so that the script does not know if a, b, or another letter has been used. Of course I could write specific macros for each Greek letter, but this would exhaust even the limit of 40 macros easily (if also capital letters are used). I know I could place the macro under the first ten macros and use the shortcut Shift+F1 or the like, but this is less intuitive.

Does anyone know of a method to access the trigger letter?

Edit: I now use a workaround. If the order of trigger and letter is reversed, the problem disappears. Instead of #a, the trigger is now universally #. Writing a#will remove the # and leave the a which can then be used in the script to be converted to \alpha. However, this cramps my typewriting style somewhat so that I would still like to know the answer to the question.

1
  • Welcome to tex.sx! It’s great that you’re sharing your solution here, and it would be even better if you posted it as (another) answer instead of leaving it in the question. We like to keep problems and solutions separate like that. You can have a look at our starter guide to familiarize yourself further with our format.
    – doncherry
    Jun 22, 2014 at 10:39

3 Answers 3

4

There is a global variable triggerMatches. It's the result of matching the regexp you defined as trigger.

triggerMatches is an array. It's zero-th component is the match to the complete regexp. The following elements are matches to groups (if groups are defined).

Example:

Trigger: #([a-z])

Typed: #a

triggerMatches[0] == '#a'
triggerMatches[1] == 'a'
3
  • That is exactly what I was looking for! Works perfectly. Would it be possible to include this and similar info in the Texstudio user guide at some future time?
    – Wolf
    Jun 21, 2014 at 22:56
  • triggerMatches is already briefly mentioned in the command list. I've now added the above explaination to the docs (will be available in the next release). Jun 22, 2014 at 22:33
  • I've posted the short script in the answer below - feel free to include it somewhere if you find it useful (though I think it is quite easy once triggerMatches is known).
    – Wolf
    Jun 24, 2014 at 18:43
3

I thought I might include the tested script for other people who might find it useful. I decided on using triggerMatches[0] so that the script works for ungrouped triggers, too:

%SCRIPT
var strTrigger = 'abg';
var greek = new Array();
greek[0]  = 'alpha';
greek[1]  = 'beta';
greek[2]  = 'gamma';
idx = triggerMatches[0];
var pos = strTrigger.search(idx.slice(idx.length-1,idx.length));
if (pos != -1) {
  editor.write('\\ensuremath{\\'+greek[pos]+'}')};

Of course, the array and the string can be extended to include other greek letters.

0

This is my full Greek macro based on the Wolf's answer.

Trigger: `([A-z])

%SCRIPT
var strTrigger = 'abgdezhqiklmnyprstfxwoABGDQLYWO';
var greek = new Array();
greek[0]  = 'alpha';greek[1]  = 'beta';greek[2]  = 'gamma';
greek[3]  = 'delta';greek[4]  = 'varepsilon';greek[5]  = 'zeta';
greek[6]  = 'eta';greek[7]  = 'theta';greek[8]  = 'iota';
greek[9]  = 'varkappa';greek[10]  = 'lambda';greek[11]  = 'mu';
greek[12]  = 'nu';greek[13]  = 'xi';greek[14]  = 'pi';
greek[15]  = 'rho';greek[16]  = 'sigma';greek[17]  = 'tau';
greek[18]  = 'phi';greek[19]  = 'chi';greek[20]  = 'psi';
greek[21]  = 'omega';greek[22]  = 'Alpha';greek[23]  = 'Beta';
greek[24]  = 'Gamma';greek[25]  = 'Delta';greek[26]  = 'Theta';
greek[27]  = 'Lambda';greek[28]  = 'Xi';greek[29]  = 'Psi';
greek[30]  = 'Omega';
idx = triggerMatches[0];
var pos = strTrigger.search(idx.slice(idx.length-1,idx.length));
if (pos != -1) {
editor.write('\\'+greek[pos])};

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.