3
$\begingroup$

Goal

Let the TeXForm of D and Integrate more pretty.

D[f[x], x] // HoldForm // TeXForm
D[f[x], {x, n}] // HoldForm // TeXForm
Integrate[f[x], {x, 0, Infinity}] // HoldForm // TeXForm
Integrate[f[x, y], x, y] // HoldForm // TeXForm

\begin{array}{*3{>{\displaystyle}l}} \text{Mathematica} & \LaTeX&\texttt{Problem}\\ \displaystyle\frac{\partial f(x)}{\partial x} & \displaystyle\frac{\mathrm{d}}{\mathrm{d}x}f(x)&\texttt{don't use \partial}\\ \displaystyle\frac {\partial^nf (x)} {\partial x^n} & \displaystyle\frac{\mathrm{d}^n}{\mathrm{d}x^n}f(x)&\texttt{don't put on numerator}\\ \displaystyle\int_0^{\infty } f(x) \, dx& \displaystyle\int_0^{\infty } f(x) \,\mathrm{d}x&\texttt{use\mathrm{d}}\\ \displaystyle\int \int f(x,y)dydx& \displaystyle\iint f(x,y)\,\mathrm{d}y\,\mathrm{d}x&\texttt{use\iint and why\,lost}\\ \end{array}


I tried @jkuczm 's TeXUtilities package.

Unprotect[Integrate,D];
Format[Integrate[fun__,x_],TeXForm]:=TeXDelimited["\\int{",fun,"\\,\\mathrm{d} ",x,"\\right)","DelimSeparator"->""];
Format[D[fun__,x_],TeXForm]:=TeXDelimited["\frac{\mathrm{d}}{\mathrm{d} ,"x ",},"fun,"DelimSeparator"->""];
Protect[Integrate,D];

Nothing happened, can't figure out how to use that.

$\endgroup$
  • $\begingroup$ By Nothing happened, do you mean that after trying TeXForm again, it looked exactly the same? $\endgroup$ – numbermaniac Dec 9 '17 at 5:30
  • $\begingroup$ There is something you are not showing or you are using different version of something. I tried the same code you have, and M gives an error. on 11.2, windows. screen shot !Mathematica graphics Are you sure you did not hear a beep? what version of M are you using? $\endgroup$ – Nasser Dec 9 '17 at 6:00
  • 1
    $\begingroup$ FYI D is a partial derivative. For the full derivative use Dt $\endgroup$ – LLlAMnYP Dec 9 '17 at 9:31
  • $\begingroup$ Except for the lost of /, in the last integral and the use of \int\int instead of \iint, ver 11.2 provides the correct TeX commands. I have pasted them in my LaTeX usual editor and looks nice. I use MacTeX TeX-Live 2015. $\endgroup$ – José Antonio Díaz Navas Dec 9 '17 at 11:02
3
$\begingroup$

We must remember to use HoldPattern to prevent evaluation of D[...] expression used on left hand side of definition. To make D formatting general we need some preprocessing of variable specification arguments to count total order of derivative.

Import@"https://raw.githubusercontent.com/jkuczm/MathematicaTeXUtilities/master/NoInstall.m"

Unprotect@D;
Format[HoldPattern@D[f_, vars__, OptionsPattern[]], TeXForm] := Module[{n = 0, v},
  v = Replace[HoldComplete@vars, {
    {x_, i_, ___} /; (n += i; True) :> HoldForm@HoldForm[x]^i,
    x_ /; (n++; True) :> HoldForm@HoldForm[x]
  }, {1}];
  TeXDelimited[
    "\\frac{" <> ToString[TeXVerbatim@"\\mathrm{d}"^n, TeXForm] <> "}{",
    Sequence @@ v,
    "} " <> ToString[Unevaluated@f, TeXForm],
    "BodyConverter" -> ("\\mathrm{d}" <> ToString[#, TeXForm]&),
    "BodySeparator" -> " \\, ",
    "DelimSeparator" -> ""
  ]
]
Protect@D;

Basic example:

D[f[x], x] // HoldForm // TeXForm
(* \frac{\mathrm{d}}{\mathrm{d}x} f(x) *)

$\frac{\mathrm{d}}{\mathrm{d}x} f(x)$

Complicated example with variables with assigned values to test potential evaluation leaks:

x = 10; y = 20; f = Plus;
D[f[x, y, z], {x, n}, y, {z, 2 + m}] // HoldForm // TeXForm
ClearAll[x, y, f]
(* \frac{\mathrm{d}^{3+m+n}}{\mathrm{d}x^n \, \mathrm{d}y \, \mathrm{d}z^{2+m}} f(x,y,z) *)

$\frac{\mathrm{d}^{3+m+n}}{\mathrm{d}x^n \, \mathrm{d}y \, \mathrm{d}z^{2+m}} f(x,y,z)$


For Integrate we must make sure that our custom format value takes precedence over built-in formatting, so we move it to beginning of FormatValues. Here I'll limit custom formatting to indefinite integrals with up to four variables, but of course more general rules can be defined.

Unprotect@Integrate;
Format[Integrate[fun_, vars : Repeated[_Symbol, {1, 4}]], TeXForm] := With[
  {v = HoldForm /@ HoldForm /@ HoldComplete@vars},
  TeXDelimited[
    "\\" <> StringRepeat["i", Length@v] <> "nt",
    Unevaluated@fun,
    Sequence @@ v,
    "",
    "DelimSeparator" -> " ",
    "BodySeparator" -> " \\, \\mathrm{d}"
  ]
]
(* Move TeXForm format to begining of FormatValues so they take precedence over existing ones. *)
FormatValues@Integrate = Join[#@False, #@True]&@GroupBy[FormatValues@Integrate, FreeQ@TeXForm];
Protect@Integrate;

Basic example:

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

$\int f(x) \, \mathrm{d}x$

Complicated example with variables with assigned values to test potential evaluation leaks:

x = 10; y = 20; f = Plus;
Integrate[f[x, y, z], x, y, z] // HoldForm // TeXForm
ClearAll[x, y, f]
(* \iiint f(x,y,z) \, \mathrm{d}x \, \mathrm{d}y \, \mathrm{d}z *)

$\iiint f(x,y,z) \, \mathrm{d}x \, \mathrm{d}y \, \mathrm{d}z$

$\endgroup$

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.