3
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – xzczd
    Apr 20 at 17:33
  • $\begingroup$ could you post a MWE? $\endgroup$
    – Nasser
    Apr 20 at 18:07
  • $\begingroup$ 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. $\endgroup$
    – xzczd
    Apr 21 at 13:50
  • $\begingroup$ Thanks @Nasser, but I am not sure what you meant by "MWE", so I do not quite know what you really wanted here $\endgroup$
    – AlbertLew
    Apr 22 at 12:59
  • $\begingroup$ You may want to read this: mathematica.meta.stackexchange.com/q/1162/1871 $\endgroup$
    – xzczd
    Apr 22 at 13:01

1 Answer 1

4
$\begingroup$

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]

System`Convert`TeXFormDump`maketex[RowBox[{SubsuperscriptBox["\[Integral]", sub_,super_,___], RowBox[{integrand_, RowBox[{"\[DifferentialD]", var_}]}]}]] :="\\int_"<>System`Convert`TeXFormDump`MakeScript[sub]<>"^"<>System`Convert`TeXFormDump`MakeScript[super]<>" "<>System`Convert`TeXFormDump`MakeTeX[integrand]<> "\\,"<>System`Convert`TeXFormDump`MakeTeX["\[DifferentialD]"]<>System`Convert`TeXFormDump`MakeTeX[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}*)
$\endgroup$
16
  • $\begingroup$ 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 ? $\endgroup$
    – xzczd
    Apr 21 at 13:46
  • $\begingroup$ Please file a bug report for this (if you have not already done so). $\endgroup$ Apr 21 at 15:01
  • $\begingroup$ @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. $\endgroup$
    – N0va
    Apr 21 at 15:41
  • $\begingroup$ @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. $\endgroup$
    – N0va
    Apr 21 at 15:48
  • 2
    $\begingroup$ 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. $\endgroup$ Apr 21 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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