# How can I make MMA give me \mathrm{d} when converting definite integrals into Latex code?

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?

• Harder than I thought. Following instructions under this and this post, I tried modifying SystemConvertTeXFormDumpmaketex and SystemConvertTeXFormDump\$Operators, but none of them seems to work. Looking forward to an answer. Commented Apr 20, 2022 at 17:33
• could you post a MWE? Commented Apr 20, 2022 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. Commented Apr 21, 2022 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 Commented Apr 22, 2022 at 12:59
• You may want to read this: mathematica.meta.stackexchange.com/q/1162/1871 Commented Apr 22, 2022 at 13:01

Simple enough as mentioned in the question:

SystemConvertTeXFormDumpmaketex["\[DifferentialD]"]="\\mathrm{d}";


should do the trick. And it actually does

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


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:

GeneralUtilitiesDefinitions@SystemConvertTeXFormDumpmaketex;
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[SystemConvertTeXFormDumpmaketex[RowBox[{\[Integral], RowBox[{SystemConvertTeXFormDumpintegrand_, RowBox[{\[DifferentialD], SystemConvertTeXFormDumpvar_}]}]}]]] :> \int <>SystemConvertTeXFormDumpMakeTeX[SystemConvertTeXFormDumpintegrand]<> \, d<>SystemConvertTeXFormDumpMakeTeX[SystemConvertTeXFormDumpvar]
HoldPattern[SystemConvertTeXFormDumpmaketex[RowBox[{SubsuperscriptBox[\[Integral], SystemConvertTeXFormDumpsub_, SystemConvertTeXFormDumpsuper_, ___], RowBox[{SystemConvertTeXFormDumpintegrand_, RowBox[{\[DifferentialD], SystemConvertTeXFormDumpvar_}]}]}]]] :> \int_<>SystemConvertTeXFormDumpMakeScript[SystemConvertTeXFormDumpsub]<>^<>SystemConvertTeXFormDumpMakeScript[SystemConvertTeXFormDumpsuper]<> <>SystemConvertTeXFormDumpMakeTeX[SystemConvertTeXFormDumpintegrand]<> \, d<>SystemConvertTeXFormDumpMakeTeX[SystemConvertTeXFormDumpvar]


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

SystemConvertTeXFormDumpmaketex[RowBox[{"\[Integral]",RowBox[{integrand_,RowBox[{"\[DifferentialD]",var_}]}]}]]:="\\int "<>SystemConvertTeXFormDumpMakeTeX[integrand]<> "\\," SystemConvertTeXFormDumpMakeTeX["\[DifferentialD]"]<>SystemConvertTeXFormDumpMakeTeX[var]

SystemConvertTeXFormDumpmaketex[RowBox[{SubsuperscriptBox["\[Integral]", sub_,super_,___], RowBox[{integrand_, RowBox[{"\[DifferentialD]", var_}]}]}]] :="\\int_"<>SystemConvertTeXFormDumpMakeScript[sub]<>"^"<>SystemConvertTeXFormDumpMakeScript[super]<>" "<>SystemConvertTeXFormDumpMakeTeX[integrand]<> "\\,"<>SystemConvertTeXFormDumpMakeTeX["\[DifferentialD]"]<>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:

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:

SystemConvertTeXFormDumpmaketex["\[DifferentialD]"]="\\mathrm{d}";
SystemConvertTeXFormDumpmaketex["I"]="\\mathrm{i}";
SystemConvertTeXFormDumpmaketex["E"]="\\mathrm{e}";
SystemConvertTeXFormDumpmaketex["\[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}*)

• 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 ? Commented Apr 21, 2022 at 13:46
• Please file a bug report for this (if you have not already done so). Commented Apr 21, 2022 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
Commented Apr 21, 2022 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
Commented Apr 21, 2022 at 15:48
• 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. Commented Apr 21, 2022 at 16:55