Skip to main content
edited body
Source Link
Domen
  • 33.4k
  • 3
  • 47
  • 66

Here is a code that works for subscript numerals, even if they represent a multi-digit number. You can modify the code to include also other subscript characters.

subscriptNumerals = CharacterRange["₁"CharacterRange["₀", "₉"];
convertSubscripts[str_String] := ToExpression[str] /. 
  (sym_Symbol /; StringContainsQ[SymbolName[sym], subscriptNumerals]) :> 
   First@StringReplace[SymbolName[sym], (x : Except[subscriptNumerals]) ~~ 
    (i : subscriptNumerals ..) :> 
   Subscript[x, FromDigits[(First@*ToCharacterCode /@ Characters[i]) - 8320]]]

convertSubscripts["α₂ + t β₁₄ z + 3γ₁₇₈"]
(* Subscript["α", 2] + t z Subscript["β", 14] + 3 Subscript["γ", 178] *)

Here is a code that works for subscript numerals, even if they represent a multi-digit number. You can modify the code to include also other subscript characters.

subscriptNumerals = CharacterRange["₁", "₉"];
convertSubscripts[str_String] := ToExpression[str] /. 
  (sym_Symbol /; StringContainsQ[SymbolName[sym], subscriptNumerals]) :> 
   First@StringReplace[SymbolName[sym], (x : Except[subscriptNumerals]) ~~ 
    (i : subscriptNumerals ..) :> 
   Subscript[x, FromDigits[(First@*ToCharacterCode /@ Characters[i]) - 8320]]]

convertSubscripts["α₂ + t β₁₄ z + 3γ₁₇₈"]
(* Subscript["α", 2] + t z Subscript["β", 14] + 3 Subscript["γ", 178] *)

Here is a code that works for subscript numerals, even if they represent a multi-digit number. You can modify the code to include also other subscript characters.

subscriptNumerals = CharacterRange["₀", "₉"];
convertSubscripts[str_String] := ToExpression[str] /. 
  (sym_Symbol /; StringContainsQ[SymbolName[sym], subscriptNumerals]) :> 
   First@StringReplace[SymbolName[sym], (x : Except[subscriptNumerals]) ~~ 
    (i : subscriptNumerals ..) :> 
   Subscript[x, FromDigits[(First@*ToCharacterCode /@ Characters[i]) - 8320]]]

convertSubscripts["α₂ + t β₁₄ z + 3γ₁₇₈"]
(* Subscript["α", 2] + t z Subscript["β", 14] + 3 Subscript["γ", 178] *)
Source Link
Domen
  • 33.4k
  • 3
  • 47
  • 66

Here is a code that works for subscript numerals, even if they represent a multi-digit number. You can modify the code to include also other subscript characters.

subscriptNumerals = CharacterRange["₁", "₉"];
convertSubscripts[str_String] := ToExpression[str] /. 
  (sym_Symbol /; StringContainsQ[SymbolName[sym], subscriptNumerals]) :> 
   First@StringReplace[SymbolName[sym], (x : Except[subscriptNumerals]) ~~ 
    (i : subscriptNumerals ..) :> 
   Subscript[x, FromDigits[(First@*ToCharacterCode /@ Characters[i]) - 8320]]]

convertSubscripts["α₂ + t β₁₄ z + 3γ₁₇₈"]
(* Subscript["α", 2] + t z Subscript["β", 14] + 3 Subscript["γ", 178] *)