# Mathematica can't differentiate when variable name has subscript component

I am not pasting my input code here since the website modifies my code. Instead I am attaching a screenshot of my notebook.

Issue: I enter two separate commands (asking Mathematica to differentiate in both case). I expected the answers to be '2' and '0' respectively, but it spits out some weird thing in the second case. Please tell me what went wrong?

In short, I expect Mathematica to identify 'x' and 'x_{p}' (where '_' stands for subscript) as two independent variables while performing the differentiation but it looks like it has some trouble in identifying variable name with a subscript component. • This is perhaps the commonest problem associated with subscripts. See this site for multiple explanations – mikado Sep 4 '16 at 18:55
• @mikado, you did not give the website. Please do so. And thanks! – Sashwat Tanay Sep 4 '16 at 19:45
• This site means this site, Mma.SE :) -- Search this site for subscript, Symbolize and the Notation package, for instance. – Michael E2 Sep 4 '16 at 20:04
• Please note that $x_p$ in your second example is not a symbol. Rather, it is an expression that is a function of two separate symbols Subscript[x,p], which merely formats as $x_p$. Therefore, Mathematica uses the chain rule to differentiate, giving the correct result. Never ever EVER use subscript to denote symbols. Use xp instead. – QuantumDot Sep 4 '16 at 23:23
• If you insist on Mathematica interpreting $x_p$ as a totally independent symbol, then use the following two lines: MakeBoxes[xp, StandardForm] := SubscriptBox["x", "p"]; MakeExpression[SubscriptBox["x", "p"], StandardForm] := MakeExpression["xp", StandardForm]. Then in all subsequent evaluations in the session, xp will display as $x_p$, but will be interpreted as xp behind-the-scenes. Only then, would D[$x_p$,x] yield zero as expected. (Try also running FullForm[$x_p$] to understand what's going on). – QuantumDot Sep 4 '16 at 23:31

## 2 Answers

A subscripted symbol is an expression involving the symbol. It is not a new symbol. Here is what you are asking Mma to do:

D[Times[2, Subscript[x, p]], x]


This is an expression in x, so Mma does exactly what you ask it to: it differentiates an expression in x with respect to x.

You can use the Notation package to get $x_p$ treated as a symbol.

Needs["Notation"]
Symbolize[ParsedBoxWrapper[SubscriptBox["x", "p"]]]
D[2*x⎵Subscript⎵p^2, x⎵Subscript⎵p]
4*x⎵Subscript⎵p


This looks rather strange in input form, but in standard form it looks as you would expect it to. Or even more generally, Which is the same as 