4
$\begingroup$

everyone! I hope to declare a variable with subscript as numbers, for example, $x_{10}$, $x_{11}$, $x_{01}$ as variables. Is it possible in mathematica? If so, how to implement it so that it treats $x_{10}$ as a variable? Thank you!

$\endgroup$
3
  • 1
    $\begingroup$ Hi ! You should add some details to your question, because it is not clear what you are actually trying to do. Add a simple (non)working example showing your goal, etc. $\endgroup$ – Sektor Feb 5 '15 at 21:09
  • 1
    $\begingroup$ Although you can do something like this it is generally advised not to do this. But have a look at the Notation package if you really want this. $\endgroup$ – Sjoerd C. de Vries Feb 5 '15 at 21:52
  • $\begingroup$ Is there some reason why the usual Ctrl + _ won't work? $\endgroup$ – DumpsterDoofus Feb 6 '15 at 1:23
2
$\begingroup$
Format[x[i_, j_]] :=
 Subscript[x, StringJoin[ToString /@ {i, j}]]

t = Total[{x[1, 0], x[1, 1], x[0, 1]}]

enter image description here

Solve[t == 1, x[0, 1]]

enter image description here

$\endgroup$
2
$\begingroup$

Using the 'Notation' package, you can create real symbols from (not only!) subscripted variables:

Needs["Notation`"]

makesymbol[obj_]:=With[{},      
  If[NameQ@ToString@Unevaluated@obj,Remove@obj]; (* remove possibly existing symbol first *)
  Symbolize@ParsedBoxWrapper@ToBoxes@obj;]       (* then create the new symbol *)

With this function, you can create a symbol like so:

makesymbol[Subscript[x,"01"]]

Mathematica will now treat it the same as any other (simpler) symbol.

And the best part is:

  1. You have the same look&feel everywhere, both in your code and the generated output.
  2. It works for all kinds of notation, not only Subscript

I hope this might be of some help to you.

$\endgroup$

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