3

Recently my work contains a lot of expressions with definite integrals, and I need to convert them into latex code to present my results. To my disappointment, MMA always converts the differential operator \[DifferentialD] in those integrals into normal italic $d$, which does not conform to the regulations I need to obey.

In pure mathematics literature, using such normal italic $d$ notation for differential operator is common, but for physics and engineering literature, normal italic $d$ notation may already stand for a quantity or variable like distance or thickness, which has to be distinguished from the differential operator \[DifferentialD],as a result I need to use "\mathrm{d}" or $\mathrm{d}$ to represent the differential operator in integrals.

I have already tried to modify the built-in Tex rules to achieve the "\mathrm{d}" in integrals. But unfortunately, it works when the expr is a pure differential. As long as it comes to a definite integral, it fails, which has given me a feelilng that the definite integral is parsed as whole in MMA and when doing so MMA ignores my self-introduced Tex rules.

My habitual way to generate latex code from MMA is right-click->Copy as->Latex. But I also welcome method that fulfill my needs by generating the latex code some other way in MMA.

Any ideas to achieve so?

xzczd
  • 65,995
  • 9
  • 163
  • 468
AlbertLew
  • 483
  • 2
  • 7
  • Harder than I thought. Following instructions under this and this post, I tried modifying System`Convert`TeXFormDump`maketex and System`Convert`TeXFormDump`$Operators, but none of them seems to work. Looking forward to an answer. – xzczd Apr 20 '22 at 17:33
  • could you post a MWE? – Nasser Apr 20 '22 at 18:07
  • I agree with @Nasser . It's better to add corresponding code sample regarding to e.g. "I have already tried to modify the built-in Tex rules to achieve the "\mathrm{d}" in integrals…" That'll make the question more attractive. – xzczd Apr 21 '22 at 13:50
  • Thanks @Nasser, but I am not sure what you meant by "MWE", so I do not quite know what you really wanted here – AlbertLew Apr 22 '22 at 12:59
  • You may want to read this: https://mathematica.meta.stackexchange.com/q/1162/1871 – xzczd Apr 22 '22 at 13:01

1 Answers1

4

Simple enough as mentioned in the question:

System`Convert`TeXFormDump`maketex["\[DifferentialD]"]="\\mathrm{d}";

should do the trick. And it actually does

"\[DifferentialD]"//TeXForm (* -> \mathrm{d} *)

plain dd

But curiously

Integrate[f[x],x]//StandardForm
%//TeXForm (* -> \int f(x) \, dx *)

does not yield the expected result even though the StandartForm (boxed Mathematica representation) clearly uses \[DifferentialD]. The reason why the TeXForm is incorrect here is as OP guessed a problem and to be frank just a lazy oversight of the programmers of Mathematica:

GeneralUtilities`Definitions@System`Convert`TeXFormDump`maketex;
Select[ToString[#]&/@%,StringContainsQ[#,"integrand"]&];
%//TableForm

lets us look under the hood (the default tool of choice PrintDefinitions from GeneralUtilities does not work because it only prints when there are less then 257 definitions). From the 1059 definitions (in version 13) of maketex we select the ones which involve the string "integrand" and we get

HoldPattern[System`Convert`TeXFormDump`maketex[RowBox[{\[Integral], RowBox[{System`Convert`TeXFormDump`integrand_, RowBox[{\[DifferentialD], System`Convert`TeXFormDump`var_}]}]}]]] :> \int <>System`Convert`TeXFormDump`MakeTeX[System`Convert`TeXFormDump`integrand]<> \, d<>System`Convert`TeXFormDump`MakeTeX[System`Convert`TeXFormDump`var]
HoldPattern[System`Convert`TeXFormDump`maketex[RowBox[{SubsuperscriptBox[\[Integral], System`Convert`TeXFormDump`sub_, System`Convert`TeXFormDump`super_, ___], RowBox[{System`Convert`TeXFormDump`integrand_, RowBox[{\[DifferentialD], System`Convert`TeXFormDump`var_}]}]}]]] :> \int_<>System`Convert`TeXFormDump`MakeScript[System`Convert`TeXFormDump`sub]<>^<>System`Convert`TeXFormDump`MakeScript[System`Convert`TeXFormDump`super]<> <>System`Convert`TeXFormDump`MakeTeX[System`Convert`TeXFormDump`integrand]<> \, d<>System`Convert`TeXFormDump`MakeTeX[System`Convert`TeXFormDump`var]

with the critical segment ...\, d<>System.... They clearly did not account for the option to properly set a TeXForm for \[DifferentialD]. Related to this is this discussion What's the proper way to typeset a differential operator? on wether differential operators (and in this context mathematical constants like the imaginary unit, Eulers constant and even Pi) should be typeset upright. On a personal note: I like them upright (all of them including \uppi see this) to clearly differentiate between operators/constant and variables. Coming back from my ramblings to the problem. Lets fix the two functions

System`Convert`TeXFormDump`maketex[RowBox[{"\[Integral]",RowBox[{integrand_,RowBox[{"\[DifferentialD]",var_}]}]}]]:="\\int "<>System`Convert`TeXFormDump`MakeTeX[integrand]<> "\\," System`Convert`TeXFormDump`MakeTeX["\[DifferentialD]"]<>System`Convert`TeXFormDump`MakeTeX[var]

SystemConvertTeXFormDumpmaketex[RowBox[{SubsuperscriptBox[&quot;\[Integral]&quot;, sub_,super_,___], RowBox[{integrand_, RowBox[{&quot;\[DifferentialD]&quot;, var_}]}]}]] :=&quot;\\int_&quot;&lt;&gt;SystemConvertTeXFormDumpMakeScript[sub]<>"^"<>SystemConvertTeXFormDumpMakeScript[super]&lt;&gt;&quot; &quot;&lt;&gt;SystemConvertTeXFormDumpMakeTeX[integrand]<> "\,"<>SystemConvertTeXFormDumpMakeTeX[&quot;\[DifferentialD]&quot;]&lt;&gt;SystemConvertTeXFormDumpMakeTeX[var]

which fixes the issue for Integrate

Integrate[f[x],x]//StandardForm
%//TeXForm (* -> \int f(x)\, \mathrm{d}x *)

and

Integrate[f[x], {x, 0, 1}] // StandardForm
% // TeXForm  (* -> \int_0^1 f(x)\,\mathrm{d}x*)

both work as expected:

Fixed

as do multidimensional integrals. For occurrences of \[DifferentialD] outside of Integrate further modifications along this line will most likely be necessary. I personally would also consider to add a thin space \, or \mkern2mu between the differential operator and the variable.

The last side note related to earlier ramblings:

System`Convert`TeXFormDump`maketex["\[DifferentialD]"]="\\mathrm{d}";
System`Convert`TeXFormDump`maketex["I"]="\\mathrm{i}";
System`Convert`TeXFormDump`maketex["E"]="\\mathrm{e}";
System`Convert`TeXFormDump`maketex["\[Pi]"]="\\uppi";

leads to the proper typesetting of Euler's formula (the solution for Pi is not ideal but it normally only appears in Mathematica as the mathematical constant)

Exp[I Pi x] // StandardForm
% // TeXForm (* -> \mathrm{e}^{\mathrm{i}\uppi x}*)
N0va
  • 3,370
  • 11
  • 16
  • Interesting. It's worth mentioning that before re-defining maketex[…] it's necessary to execute e.g. TeXForm[1] once. BTW, do you have any idea about how to fix right-click->Copy as->LaTeX ? – xzczd Apr 21 '22 at 13:46
  • Please file a bug report for this (if you have not already done so). – Daniel Lichtblau Apr 21 '22 at 15:01
  • @xzczd I did not realize that but important to know. Regarding your question about Copy as -> LaTeX: I do not know how to do this. There are some questions and answers in this direction on this page but I was unable to get it to work. Interestingly one can completely clear all definitions from maketex and Copy as -> LaTeX still works, while TeXForm no longer does. I have no idea how to get them to work consistently. – N0va Apr 21 '22 at 15:41
  • @DanielLichtblau Technically this is not a bug (I suppose) but rather a missing feature. They never intended to use a custom form for \[DifferentialD]. The whole LaTeX formatting code/functionality seems a bit hacked together to be honest and they should probably overhaul it. But I have the feeling this is not a priority since at least Stephen Wolfram pushes for the idea to have everything in Mathematica/Notebooks. This is in my opinion also the reason for other poorly implemented export functions. Like exporting plots as pdfs. – N0va Apr 21 '22 at 15:48
  • Daniel is a member of WRI, so… :) – xzczd Apr 21 '22 at 16:03
  • 2
    To be sure, there are numerous problems with TeXForm and with exporting (also TeXForm may well be both dated and not well designed, I'm not familiar with the history). Some issues I myself have raised in house. While these have not attained a level of high priority, they are no longer as far down as might have once been the case. And the CEO is nowadays well aware that they have an impact. – Daniel Lichtblau Apr 21 '22 at 16:55
  • Thanks @N0va. Your answer has shed enormous light on my problem. And the illumination from your answer has enabled me to fulfill my needs but with an unexpected phenomenon observed. – AlbertLew Apr 22 '22 at 13:32
  • Since there are too many clicks to generate the code by right-clilck->Copy As->LaTex, I have modified the initial file with the code MenuItem["Plain &Text", FrontEndCopySpecial["PlainText"]], MenuItem["&LaTeX", KernelExecute[ToExpression["FrontEndCopyAsTeX[]"]], MenuKey["C", Modifiers->{"Control", "Shift"}], MenuEvaluator -> Automatic] . By that way my actual method to generate Latex code is Ctr+Shift+C, which is much more convenient. And from my previous experience, the ways to modify functional output TeXForm[ ] don't work at all with my Ctr+Shift+C way. – AlbertLew Apr 22 '22 at 13:42
  • From my code you can see, the Ctr+Shift+C way works because it sets up a connection between the input alias and the MenuItem["&LaTeX", KernelExecute[ToExpression["FrontEndCopyAsTeX[]"]]`. As a result, I had observed the way which works to modify the Ctr+Shift+C output also works for right-click->Copy As->Latex and vise versa. To make my post succinct, I always mentioned my way to do so as right-click->Copy As->Latex, omitting verbose details related to initial files modification. And thanks to your inspiration, I have just managed to make my Ctrl+Shift+C way works to generate the upright d. – AlbertLew Apr 22 '22 at 14:00
  • You can reference the 2nd answer in https://mathematica.stackexchange.com/a/132069 about how to manage the CopyAs > LaTeX. But an unexpected phenomenon has occurred this time: the way to have Ctr+Shitf+C successfully generate upright d has no impact on right click->Copy As-> Latex , which has made me realized that the MenuItem["&LaTeX", KernelExecute[ToExpression["FrontEndCopyAsTeX[]"]]` are not always tied together to right click->Copy As-> Latex, but probably linked to menu-bar command Edit > CopyAs > LaTeX on my Windows although Michael reported the reverse result on his Mac in that post. – AlbertLew Apr 22 '22 at 14:20
  • @AlbertLew Which version are you in? How do you set BoxRules? On v12.3.1, win10, after setting SetOptions[Convert`TeX`BoxesToTeX, "BoxRules" -> (System`Convert`TeXFormDump`$Operators /. ("\[DifferentialD]" -> "d") -> ("\[DifferentialD]" -> "\\mathrm{d}"))], Edit > CopyAs > LaTeX doesn't work, either. (I haven't modified MenuItem[…], but this shouldn't be a problem? ) – xzczd Apr 22 '22 at 15:42
  • @xzczd. Definitely. Actually our MMA version and OS are identical but after my modification, both the Edit > CopyAs > LaTeX and Ctr+Shift+C work like a charm. I did not adopt the way you mentioned. Instead, I set an additional box rules RowBox[{"\[Integral]", RowBox[{integrand_, RowBox[{"\[DifferentialD]", var_}]}]}] :> "\\int " <> ConvertTeXBoxesToTeX[integrand] <> "\\mathrm{d}" <> ConvertTeXBoxesToTeX[var] into built-in ones. – AlbertLew Apr 23 '22 at 06:19
  • @xzczd Reasons your way does not work might be:1. You should not only set the rules for the mere "[DifferentialD]" only, it won't work as I tried it before. In addition to the mere symbol d rule, you have to set rules for the complete box structure of the univariate integral like N0va does in his example. Otherwise MMA will ignore your d rule since the integrals are pasesed as a whole. – AlbertLew Apr 23 '22 at 06:26
  • @xzczd The 2nd place where your method is problematic: One of the annoying features of TeXForm is that "\ConvertTeXExpressionToTeX " overrides the default "BoxRules" of "\ConvertTeXBoxesToTeX " by setting it equal to "\SystemConvertTeXFormDump$GreekWords" , so you have to hook into it rather than "$Operators" or anywhere else to get TeXForm recognizing your self-defined TeX rules. Finally, to make the Edit->Copy->Latex work, an elegant line of code SetOptions[ConvertTeXBoxesToTeX, "BoxRules" -> SystemConvertTeXFormDump$GreekWords];` will do the trick. – AlbertLew Apr 23 '22 at 06:36
  • @AlbertLew You can wrap the code with two backticks e.g. ``aaa`bbb`` to format it properly. So, you've added the mentioned rule to System`Convert`TeXFormDump`$GreekWords? Perhaps you can add an answer to elaborate about how to fix FrontEnd`CopyAsTeX[]? – xzczd Apr 23 '22 at 08:03
  • Thanks @xzczd. It annoyed me quite a bit with the formatting problem of the code with inherent backticks, I never thought that two back-ticks will do the trick. In terms of managing the FrontEnd`CopyAsTeX[], actually it is not my original idea, it is @Michael E2 's. So I'd better reference Michael's answer here rather than add my own post after Nova's. – AlbertLew Apr 23 '22 at 13:15