How to stop Mathematica from deriving index? [duplicate]

If I have two variables $$k, k_x$$ with $$k_x\neq f(k)$$, it is obvious that $$\frac{\partial k_x}{\partial k}=0$$ In Mathematica, I need the Subscript[] function to implement is derivative in an aesthetic way:

This leads to the issue seen above. How to get rid of Mathematica's interpretation of the Subscript[] function - considering it to be a mathematical function that has to be derived too but as a "stylistic device"?

PS: The desired solution should be $$=0$$.

• TIP: never use Subscripts. May 27, 2019 at 15:01
• Haha, thanks. But I want to use TeXForm for LaTeX output and therefore, It would be very comfortable to have it already in the right style/form. Subscripts are well translated into LaTeX subscripts. However, can you help me with that? May 27, 2019 at 15:08
• – kglr
May 27, 2019 at 15:10
• @Kutsubato Do all your calculations without subscripts, and substitute them back only at the very end, when exporting them to latex. May 27, 2019 at 15:16
• I wouldn't say never use Subscript ... but if you do use Subscript[a,b] then do not use a and b separately. This will eventually lead to problems. May 27, 2019 at 19:02

This can be solved by applying custom formatting to kx rather than using Subscript:

kx /: MakeBoxes[kx, TraditionalForm | StandardForm] := SubscriptBox["k", "x"];


Then kx will be output as $$k_x$$, and the derivative will be correct:

D[kx, k]


0

Would highly recommend looking at TagSet (/:) and MakeBoxes in the documentation for further information.

Due to the TraditionalForm used in the MakeBoxes pattern, this will also work with TeXForm:

TeXForm[kx]


k_x

@Roman also suggested Format as an alternative, so to provide some clarification there:

Format[kx] := Subscript[k, x];


By assigning Format[_], the output format of _ can be changed. It's a bit less excessively low-level, as Roman comments. In my own personal experience with Mathematica, it's almost a bit less intuitive, but your mileage will vary. This also works properly with TeXForm.

Regarding OverDot, that is also possible in these cases. For example, with v:

Format[v] := OverDot[v];


Or:

v /: MakeBoxes[v, TraditionalForm | StandardForm] := OverscriptBox["v", "."];

• Thank you. That should work for pretty much all cases! May 27, 2019 at 15:21
• Is there an equivalent for the OverDot function? May 27, 2019 at 15:30
• I find assigning values to Format a bit less low-level than tag-setting MakeBoxes. May 27, 2019 at 15:36
• @Roman TRUE! I looked it up and it works for me as well. Furthermore, it covers solutions for my problem with other operators like OverDot. Thanks to all of you. May 27, 2019 at 15:55
• @Roman Fair. I'm just personally more familiar with the super low-level way of doing things, so thanks for the note. @Kutsubato I included an implementation for OverDot with MakeBoxes as well. MakeBoxes is quite powerful, but definitely somewhat trickier to use. Format should handle 99% of normal formatting needs, for sure. May 27, 2019 at 16:42

Thanks to all contributions. According to @Roman, the use of Format is possible as well. I did it like this and it worked:

Format[cv] := Subscript[c, v];
Format[lv] := Subscript[l, v];
Format[Jz] := Subscript[J, z];
Format[beta] := \[Beta];
Format[psidot] := OverDot[\[Psi]];


This is my preamble - so it comes right before all other code. In the end, after I have applied calculations to the variables inside the Format function, I can use TeXForm to turn them into LaTeX code and in this code, my format options are already applied.