3
$\begingroup$

Background

I'm trying to programmatically generate a subscripted variable in a way that makes it appear "pretty". Here is a minimal working example that shows some strange behavior I'm trying to understand and correct for (picture of NB, code):

Image of Code

minimal-working-example

Copy-Pasted Code

(*fullform syntax*)
Subscript[s, 1] // FullForm

(*symbolize*)
<< Notation`
Symbolize[
ParsedBoxWrapper[
SubscriptBox["s", "_"]]] // Once

(*recheck fullform syntax*)
Subscript[s, 2] // FullForm (*syntax has changed*)
s\[UnderBracket]Subscript\[UnderBracket]2 (*pretty output*)
s\[UnderBracket]Subscript\[UnderBracket]3 (*not pretty output*)

Question

So, why does a symbolized, subscripted variable have to be input using "ctrl-_" and manually evaluated in order to look "pretty?

Additional Comments

In the end, I'm hoping to generate a list of "pretty" subscripted variables and use this list while I'm doing derivations in higher dimensions, but it will only display pretty subscripted variables if I've previously evaluated them. Example:

n = 3; (*# of subscripted variables to generate*)
S = ToExpression[
   ToString[s\[UnderBracket]Subscript\[UnderBracket]] <> 
    ToString[#]] & /@ Range[n] (*vector of subscripted variables, s2 is the only pretty output*)

Related Questions

However, I really want to get this using the Symbolize[] functionality).

Any discussion/suggestions would be much appreciated. Thanks!

$\endgroup$
  • $\begingroup$ Can you specify a clear list of steps and the desired final result? I am having trouble understanding what is the final goal, what exactly did you do and in what order. $\endgroup$ – Kuba Jun 19 at 6:42
  • $\begingroup$ Hi @Kuba, sorry for the confusion. I've updated the question to try to clarify. I reference Mathematica Stack Exchange frequently, but am relatively new to questions, so let me know if you think it needs more revising. $\endgroup$ – Sterling Jun 21 at 1:02
4
$\begingroup$

Assuming I understand your question the short answer is to produce your Symbols like this:

new = ToExpression[SubscriptBox["s", "3"]];

Head[new]

FullForm[new]
Symbol
s\[UnderBracket]Subscript\[UnderBracket]3

The mechanism of the Notation package is to intercept MakeExpression (called by ToExpression) and MakeBoxes. If these definitions do not trigger the package will not work.

The effect of your Symbolize command is a definition on MakeExpression (by way of NotationMakeExpression) that looks for a Box expression that matches SubscriptBox["s", _] and processes it accordingly. When this processing takes place a rule is made on MakeBoxes (by way of NotationMakeBoxes) to handle the UnderBracket form.

If you are interested in these internal mechanisms have a look at:

DownValues[NotationMakeBoxes] // FullForm

DownValues[NotationMakeExpression] // FullForm
| improve this answer | |
$\endgroup$
  • $\begingroup$ Wonderful! Thank you. $\endgroup$ – Sterling Jun 22 at 17:16
  • 1
    $\begingroup$ Not that it's a difficult extension of your answer but just as a reference for others, now I can very simply do: n = 3; S = ToExpression[SubscriptBox["s", ToString[#]]]&/@Range[n] $\endgroup$ – Sterling Jun 24 at 19:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.