Function Repository Resource:

SymbolToSubscript

Source Notebook

Convert symbols into subscript forms

Contributed by: E. Chan-López & Jaime Manuel Cabrera

ResourceFunction["SymbolToSubscript"][expr]

converts any multi-character symbol within expr so that all but the first character become a subscript.

ResourceFunction["SymbolToSubscript"][expr,"NestedIndices"]

nests multi-character subscripts.

ResourceFunction["SymbolToSubscript"][expr,"MultipleIndices"]

separates multi-character subscripts with commas.

Details and Options

ResourceFunction["SymbolToSubscript"] only acts on unprotected subscriptable symbols.
ResourceFunction["SymbolToSubscript"] converts subscriptable symbols involving C into C[i].
SymbolToSubscripts[expr,"NestedIndices"] can be used on subscriptable symbols with multiple indices.
SymbolToSubscripts[expr,"MultipleIndices"] can be used on symbolic matrices, where it separates numbers with commas.
SymbolToSubscripts[expr,"FormatStyle""ToStringFormat"] changes the format of the indices from symbol expressions to strings. It can be used to get the outputs of expressions in string format.

Examples

Basic Examples (2) 

Convert a symbol to a subscripted symbol:

In[1]:=
ResourceFunction["SymbolToSubscript"][x1]
Out[1]=

Convert multiple symbols:

In[2]:=
ResourceFunction["SymbolToSubscript"][{x1, x2}]
Out[2]=
In[3]:=
ResourceFunction["SymbolToSubscript"][{x1[t], x2[t]}]
Out[3]=
In[4]:=
ResourceFunction["SymbolToSubscript"][{x\[Alpha][t], x\[Beta][t]}]
Out[4]=

Scope (9) 

Use with a list of expressions:

In[5]:=
ResourceFunction[
 "SymbolToSubscript"][{Sin[t], Cos[x], Tan[s], Cot[x1], BesselJ[0, x1[t] + x2[t]], Csch[(x^(m - 2) - x3) (x^(n + 1) + x2)]}]
Out[5]=

Use with a symbolic matrix:

In[6]:=
A = {{a1, a2}, {a3, a4}};
MatrixForm@ResourceFunction["SymbolToSubscript"][#] &@A
Out[7]=

Use with a tensor of rank 3:

In[8]:=
MatrixForm@
 ResourceFunction["SymbolToSubscript"]@
  D[{x1^2 y1 + x1, -y1 x1^3 - 2 x1}, {{x1, y1}, 2}]
Out[8]=

Use with a tensor of rank 4:

In[9]:=
ResourceFunction["SymbolToSubscript"]@
 MatrixForm@D[{C1*x1^2 y1 + x1, -C2*y1 x1^3 - 2 C3*x1}, {{x1, y1}, 3}]
Out[9]=

Use with a nonlinear system of ordinary differential equations:

In[10]:=
ResourceFunction[
 "SymbolToSubscript"]@{x1'[t] == -x1[t]^2 - x2[t], x2'[t] == 2 x1[t] - x2[t]^3}
Out[10]=

Use with a system of partial differential equations:

In[11]:=
ResourceFunction["SymbolToSubscript"]@{\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(u1[t, x]\)\) == \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\((\((u2[t, x] - 1)\)\ 
\*SubscriptBox[\(\[PartialD]\), \(x\)]u1[t, x])\)\) + (16 x t - 2 t - 16 (u2[t, x] - 1)) (u1[t, x] - 1) + 10 x E^(-4 x), \!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(u2[t, x]\)\) == \!\(
\*SubscriptBox[\(\[PartialD]\), \({x, 2}\)]\(u2[t, x]\)\) + \!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\(u1[t, x]\)\) + 4 u1[t, x] - 4 + x^2 - 2 t - 10 t E^(-4 x)}
Out[11]=

SymbolToSubscript optionally uses "NestedIndices" to represent subscriptable symbols with multiple indices in a nested format:

In[12]:=
ResourceFunction["SymbolToSubscript"][Xabc + Ycde, "NestedIndices"]
Out[12]=

SymbolToSubscript optionally uses "MultipleIndices" to represent subscriptable symbols with multiple indices, separating them by commas:

In[13]:=
B = Array[Subscript[b, ##] &, {3, 3}];
ResourceFunction["SymbolToSubscript"][#, "MultipleIndices"] &@
 MatrixForm@B
Out[14]=

Both the base and subscript in the output are symbols and inherit any existing definitions:

In[15]:=
y = 3; z = 5;
In[16]:=
ResourceFunction["SymbolToSubscript"][yz]
Clear[y, z]
Out[16]=

Options (1) 

FormatStyle (1) 

SymbolToSubscript includes an option that enables its outputs to be represented in a string format:

In[17]:=
gridData = {{"No String Format", "String Format"}, TeXForm /@ {ResourceFunction["SymbolToSubscript"][x12], ResourceFunction["SymbolToSubscript"][x12, "FormatStyle" -> "ToStringFormat"]}};
Grid[gridData, Frame -> All, Background -> {None, {LightGray, {White}}}]
Out[18]=

Applications (3) 

Format a symbolic polynomial:

In[19]:=
polynomial[var_Symbol, coeff_Symbol, n_Integer?NonNegative] := Total[Array[
   Times[ToExpression[
      ToString[coeff] <> ToString[FromDigits[{n - ##}]]], var^#] &, n + 1, 0]]
In[20]:=
Column[MapAt[ResourceFunction["SymbolToSubscript"], ConstantArray[polynomial[x, a, 5], 2], {2}], Spacings -> 0.75]
Out[20]=

Format a symbolic matrix:

In[21]:=
Matrix[symbol_Symbol, {row_Integer?Positive, column_Integer?Positive}] :=
 ToExpression[
  Array[ToString[symbol] <> ToString[#1] <> ToString[#2] &, {row, column}]]
In[22]:=
Row[Map[MatrixForm, MapAt[Map[ResourceFunction["SymbolToSubscript"], #] &, ConstantArray[Matrix[x, {5, 5}], 2], {2}]], " "]
Out[22]=
In[23]:=
Row[Map[MatrixForm, MapAt[ResourceFunction["SymbolToSubscript"][#, "MultipleIndices"] &,
    ConstantArray[Matrix[x, {5, 5}], 2], {2}]], " "]
Out[23]=

Use SymbolToSubscript and TeXForm:

In[24]:=
TeXForm@MatrixForm@
    ResourceFunction["SymbolToSubscript"][#, "MultipleIndices"] &@
 Matrix[x, {5, 5}]
Out[24]=

Define a function for making a Vandermonde matrix:

In[25]:=
vandermondeMatrix[symbol_Symbol, n_Integer?Positive] := Map[ToExpression, Array[(ToString[symbol] <> ToString[#1])^(#2 - 1) &, ConstantArray[n, 2]], {-1}]

Format a Vandermonde matrix:

In[26]:=
Row[Map[MatrixForm, MapAt[Map[ResourceFunction["SymbolToSubscript"], #] &, ConstantArray[vandermondeMatrix[x, 5], 2], {2}]], " "]
Out[26]=

The SymbolToSubscript command allows TeXForm to be used correctly when we have expressions with subscripts that are written as symbols:

In[27]:=
MatrixForm@vandermondeMatrix[x, 5]
Out[27]=
In[28]:=
TeXForm@MatrixForm@ResourceFunction["SymbolToSubscript"]@%
Out[28]=
In[29]:=
ResourceFunction["SymbolToSubscript"]@
 Factor@Det@vandermondeMatrix[x, 5]
Out[29]=
In[30]:=
TeXForm@%
Out[30]=

Applications (2) 

Use SymbolToSubscript with the resource function HurwitzMatrix:

In[31]:=
polynomial[var_Symbol, coeff_Symbol, n_Integer?NonNegative] := Total[Array[
   Times[ToExpression[
      ToString[coeff] <> ToString[FromDigits[{n - ##}]]], var^#] &, n + 1, 0]]
In[32]:=
ResourceFunction["HurwitzMatrix"][polynomial[x, C, 7], x];
MatrixForm@%
Out[33]=
In[34]:=
ResourceFunction["SymbolToSubscript"]@MatrixForm@%
Out[34]=

Use SymbolToSubscript with the resource function SolutionRulesToFunctions:

In[35]:=
ResourceFunction["SymbolToSubscript"]@
 ResourceFunction[
   "SolutionRulesToFunctions"][\[Theta][t] -> \[Theta]0 + t \[Omega]0 + (g t^2 Sin[\[Alpha]])/(3 R)]
Out[35]=

Possible Issues (2) 

The function SymbolToSubscript, when transforming an expression into subscript format, may encounter a potential issue. As the function dissects the expression and processes each component via ToExpression, numerical expressions are simplified by default. For instance, if the only numeric indices we have are zero followed by one, we are left with just the number one. This interpretation leads to the conversion of this pair of numbers into a single subscript, which may not be the intended outcome. To circumvent this problem and provide more granular control over the output, the function is equipped with options that allow for improved handling of how expression components are processed and displayed. Let's illustrate this with an example:

In[36]:=
ResourceFunction["SymbolToSubscript"][x1y2]
Out[36]=

To handle the above problem, SymbolToSubscript has the "ToStringFormat" option:

In[37]:=
ResourceFunction["SymbolToSubscript"][x1y2, "FormatStyle" -> "ToStringFormat"]
Out[37]=
In[38]:=
TeXForm@%
Out[38]=

We can also address this issue using the options for nested indices and multiple indices separated by commas, as demonstrated below:

In[39]:=
gridData = {{"Nested Indices Format", "Multiple Indices Format"}, TeXForm /@ {ResourceFunction["SymbolToSubscript"][x1y2, "NestedIndices"], ResourceFunction["SymbolToSubscript"][x1y2, "MultipleIndices"]}};
Grid[gridData, Frame -> All, Background -> {None, {LightGray, {White}}}]
Out[40]=

The SymbolToSubscript function provides a simple way to handle subscriptable symbols. However, it's essential to be mindful of protected symbols such as E (exponential constant), π (Pi), I (Imaginary unit), among others when using this function. Let's illustrate this with an example:

In[41]:=
ResourceFunction["SymbolToSubscript"]@{\[Pi]2, E2, I2} // N
Out[41]=

To avoid the above misunderstandings, users are highly advised not to use protected symbols.

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 3.0.0 – 09 August 2023
  • 2.0.0 – 26 June 2023
  • 1.1.0 – 22 August 2022
  • 1.0.0 – 19 July 2022

Related Resources

License Information