# TeXworks script for inserting inline math mode

I've been trying to set up a TeXworks script that would add the in-line math mode symbol '$' around a selection. E.g., if one selected '\omega' and executed the script, the result would be '$\omega$'. I unashamedly based myself on the two scripts that have been built in TeXworks. One finds these under 'Scripts/Latex styles'. Using a Text Editor under Mac OS X 10.10.5, I came up with the script below. The last line is the one that calls the function that would add two '$' to the selected text.

Following the TeXworks manual, I've stored it in the proper script directory and had TeXworks re-read the script list. Lo and behold, my script shows up in the Menu, with the proper key combo. But, alas, nothing happens. TeXworks acknowledges the key combination from the keyboard but doesn't do a thing to the selected text.

Any suggestion would be highly appreciated. Thank you.

// TeXworksScript
// Title: Toggle Math Mode
// Shortcut: Ctrl+Shift+M
// Description: Encloses the current selection in $$(...) // Script-Type: standalone // Context: TeXDocument function addOrRemove(prefix, suffix) { var txt = TW.target.selection; var len = txt.length; var wrapped = prefix + txt + suffix; var pos = TW.target.selectionStart; if (pos >= prefix.length) { TW.target.selectRange(pos - prefix.length, wrapped.length); if (TW.target.selection == wrapped) { TW.target.insertText(txt); TW.target.selectRange(pos - prefix.length, len); return; } TW.target.selectRange(pos, len); } TW.target.insertText(wrapped); TW.target.selectRange(pos + prefix.length, len); return; }; addOrRemove(“”, “”);  ## 1 Answer There are two mistakes in your script: 1. The (...) in the header has to be removed or commented with //. 2. The quotation marks in the function call are ” while they should be ". So this script works: // TeXworksScript // Title: Toggle Math Mode // Shortcut: Ctrl+Shift+M // Description: Encloses the current selection in$$
// (...)
// Script-Type: standalone
// Context: TeXDocument

var txt = TW.target.selection;
var len = txt.length;
var wrapped = prefix + txt + suffix;
var pos = TW.target.selectionStart;
if (pos >= prefix.length) {
TW.target.selectRange(pos - prefix.length, wrapped.length);
if (TW.target.selection == wrapped) {
TW.target.insertText(txt);
TW.target.selectRange(pos - prefix.length, len);
return;
}
TW.target.selectRange(pos, len);
}
TW.target.insertText(wrapped);
TW.target.selectRange(pos + prefix.length, len);
return;
};

addOrRemove("$", "$");

• thanks for picking up the quotation marks problem! Now, as crazy as it may sound, I tried using two single quotes at the beginning and end of each of the two strings and it didn't work. Tried  and '' (i.e. 4 strokes) and it didn't work either. I had to cut/paste (!) the quotes from one of the scritps within TeXworks in order to have it work (!). I thought the problem was my using the US-International keyboard layout w/ my MacBook Air; I added the US keyboard layout (which is essentially the same) and typing ' twice or the ` and ' combo didn't work either! Amazing. Thank you. – user49069 Jan 20 at 19:30
• @user49069 Perhaps there is some auto-replacing set up in your editor. TeXworks for example has something called Smart Quotes, found under the Format-menu. – Torbjørn T. Jan 20 at 19:38
• Good point. Smart Quotes has been set to None from the beginning though. Under Preferences/Editor however, it was set to 'TeX Ligatures', which I suspect would do the trick from now on... (By the way, don't know what happened to '@Torbjørn T.' at the start of my previous comment, sorry.) Thanks. – user49069 Jan 20 at 19:49
• I meant, I changed Smart Quotes to None under Preferences... – user49069 Jan 20 at 19:50
• @user49069 The owner of a question/answer is always notified of comments to that post, so tagging me isn't necessary here. Might be that it's removed automatically. – Torbjørn T. Jan 20 at 19:53