How to replace LaTeX commands with Unicode symbols?
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();
});