Character sequence for a unicode character

I’d like to get the keyboard input needed to write a unicode symbol. Alternatively, getting the help output of the symbol would be just as good. That is, for 𝒜

help?> 𝒜
"𝒜" can be typed by \scrA<tab>
...

I’d like to get the “\scrA”. The above output isn’t part of the normal @doc machinery though as far as I can tell, and I can’t figure where to find it as a string.

Something like:

using REPL

function whatkey(s)
   for (k, v) in REPL.REPLCompletions.latex_symbols
      if v == s
         println("$(s) can be typed by $(k)")
      end
   end
end

whatkey("𝒜")

𝒜 can be typed by \scrA

4 Likes

Note, there’s an exact function to get the help output printed:

julia> REPL.repl_latex("√")
"√" can be typed by \sqrt<tab>

Almost the same if you want in a variable:

s=REPL.symbol_latex("√")  # you need to strip the extra \ that I THINK is always there.
"\\sqrt"

You could have modified the other code if you want only one def printed out (or returned for use in a variable):

println("$(s) can be typed by $(k)<tab>"); return # as the lookup isn't unique

but it would return:

julia> whatkey("√")
√ can be typed by \surd

instead of:
julia> whatkey("√")  # both correct:
√ can be typed by \surd<tab>
√ can be typed by \sqrt<tab>

FYI: The code in Base uses a Dict, and while they do not specify order, Base relies there on the specific order it is in. This is maybe not a bug unless you try to change the Dict implementation, as I tried. I.e. to OrderedDict. Then this code broke tests, gave \surd rather than \sqrt while both technically correct options, and could thus have been given, while I was only familiar with the latter.

7 Likes

Good stuff! I should have read the manual… :slight_smile:

That would not have worked, looking up either repl_latex or symbol_latex gives “Number of results: 0” :slight_smile:

I only have knew about the duplicate issue since I was working on Dict replacement. Now, I didn’t remember what function provided, so looking up “can be typed by” in the source code (at Github) helped, and everyone could have done that, as the code for this must me somewhere in Julia.

If there are multiple ways to enter the same symbol, then indeed on different systems currently one can have different character sequences. But on the other hand, I highly doubt that one version is any better than the other and should be considered as preferred. Anyway currently there is only 20 duplicates

using SplitApplyCombine
using Underscores

julia> symbols = Base.REPL_MODULE_REF[].REPLCompletions.latex_symbols

julia> @_ group(_[2], _[1], symbols) |> filter(length(_) > 1, __)
20-element Dictionaries.Dictionary{Any,Array{Any,1}}
 "⟸" │ Any["\\impliedby", "\\Longleftarrow"]
 "√" │ Any["\\surd", "\\sqrt"]
 "≥" │ Any["\\ge", "\\geq"]
 "♂" │ Any["\\male", "\\mars"]
 "→" │ Any["\\to", "\\rightarrow"]
 "⟧" │ Any["\\openbracketright", "\\rrbracket"]
 "♀" │ Any["\\venus", "\\female"]
 "∅" │ Any["\\varnothing", "\\emptyset"]
 "…" │ Any["\\dots", "\\ldots"]
 "⨝" │ Any["\\Join", "\\join"]
 "⊻" │ Any["\\veebar", "\\xor"]
 "⟹" │ Any["\\implies", "\\Longrightarrow"]
 "⟦" │ Any["\\llbracket", "\\openbracketleft"]
 "∇" │ Any["\\del", "\\nabla"]
  "̶" │ Any["\\strike", "\\sout"]
 "ε" │ Any["\\upepsilon", "\\varepsilon"]
 "ð" │ Any["\\eth", "\\dh"]
 "⟺" │ Any["\\Longleftrightarrow", "\\iff"]
 "ℯ" │ Any["\\scre", "\\euler"]
 "≤" │ Any["\\le", "\\leq"]

Maybe instead of ordered return version with the least amount of symbols? Yet it wouldn’t solve sqrt/surd problem.

1 Like

Thanks everyone for the replies! Literally each of these methods is better than I’d hoped for!
Even just learning about REPL.REPLCompletions.latex_symbols is great.

1 Like

@tomerarnon, I’m having second thoughts about pointing you to REPL.symbol_latex("√")

It might be NOT exported for a reason. At least I’m considering a PR where it returns an array of possibilities. Or possibly add symbols_latex for that, if compatibility is important.

Now, it seems pretty random what’s returned:

julia> REPL.symbol_latex("♂")
"\\mars"

vs.

julia> REPL.symbol_latex("♀")
"\\female"

Another reason not to prefer shorter length, I prefer \euler shown (what’s now done) over \scre but there’s no reason to not show both.

And since I’m Icelandic I want eth (how it’s read) for our ð, Ð, that was also in Old English. It was replaced in English with dh and later d. Do English-native people prefer?

julia> REPL.symbol_latex("ð")
"\\dh"

I like in general that you can type in more than one option. In that case it seems to me, surd isn’t for sure a square root, could be a cube root, just somebody decided square:

Surds. When we can’t simplify a number to remove a square root (or cube root etc) then it is a surd.

3 Likes