Tuesday 19 November 2019

windows - How to replace LaTeX commands with Unicode symbols?


Often, during a conversation or an email, or at a forum, I would like to type some math, but I don't need full equation support. Unicode symbols should suffice.


What I need is an easy way to type math-related Unicode symbols. Since I already know LaTeX, it makes sense to use the LaTeX symbol mnemonics to type the math symbols.


What I currently did is to write an AutoHotkey script which automatically replaces LaTeX symbol names preceded by \ with the corresponding Unicode symbol, using the hotstring AutoHotkey feature. However, the AutoHotkey hotstrings proved unstable for many strings. Having a couple of tens lines would cause AHK to fail recognizing the strings from time to time.


Any other solution? (No, Alt+(Unicode number) isn't convenient enough.)


Attached is my AHK script. The PutUni function is taken from here.


::\infty::
PutUni("e2889e")
return
::\sum::
PutUni("e28891")
return
::\int::
PutUni("e288ab")
return
::\pm::
PutUni("c2b1")
return
::\alpha::
PutUni("c991")
return
::\beta::
PutUni("c992")
return
::\phi::
PutUni("c9b8")
return
::\delta::
PutUni("ceb4")
return
::\pi::
PutUni("cf80")
return
::\omega::
PutUni("cf89")
return
::\in::
PutUni("e28888")
return
::\notin::
PutUni("e28889")
return
::\iff::
PutUni("e28794")
return
::\leq::
PutUni("e289a4")
return
::\geq::
PutUni("e289a5")
return
::\sqrt::
PutUni("e2889a")
return
::\neq::
PutUni("e289a0")
return
::\subset::
PutUni("e28a82")
return
::\nsubset::
PutUni("e28a84")
return
::\nsubseteq::
PutUni("e28a88")
return
::\subseteq::
PutUni("e28a86")
return
::\prod::
PutUni("e2888f")
return
::\N::
PutUni("e28495")
return

Answer



I use a javascript bookmarklet for typing unicode symbols at math.stackexchange.com. Mathjax renders most unicode the same as the corresponding latex macros. For example $ℝ$ and $\mathbb{R}$ give the same result. I like the way tex code stays more compact and readable with unicode symbols.


I think this code is able to do what you want. I like to use not too many keystrokes, so instead of \alpha I use \a to produce α. You can modify this script to your own needs, and then convert it to a bookmarklet, using this website for example: http://jasonmillerdesign.com/Free_Stuff/Instant_Bookmarklet_Converter


If you want to use this script on a website without jquery, then you first need to run this bookmarklet: http://www.learningjquery.com/2006/12/jquerify-bookmarklet/


jQuery.fn.autocorrect = function(options)
{
if ("text" != jQuery(this).attr("type") && !jQuery(this).is("textarea"))
{
return;
}
var defaults = {
corrections: {
a: "α",
b: "β",
c: "γ",
d: "δ",
e: "ϵ",
emp : "∅",
f: "\\frac{}{}",
in : "∈",
s: "σ",
t: "\\text{}",
tau : "τ",
th : "θ",
p: "π",
pm : "±",
o : "ω",
O : "Ω",
r : "ρ",
A : "∀",
E : "∃",
R: "ℝ",
C: "ℂ",
H: "ℍ",
N: "ℕ",
Q: "ℚ",
Z: "ℤ",
int: "\\int_{}^{}",
inf : "∞",
sum : "\\sum_{}^{}",
"-1": "^{-1}",
ph: "ϕ",
ch: "χ",
ps: "ψ",
leq : "≥",
xi : "ξ",
geq : "≤",
"/=" : "≠",
"==" : "≡",
"<" : "\\langle {} \\rangle",
"->" : "→",
"=>" : "⇒",
"<=" : "⇐",
"<>" : "⇔",
"sq" : "\\sqrt{}"
}
};
if (options && options.corrections)
{
options.corrections = jQuery.extend(defaults.corrections, options.corrections);
}
var opts = jQuery.extend(defaults, options);
getCaretPosition = function(oField)
{
var iCaretPos = 0;
if (document.selection)
{
var oSel = document.selection.createRange();
oSel.moveStart("character", 0 - oField.value.length);
iCaretPos = oSel.text.length;
}
else if (oField.selectionStart || oField.selectionStart == "0")
{
iCaretPos = oField.selectionStart;
}
return (iCaretPos);
}
function setCaretPosition (oField, iCaretPos)
{
if (document.selection)
{
var oSel = document.selection.createRange();
oSel.moveStart("character", 0 - oField.value.length);
oSel.moveStart("character", iCaretPos);
oSel.moveEnd("character", 0);
}
else if (oField.selectionStart || oField.selectionStart == "0")
{
oField.selectionStart = iCaretPos;
oField.selectionEnd = iCaretPos;
}
}
this.keyup(function(e)
{
if (32 != e.keyCode)
{
return;
}
var caretPosition = (getCaretPosition(this) - 1);
if (1 > caretPosition)
{
return;
}
var valueOfField = this.value;
var stringUptoCaretPosition = (valueOfField).substr(0, caretPosition);
if (" " == stringUptoCaretPosition.charAt(caretPosition - 1))
{
return;
}
var beginIndex = stringUptoCaretPosition.lastIndexOf('\\');
if (beginIndex < stringUptoCaretPosition.lastIndexOf(' '))
{
return;
}
var stringToSearch = stringUptoCaretPosition.substring(beginIndex+1);
var stringNotToSearch = stringUptoCaretPosition.substring(0, beginIndex);
if (!opts.corrections[stringToSearch])
{
return;
}
var stringToReplace = opts.corrections[stringToSearch];
stringUptoCaretPosition = stringNotToSearch+ stringToReplace;
var stringFromCaretPositionUptoEnd = (valueOfField).substr(caretPosition+1);
this.value = (stringUptoCaretPosition + stringFromCaretPositionUptoEnd);
if (stringToReplace.indexOf("{}")!=-1 )
{
setCaretPosition(this, stringUptoCaretPosition.indexOf("{}")+1);
}
else { setCaretPosition(this, stringUptoCaretPosition.length);}

});
};
$(document).ready(function()
{
$("textarea").autocorrect();
});

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...