How to replace LaTeX commands with Unicode symbols?

3

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

Elazar Leibovich

Posted 2010-01-03T13:37:27.300

Reputation: 744

FYI: using AutoHotKey to streamline LaTeX document authoring

– Ooker – 2017-11-11T22:32:36.733

Did the bookmarklet work for you ? – Kasper – 2013-11-03T04:39:56.287

Didn't try that yet. – Elazar Leibovich – 2013-11-03T07:42:29.130

Answers

3

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();
});

Kasper

Posted 2010-01-03T13:37:27.300

Reputation: 299

0

Autohotkey-script for converting LaTeX-like input to unicode characters

"Ctrl+Alt+Shift+U" toggles it on and off (look at the bottom right icon to see it's in suspense mode (icon S) of active mode (icon H).

Test: αβΓ∞¹₂ℝ

Ooker

Posted 2010-01-03T13:37:27.300

Reputation: 1 242

0

I like to use a software package for inputting characters from languages with non-Latin characters, such as Chinese. In, for example, Sogou pinyin one can define strings to be other strings of Unicode characters. Then, when typing, I quickly switch over to Chinese, write alpha, choose α from the list of suggestions and silently switch back to my original input language. This is really convenient!

MunHo

Posted 2010-01-03T13:37:27.300

Reputation: 1

0

If I need it I just tend to type the math in Word and copy/paste. Word uses the same macros LaTeX uses by default and auto-converts them into proper Unicode. And I find the linear format there more readable than LaTeX markup.

Still, if your mail recipients read from some dumb terminal or without proper font support (on XP maybe) you probably want to just type up raw LaTeX instead of Unicode just to allow them to understand what you want to say.

Joey

Posted 2010-01-03T13:37:27.300

Reputation: 36 381

1First, I stated explicitly the use case. Online internet forum/chat. There's no sane webservice today that do not support UTF-8. I might not be able to use the more obscure symbols, but common machines with common unicode fonts has much more than I could use. I have no intention to support terminal, although xterm should be able to cope with that.

About Word: (1) It works only with Office07, other math support lame. (2) I'm trying to avoid this context switch. What about chatting session? Alt-tab+Ctrl-C+Ctrl-v is not good enough. – Elazar Leibovich – 2010-01-03T15:20:03.597

0

You could use MSKLC to create a math-centric keyboard layout. For example eurokb includes symbols like ≡≤≠±∔π∜ on AltGr dead key combinations. (eurokb probably isn't math-heavy enough for you, but it might give you some ideas.)

bobince

Posted 2010-01-03T13:37:27.300

Reputation: 8 816

The only downside is, no mnemonics at all. I'll have to memorize tens of keyboard combinations.

BTW I think you should include a simulation of the keyboard mapping of your eurokb map on its website, and not just in the distribution. That would be useful for testing it. – Elazar Leibovich – 2010-01-04T14:43:26.467

(I do: hit the UK or US documentation link and it's at the top.) Yeah, you would certainly have to come up with a logical way to arrange and remember the layout, but you've got quite a lot of latitude to play with when you can have any combination of two potentially-shifted-or-alted keys. The Greek letters you pretty much already have by doing as eurokb does and following the standard Greek keyboard layout (http://en.wikipedia.org/wiki/Keyboard_layout#Greek). Then you could group by function, eg. AltGr+S for set functions; AltGr+S,= for ∈, AltGr+S,/ for ∉, or whatever.

– bobince – 2010-01-05T02:49:14.140

-2

Type up a short LaTeX fragment and tell them to cut and paste it into http://www.numberempire.com/texequationeditor/equationeditor.php and press "Render". Example:

\begin{align*}
R_s &\;=\; \frac{\widehat{\sigma}_s}{K} \left(\sqrt{1 - \sum^{15}_{k=1} \beta_k^2} \right) G_s
\quad.
\\
& \text{Parlez-vous Fran{\c c}ais? Czy mo\.zna u\.zy\'c tego?}
\\
& \mathbb{Z}_2 \oplus \mathbb{Z}_3 \cdots \oplus \mathbb{Z}_p,  
\end{align*}

Peon

Posted 2010-01-03T13:37:27.300

Reputation: 771

2-1. I asked: "What I need is an easy way to type math related unicode symbols." You told me how to render latex online to an image. I don't see how what you say helps me. It doesn't allow me to paste the result anywhere. – Elazar Leibovich – 2010-03-25T11:07:28.630

I said you should "tell them [your reader] to cut and paste" the LaTeX to the site to view the equations. – Peon – 2010-03-25T19:06:48.990

It's really a burden. What if I include 10 small equations? What if I'm chatting with my friend, and he's reading it through a smartphone? – Elazar Leibovich – 2011-10-04T07:30:04.833

If this disturbs you so much, why don't you write an app which translates unicode mneumonics to characters? Write it for his smartphone (Android, PalmOS, or WebOS), port it to Windows and Mac as well. Have it integrate with all existing apps too. If you are really deeply serious about it, of course. Otherwise you can have him learn to read Latex. Since your symbols are so limited, you can use an app that understands HTML and therefore can render unicode escape sequences into unicode characters. – Peon – 2011-10-05T23:57:59.143

The nice thing about unicode is, I don't have to write the app for his smartphone. I can just type the unicode symbols, and they would appear nicely on his smartphone without any effort from my side. An app for windows is enough (and actually it's sort-of what I've written in the answer). – Elazar Leibovich – 2011-10-06T04:51:04.757