I have stumbled over several issues about subscripted symbols recently. I would like that I can enter subscripted variables with my keyboard and that they are treated as symbols. I would also like to have lists (matrices) of these subscripted symbols and that they are formatted in the output nicely so that I can do calculations which them and still be able to see the nice output.

So far I have come up with the following (which uses the Notation Package):


Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]
Symbolize[ParsedBoxWrapper[SubsuperscriptBox["_", "_", "_"]]]

makeSpecialSymb[var_String, i_, j_] := 
  ToExpression[SubscriptBox[var, ToString[i], ToString[j]]];

makeSpecialSymb[var_SubscriptBox, i_, j_] := 
    var[[2]] <> "," <> ToString[i] <> "," <> ToString[j]]];

makeSpecialSymb[var_SubsuperscriptBox, i_, j_] := 
    var[[2]] <> "," <> ToString[i] <> "," <> ToString[j], var[[3]] ]];

SymbolicMatrix[func_Function, m_Integer, n_Integer] :=
  Table[func[i, j], {i, 1, m}, {j, 1, n}];

SymbolicMatrix[func_Function, m_Integer] :=
  Table[func[i], {i, 1, m}, {j, 1, 1}];

SymbolicMatrix[argSpecSymb_, m_Integer, n_Integer] :=
  Table[makeSpecialSymb[argSpecSymb, i, j], {i, 1, m}, {j, 1, n}];

SymbolicMatrix[argSpecSymb_, m_Integer] :=
  Table[makeSpecialSymb[argSpecSymb, i], {i, 1, m}, {j, 1, 1}];

So i can do the following:

A = Map[HoldForm, SymbolicMatrix["W", 2, 2], 2];
B = SymbolicMatrix[SubsuperscriptBox["G", "S", "k"], 2, 2] ;
A // FullForm
B // MatrixForm

enter image description here

So everything works fine, I can make a Matrix out of Symbols which are defined in the global context and which get formatted really nice. This is cool!

Another approach would be something (from Mike), where we do not generate the symbols but rather use some sort of formatting expression like the following:


SetAttributes[fancySubscript1, HoldFirst]

fancySubscript1 /: 
 MakeBoxes[fancySubscript1[var_, index_], StandardForm] := 
 SubscriptBox[MakeBoxes[var, StandardForm], 
  StyleBox[RowBox[{MakeBoxes[var, StandardForm], 
     MakeBoxes[index, StandardForm]}], ZeroWidthTimes -> True]]

fancySubscript1[var_, indices : {_Integer ..}] := 
 fancySubscript1[var, #] & /@ indices

fancySubscript[H, {{1, 2}, {3, 4}}]

Which gives: enter image description here

If I would go with this method, which prints all values very nicely, I can make matrices which do not contain symbols but rather function expression. In this method I am probably also much more flexible when I want to define my own look how the individual fancySubscripts are formatted.

Which version would you go for in which cases? I am a bit unsure, which one of these is better to use, also with respect to flexibility. Also with respect to what method is better to use for if I want to replace the variables with values in subsequent calculations (either by replacement rules or by Set[] ) ?

Another question for method 1, how does mathematica decide how to display my crazy named variables like: W\[UnderBracket]Subscript\[UnderBracket]1 in the output? It seems that all symbols, which get created by the ParsedBoxWrapper which I think gets called when ever it sees a SubscriptBox pattern as defined in the Symbolize command, are displayed nicely. If I enter RR\[UnderBracket]Subscript\[UnderBracket]100 in a new notebook it does not immediately get converted to a nice subcripted output format? Whats the problem here?

I would be really interessted what the caveats are in these two methods, because I am not kind of a hero with mathematica.


  • 1
    $\begingroup$ It seems to me like the Notation package lets $PreRead parse your custom notations and interprets your custom symbols before sending to the kernel. If your code generates a subscripted expression in the kernel, then $PreRead doesn't have a chance to convert it before the kernel passes it on to the next function. Similarly, I think $PrePrint converts the crazy named variables into your pretty notation. $\endgroup$ – Timothy Wofford Nov 15 '13 at 16:07
  • 1
    $\begingroup$ When you use Symbolize the rules are added to the $PreRead and $PrePrint definitions, so the crazy names that you enter directly don't get caught by the Symbolize pattern, so the $PreRead and $PrePrint rules don't get created. $\endgroup$ – Timothy Wofford Nov 15 '13 at 16:08
  • 8
    $\begingroup$ The best practice to using Subscripts is to not use them =) $\endgroup$ – rm -rf Nov 15 '13 at 23:39
  • 3
    $\begingroup$ I must admit not having read the whole post, so sorry if this isn't appropriate. I would suggest symbolizing the subscripts only if you are really in need of them being treated as symbols. Otherwise, use some private head and make it format as subscript. $\endgroup$ – Rojo Nov 16 '13 at 5:45
  • 2
    $\begingroup$ Some issues with subscripts are discussed in these answers to the Pitfalls question and links contained in them: (1), (2) $\endgroup$ – Michael E2 Nov 16 '13 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.