Function Repository Resource:

SymbolToSubscript (2.0.0) current version: 3.0.0 »

Source Notebook

Convert symbols into subscript forms

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

ResourceFunction["SymbolToSubscript"][expr]

gives the representation with subscripts for one or more unprotected symbols expr.

Details

ResourceFunction["SymbolToSubscript"] does not act on numbers, it just returns them.
ResourceFunction["SymbolToSubscript"] does not act on symbols that already have subscripts, it just returns them.
ResourceFunction["SymbolToSubscript"] does not act on protected heads of functions.
SymbolToSubscripts can be used on symbolic matrices, where it separates numbers with commas.
SymbolToSubscripts[expr,"ToStringFormat"] can be used to get the string format of expressions.

Examples

Basic Examples (2) 

Use SymbolToSubscript with an unprotected symbol:

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

Use SymbolToSubscript with a list of unprotected 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 SymbolToSubscript with a list of functions with protected heads:

In[5]:=
ResourceFunction["SymbolToSubscript", ResourceVersion->"2.0.0"][{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 SymbolToSubscript with the default form for the ith constant C[i]:

In[6]:=
ResourceFunction["SymbolToSubscript"][{C1, C2}]
Out[6]=

Use SymbolToSubscript with a symbolic matrix:

In[7]:=
A = {{x11, x12}, {x21, x22}};
MatrixForm@ResourceFunction["SymbolToSubscript"][#] &@A
Out[8]=

Use SymbolToSubscript with a tensor of rank 3:

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

Use SymbolToSubscript with a tensor of rank 4:

In[10]:=
MatrixForm@
 ResourceFunction["SymbolToSubscript"]@
  TensorProduct[{{x1, x2}, {x3, x4}}, {{x5, x6}, {x7, x8}}]
Out[10]=

If the expression contains symbols with a capital C, the format for the other indexed symbols is the same as ith constant C[i]:

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

Use SymbolToSubscript with a nonlinear system of ordinary differential equations:

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

Use SymbolToSubscript with a system of partial differential equations:

In[13]:=
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[13]=

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

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

Applications (3) 

Format a symbolic polynomial:

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

Format a symbolic matrix:

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

Use SymbolToSubscript and TeXForm:

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

Define a function for making a Vandermonde matrix:

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

Format a Vandermonde matrix:

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

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

In[24]:=
MatrixForm@vandermondeMatrix[x, 5]
Out[24]=
In[25]:=
Factor@Det[vandermondeMatrix[x, 5]]
Out[25]=
In[26]:=
TeXForm@MatrixForm@
  Map[ResourceFunction["SymbolToSubscript"], vandermondeMatrix[x, 5]]
Out[26]=
In[27]:=
ResourceFunction["SymbolToSubscript"]@
 Factor@Det[vandermondeMatrix[x, 5]]
Out[27]=
In[28]:=
TeXForm@ResourceFunction["SymbolToSubscript"]@
  Factor@Det[vandermondeMatrix[x, 5]]
Out[28]=

Properties and Relations (2) 

Use SymbolToSubscript with the resource function HurwitzMatrix:

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

Use SymbolToSubscript with the resource function SolutionRulesToFunctions:

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

Possible Issues (2) 

Before getting the subscript format, SymbolToSubscript separates the expression x1y2 as {"x","1y2"}, and then passes each part to input form using ToExpression. Therefore, with the expression x1y2, we get x{XMLElement[span, {class -> stylebox}, {XMLElement[i, {class -> ti}, {y}], 2}]} instead of x1y2:

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

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

In[35]:=
ResourceFunction["SymbolToSubscript"][x1y2, "ToStringFormat"]
Out[35]=
In[36]:=
TeXForm[%]
Out[36]=

Publisher

Ramón Eduardo Chan López

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