3

I'm trying to do the following:

LaTeX -> (text selected, keyboard shortcut pressed) -> \hyperref[LaTeX]{LaTeX}

I can't use user tags for this because it can't use selection twice, only once. I made this script by editing one of the simple, provided scripts for TeXWorks, which turns normal text into bolded text.

toggleHyperref.js

// TeXworksScript
// Title: Toggle Hyperlink
// Shortcut: Ctrl+Shift+H
// Description: Converts the current selection to be a hyperlink.
// Author: Robo
// Version: 0.1
// Date: 2018-01-06
// Script-Type: standalone
// Context: TeXDocument

function addOrRemove(prefix, midfix, suffix) {
  var txt = TW.target.selection;
  var len = txt.length;
  var wrapped = prefix + txt + midfix + 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("\\hyperref[", "]{", "}");

For reference, this is what a .tms script (provided by TeXMaker) looks like:

hardwordwrap_selection_80col.tms

/***************************************************************************
 *   copyright       : (C) 2003-2014 by Pascal Brachet                     *
 *   http://www.xm1math.net/texmaker/                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

function wordwrap( str, width, brk, cut ) {

    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

    return str.match( RegExp(regex, 'g') ).join( brk );

}

var textwidth=80;

var txt = TM.selection;
if (txt != "") {
  var pos = TM.selectionStart;
  var wraptext="";
  var lines = txt.split('\n');

  for (i = 0; i < lines.length; ++i) {
  var line = lines[i];
  wraptext+=wordwrap(line, textwidth, '\n')+"\n";
  }
  TM.insertText(wraptext);
  TM.selectRange(pos, wraptext.length);
}

undefined;

Thanks!

2
  • Nice first question. Welcome.
    – A Feldman
    Jan 13, 2018 at 4:03
  • Thanks! I just love toying with LaTeX. And funnily enough, I'm gonna have my first answer too, to my own question, since I figured it out.
    – Robo
    Jan 13, 2018 at 4:10

1 Answer 1

4

The answer to my question is here: http://www.xm1math.net/texmaker/doc.html#SECTION45

Specifically, this:

Remarks:
to use TexWorks scripts, the TW.target prefix must be replaced by TM.

I misread this line when checking the documentation. To fix it I replaced TW with TM. Now that I followed the instructions properly, it works great.

This is the code of my new, working script:

toggleHyperref.tms

// TeXworksScript
// Title: Toggle Hyperlink
// Shortcut: Ctrl+Shift+H
// Description: Converts the current selection to be a hyperlink.
// Author: Robo
// Version: 0.1
// Date: 2018-01-06
// Script-Type: standalone
// Context: TeXDocument

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

addOrRemove("\\hyperref[", "]{", "}");

You must log in to answer this question.

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