5
$\begingroup$

This question already has an answer here:

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:

enter image description here

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$.

$\endgroup$

marked as duplicate by Daniel Lichtblau, Szabolcs, MarcoB, m_goldberg, Community May 29 at 6:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ TIP: never use Subscripts. $\endgroup$ – AccidentalFourierTransform May 27 at 15:01
  • $\begingroup$ 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? $\endgroup$ – Kutsubato May 27 at 15:08
  • $\begingroup$ duplicate? : How to treat Subscript objects as constants when using D $\endgroup$ – kglr May 27 at 15:10
  • 1
    $\begingroup$ @Kutsubato Do all your calculations without subscripts, and substitute them back only at the very end, when exporting them to latex. $\endgroup$ – AccidentalFourierTransform May 27 at 15:16
  • 2
    $\begingroup$ 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. $\endgroup$ – Szabolcs May 27 at 19:02
7
$\begingroup$

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", "."];
$\endgroup$
  • $\begingroup$ Thank you. That should work for pretty much all cases! $\endgroup$ – Kutsubato May 27 at 15:21
  • $\begingroup$ Is there an equivalent for the OverDot function? $\endgroup$ – Kutsubato May 27 at 15:30
  • 3
    $\begingroup$ I find assigning values to Format a bit less low-level than tag-setting MakeBoxes. $\endgroup$ – Roman May 27 at 15:36
  • $\begingroup$ @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. $\endgroup$ – Kutsubato May 27 at 15:55
  • 1
    $\begingroup$ @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. $\endgroup$ – eyorble May 27 at 16:42
0
$\begingroup$

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.

$\endgroup$

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