5
$\begingroup$

Any Ideas how I could convert unicode to Mathematica Input ?

For instance : I have the following Unicode: test= {"a", "\\:1d62"}

So here I would like to convert it to:

Subscript[a,i]

This is just an example though. I really would like to have a general way of converting it to math input.

$\endgroup$
5
  • $\begingroup$ Subscript @@ ToExpression /@ test $\endgroup$
    – LouisB
    Oct 16, 2018 at 19:43
  • $\begingroup$ @LouisB Thanks for your comment. However, I would need something that works in general $\endgroup$
    – james
    Oct 16, 2018 at 19:47
  • 3
    $\begingroup$ @james in what way does that not work generally ? $\endgroup$
    – ktm
    Oct 16, 2018 at 23:34
  • $\begingroup$ @user6014 Well what if my unicode text does not have a subscript in some parts but not in all parts? $\endgroup$
    – james
    Oct 17, 2018 at 4:08
  • $\begingroup$ james, can you add the example list (in your comment below) to your question? $\endgroup$
    – kglr
    Oct 31, 2018 at 9:41

2 Answers 2

7
$\begingroup$

You can use RemoveDiacritics:

RemoveDiacritics /@ {"a", "\:1d62"}
{"a", "i"}
$\endgroup$
2
  • $\begingroup$ Thanks, but how do you convert it to math input form ? The little "i" is defined as a subscript "i" in unicode. So my goal is that the Mathematica code realizes that and creates the variable "a subscript i". $\endgroup$
    – james
    Oct 17, 2018 at 4:07
  • $\begingroup$ I would say that you just need to use @Chip Hurst's answer and prepend it with this: Subscript @@ ToExpression /@, that way you would get Subscript @@ ToExpression /@ RemoveDiacritics /@ {"a", "\:1d62"} which gives the desired result. EDIT: Sorry, just saw this in the comments above. $\endgroup$
    – wilsnunn
    Nov 12, 2018 at 9:43
6
$\begingroup$

A very long answer for a very short question...

A manually compiled list of Unicode subscript and superscript characters (from this post):

unicodeSubSuperscriptList = {8304, 8320, 185, 8321, 178, 8322, 179, 
   8323, 8308, 8324, 8309, 8325, 8310, 8326, 8311, 8327, 8312, 8328, 
   8313, 8329, 7491, 8336, 7495, 7580, 7496, 7497, 8337, 7584, 7501, 
   688, 8341, 8305, 7522, 690, 11388, 7503, 8342, 737, 8343, 7504, 
   8344, 8319, 8345, 7506, 8338, 7510, 8346, 691, 7523, 738, 8347, 
   7511, 8348, 7512, 7524, 7515, 7525, 695, 739, 8339, 696, 7468, 
   7470, 7472, 7473, 7475, 7476, 7477, 7478, 7479, 7480, 7481, 7482, 
   7484, 7486, 7487, 7488, 7489, 11389, 7490, 8314, 8330, 8315, 8331, 
   8316, 8332, 8317, 8333, 8318, 8334, 7493, 7517, 7526, 7518, 7527, 
   7519, 7499, 7615, 7589, 7528, 7602, 7520, 7529, 7521, 7530} ;

ussl = FromCharacterCode /@ unicodeSubSuperscriptList;
Multicolumn[ussl, 20, Appearance -> "Horizontal"]

enter image description here

unicodeSubSuperscriptQ = StringMatchQ[Alternatives @@ (FromCharacterCode /@ 
   unicodeSubSuperscriptList )];
fromSubSuperscripts = ToCharacterCode[#][[1]] /. 
   {8304 | 8320 -> "0", 185 | 8321 -> "1", 178 | 8322 -> "2", 
    179 | 8323 -> "3", 8308 | 8324 -> "4", 8309 | 8325 -> "5", 
    8310 | 8326 -> "6", 8311 | 8327 -> "7", 8312 | 8328 -> "8", 
    8313 | 8329 -> "9", 7491 | 8336 -> "a", 7495 -> "b",  7580 -> "c", 
    7496 -> "d", 7497 | 8337 -> "e", 7584 -> "f", 7501 -> "g", 
    688 | 8341 -> "h", 8305 | 7522 -> "i", 690 | 11388 -> "j", 
    7503 | 8342 -> "k", 737 | 8343 -> "l", 7504 | 8344 -> "m", 
    8319 | 8345 -> "n", 7506 | 8338 -> "o", 7510 | 8346 -> "p", 
    691 | 7523 -> "r", 738 | 8347 -> "s", 7511 | 8348 -> "t", 
    7512 | 7524 -> "u", 7515 | 7525 -> "v", 695 -> "w", 
    739 | 8339 -> "x", 696 -> "y", 7468 -> "A", 7470 -> "B",  7472 -> "D",
    7473 -> "E", 7475 -> "G", 7476 -> "H", 7477 -> "I",  7478 -> "J", 
    7479 -> "K", 7480 -> "L", 7481 -> "M", 7482 -> "N", 7484 -> "O", 
    7486 -> "P", 7487 -> "R", 7488 -> "T", 7489 -> "U", 11389 -> "V", 
    7490 -> "W", 8314 | 8330 -> "+", 8315 | 8331 -> "-", 
    8316 | 8332 -> "=", 8317 | 8333 -> "{", 8318 | 8334 -> "}", 
    7493 -> "α", 7517 | 7526 -> "β", 7518 | 7527 -> "γ", 7519 -> "δ", 
    7499 -> "ϵ", 7615 -> "θ", 7589 -> "ι", 7528 -> "ρ", 7602 -> "ϕ", 
    7520 | 7529 -> "ψ", 7521 | 7530 -> "χ", x_ :> FromCharacterCode[x]} &; 

Examples:

Multicolumn[fromSubSuperscripts /@ ussl, 20, Appearance -> "Horizontal"]

enter image description here

test= {"a", "\\:1d62"} ;
Subscript @@ fromSubSuperscripts /@ (Characters /@ ToString[ToExpression @ #]& /@ test)

TeXForm[%]

$\text{a}_{\text{i}}$

A more challenging example:

list = {"a", "\\:1d62", "\\:2081", "\[Beta]", "\\:2081", " ", "+", 
  " ", "a", "\\:1d62", "\\:2082", "\[Beta]", "\\:2082", " ", "+", 
  " ", ".", ".", ".", " ", "+", " ", "a", "\\:2099", "\\:2081", 
  "\[Beta]", "\\:2099", " ", "=", " ", "b", "\\:1d62"} ;

fromSubSuperscripts /@ (If[SyntaxQ[#] && Not @ StringMatchQ[#, " "], 
   ToExpression[#, StandardForm, ToString], #]& /@ list)

{"a", "i", "1", "β", "1", " ", "+", " ", "a", "i", "2", "β", "2", " ", "+", " ", ".", ".", ".", " ", "+", " ", "a", "n", "1", "β", "n", " ", "=", " ", "b", "i"}

string = StringJoin[If[SyntaxQ[#] && Not@StringMatchQ[#, " "], 
           ToExpression[#, StandardForm, ToString], #] & /@ list] ;

ToExpression[StringReplace[string, {"..." -> "…", 
   a : LetterCharacter ~~ b : Longest[__?unicodeSubSuperscriptQ] :> 
   "Subscript[" <> a <> "," <> StringRiffle[fromSubSuperscripts /@ Characters[b], ","] <> 
     "]"}], StandardForm, HoldForm] 

enter image description here

$\endgroup$
5
  • $\begingroup$ Nice. I wonder if reformulating as an Association[] and using Lookup[] might be convenient... $\endgroup$ Oct 17, 2018 at 7:45
  • $\begingroup$ @J.M.iscomputer-less, that's great idea. $\endgroup$
    – kglr
    Oct 17, 2018 at 7:58
  • $\begingroup$ Very nice ! Thank you so much for your great work ! $\endgroup$
    – james
    Oct 17, 2018 at 8:28
  • $\begingroup$ can you also try: list = {"a", "\\:1d62", "\\:2081", "\[Beta]", "\\:2081", " ", "+", " ", "a", "\\:1d62", "\\:2082", "\[Beta]", "\\:2082", " ", "+", " ", ".", ".", ".", " ", "+", " ", "a", "\\:2099", "\\:2081", "\[Beta]", "\\:2099", " ", "=", " ", "b", "\\:1d62"} $\endgroup$
    – james
    Oct 17, 2018 at 8:55
  • $\begingroup$ @james, please see the update. I suggest you also add list to your question; as is test is misleadingly simple. $\endgroup$
    – kglr
    Oct 17, 2018 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.