3

I try to write a script for the TexWorks editor. I want to be able to convert easy writeable physical units into the syntax of the SI-Unit latex package.

For example:

I write: 12,45A

mark it and hit my shortcut to convert it (by script) to

\SI{12.45}{\ampere}

I managed to put the following script together:

// TeXworksScript
// Title: convert SI
// Shortcut: Ctrl+Shift+K
// Description: Converts into SI 
// (...)
// Script-Type: standalone
// Context: TeXDocument



function addOrRemove(prefixA, suffixA, prefixB, suffixB) {
  var txt = TW.target.selection; //ausgewählter text
  //var komplett = txt.match(/\d+\,\d+[a-zA-Z]+/);
  //txt=txt.replace(",",".");
  var zahl = txt.match(/\d*\,?\d*/);
  zahl=zahl.toString().replace(",",".");
  var einheit = txt.match(/[a-zA-Z]+/);
  einheit=einheit.toString().replace("V","\\volt ");
  einheit=einheit.toString().replace("kmh","\\kilo\\meter\\per\\hour ");
  einheit=einheit.toString().replace("W","\\watt ");
  einheit=einheit.toString().replace("A","\\ampere ");
  einheit=einheit.toString().replace("m","\\meter ");
  einheit=einheit.toString().replace("g","\\gram ");
  einheit=einheit.toString().replace("kg","\\kilo\\gram ");
  einheit=einheit.toString().replace("ms","\\meter\\per\\second ");
  einheit=einheit.toString().replace("s","\\second ");
  var wrapped = prefixA + zahl + suffixA + prefixB + einheit + suffixB;
  var len = txt.length;
  var pos = TW.target.selectionStart;
  if (pos >= prefixA.length) {
    TW.target.selectRange(pos - prefixA.length, wrapped.length);
    TW.target.selectRange(pos, len);
  }
  TW.target.insertText(wrapped);
  TW.target.selectRange(pos + prefixA.length + 5 + 2*len, 0);
  return;
};

addOrRemove("\\SI{", "}", "{", "}");

It kind of works. The first problem is the replacement. It iterates through, so I get double replacements. 2kmh becomes \SI{2}{\kilo\meter eter\per\hour }

It would be very helpful, if I could adress the single regular expression parts and use them.

Thank you very much for your help.

1 Answer 1

2

Seems, like I found the answer.

// TeXworksScript
// Title: convert SI
// Shortcut: Ctrl+Shift+L
// Description: Converts into SI Syntax
// (...)
// Script-Type: standalone
// Context: TeXDocument



function replace() {
  var txt = TW.target.selection; //ausgewählter text //save selected text in txt
  var posA = TW.target.selectionStart;
  var posi = txt.search(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g); //Position des ersten Elements //position of first element
  var item = txt.match(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g); //Inhalt des ersten Elements //content of first element
  if (item != null){ //Prüfe, ob ein Element vorhanden ist //check if there is an element
    TW.target.selectRange(posi+posA,item[0].length); //erstes Element markieren //select first element
    var wrapped = TW.target.selection;

//Ersetzen mit SI und {} //replace with SI and brakets 
    wrapped = wrapped.replace(/(\d+)(\,?\.?)(\d*)([a-zA-Z]+\/?[a-zA-Z]?\^?\d?)/g, "\\SI{$1$2$3}{$4}");
    wrapped = wrapped.replace(",",".");


//Ersetzungen der Einheiten //replacement of the units


//Volumina //volumes

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km\^3}/g, "\\SI{$1$2$3}{\\kilo\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m\^3}/g, "\\SI{$1$2$3}{\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm\^3}/g, "\\SI{$1$2$3}{\\deci\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm\^3}/g, "\\SI{$1$2$3}{\\centi\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm\^3}/g, "\\SI{$1$2$3}{\\milli\\meter\\cubed}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{ml}/g, "\\SI{$1$2$3}{\\milli\\liter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{l}/g, "\\SI{$1$2$3}{\\liter}");



//Flächen //areas

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km\^2}/g, "\\SI{$1$2$3}{\\kilo\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m\^2}/g, "\\SI{$1$2$3}{\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm\^2}/g, "\\SI{$1$2$3}{\\deci\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm\^2}/g, "\\SI{$1$2$3}{\\centi\\meter\\squared}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm\^2}/g, "\\SI{$1$2$3}{\\milli\\meter\\squared}");

//Längen //lengths

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{km}/g, "\\SI{$1$2$3}{\\kilo\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{m}/g, "\\SI{$1$2$3}{\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{dm}/g, "\\SI{$1$2$3}{\\deci\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{cm}/g, "\\SI{$1$2$3}{\\centi\\meter}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mm}/g, "\\SI{$1$2$3}{\\milli\\meter}");





//Elektrizität //electricity

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kA}/g, "\\SI{$1$2$3}{\\kilo\\ampere}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{A}/g, "\\SI{$1$2$3}{\\ampere}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mA}/g, "\\SI{$1$2$3}{\milli\\ampere}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kV}/g, "\\SI{$1$2$3}{\\kilo\\volt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{V}/g, "\\SI{$1$2$3}{\\volt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mV}/g, "\\SI{$1$2$3}{\milli\\volt}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kC}/g, "\\SI{$1$2$3}{\\kilo\\coulomb}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{C}/g, "\\SI{$1$2$3}{\\coulomb}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mC}/g, "\\SI{$1$2$3}{\\milli\\coulomb}");

    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kW}/g, "\\SI{$1$2$3}{\\kilo\\watt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{W}/g, "\\SI{$1$2$3}{\\watt}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mW}/g, "\\SI{$1$2$3}{\\milli\\watt}");

    
//Masse //mass
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{mg}/g, "\\SI{$1$2$3}{\\milli\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{g}/g, "\\SI{$1$2$3}{\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{kg}/g, "\\SI{$1$2$3}{\\kilo\\gram}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{t}/g, "\\SI{$1$2$3}{\\tonne}");


//Zeit //time
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{d}/g, "\\SI{$1$2$3}{\\day}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{h}/g, "\\SI{$1$2$3}{\\hour}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{min}/g, "\\SI{$1$2$3}{\\minute}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{s}/g, "\\SI{$1$2$3}{\\second}");
    wrapped = wrapped.replace(/\\SI{(\d+)(\,?\.?)(\d*)}{ms}/g, "\\SI{$1$2$3}{\\milli\\second}");



    TW.target.insertText(wrapped);
    TW.target.selectRange(posA,txt.length+wrapped.length-item[0].length); //ursprüngliche Auswahl markieren //select original slelected text
  }
  return;
};

replace();
2
  • Maybe you could add the English translation of the comments.
    – egreg
    Commented Jun 25, 2021 at 13:14
  • Done. If you have any questions, don't hesitate. Absolutely changing workflow
    – user125198
    Commented Jun 26, 2021 at 14:23

You must log in to answer this question.

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