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]=
Image

Convert multiple symbols:

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

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]=
Image

Use with a symbolic matrix:

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

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]=
Image

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]=
Image

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]=
Image

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]=
Image

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

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

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]=
Image

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]=
Image

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]=
Image

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]=
Image

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]=
Image
In[23]:=
Row[Map[MatrixForm, MapAt[ResourceFunction["SymbolToSubscript"][#, "MultipleIndices"] &,
    ConstantArray[Matrix[x, {5, 5}], 2], {2}]], " "]
Out[23]=
Image

Use SymbolToSubscript and TeXForm:

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

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]=
Image

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]=
Image
In[28]:=
TeXForm@MatrixForm@ResourceFunction["SymbolToSubscript"]@%
Out[28]=
Image
In[29]:=
ResourceFunction["SymbolToSubscript"]@
 Factor@Det@vandermondeMatrix[x, 5]
Out[29]=
Image
In[30]:=
TeXForm@%
Out[30]=
Image

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]=
Image
In[34]:=
ResourceFunction["SymbolToSubscript"]@MatrixForm@%
Out[34]=
Image

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]=
Image

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]=
Image

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

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

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]=
Image

The SymbolToSubscript behavior is governed by a strict internal check: the transformation is blocked if the symbol contains any character that is defined within the System context such as E (exponential constant), π (Pi), I (imaginary unit), and D (derivative operator), etc. This mechanism effectively prevents issues with built-in symbols and preserves symbolic integrity. The only symbol exempted from this check is the symbol C (commonly used for integration constants or generic coefficients), which will be converted if followed by another symbol, but typically by an integer.

The following examples demonstrate how symbols containing elements defined in the System context are left untouched (e.g., those containing E or D), while the symbol C (which is an explicit exception) is transformed:

In[41]:=
ResourceFunction["SymbolToSubscript"]@{\[Pi]2, E2, I2, K2, I2, D2, C1}
Out[41]=
Image

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 4.0.0 – 22 October 2025
  • 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