TexWorks: How to add a command to change the text color (using a script)

In this blog post, I will show how to add a script (commands) to TexWorks for adding a color to your Latex document. This is easy and can be used also for other types of commands.

1) In TexWorks, go to the menu Scripts and then choose Show Scripts Folder:

This will open the folder containing the scripts.

2) Open the subfolder Latex styles as we will add our new script to this folder:

3) Make a copy of the file toogleBold.js and call it toogleRed.js:

4) Edit the file toogleRed.js as follows and save it:

// TeXworksScript
// Title: Toggle Red
// Shortcut: Ctrl+Shift+G
// Description: Encloses the current selection in \textcolor{red}{}
// Author: based on toogleBold by Jonathan Kew
// Version: 0.3
// Date: 2010-01-09
// 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("\\textcolor{red}{", "}");

Here, what I have done is to make a new command that will automatically add \textcolor{red}{} arround some selected text when the user presses CTRL+SHIFT+G.

5) Go back to TexWorks, and reload the script list using the menu “Reload script list”:

Then, the new command will appear in the menu Latex Styles:

6) Then, you can try it by selecting some text in a latex document and then pressing CTRL+Shift+G:

That’s all!

And of course, to compile the LaTeX document, I assume that you are using the color package.

It is very convenient to make scripts for new commands in TexWorks!

And, If you want to do the same for the blue color, we could make another script like this:

// TeXworksScript
// Title: Toggle Blue
// Shortcut: Ctrl+Shift+D
// Description: Encloses the current selection in \textcolor{blue}{}
// Author: based on toogleBold by Jonathan Kew
// Version: 0.3
// Date: 2010-01-09
// 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("\\textcolor{blue}{", "}");
This entry was posted in Latex and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *