I have expression expr = int[1 - x + x^2 - x^3 + x^4 - x^5, x]
and I want to convert it to Latex. But want to replace int
by Integrate
but without evaluating it.
I tried using Format
and that works inside Mathematica. It prints correctly on the screen.
But when using TeXForm
on expr
it ignored the Format
.
Here is MWE
ClearAll[int, x]
Format[int[e_,x_],StandardForm] := Defer[Integrate[e, x]]
expr = int[1-x+x^2-x^3+x^4-x^5,x]
And that what I want in Latex. But when now I do
TeXForm[expr]
it gives
"\text{int}\left(-x^5+x^4-x^3+x^2-x+1,x\right)"
Which compiles as
Instead, I wanted the latex be generated based on the display it shows on the screen from Format
which should be something like
"\int { \left(-x^5+x^4-x^3+x^2-x+1 \right) \, dx }"
so it matches what it shows above on the screen.
I looked at this post Format and TeXForm does not work as expected and tried few answers there, but so far could not make anything work. It is almost 10 years old and many things changed and I was lost of the complexity of answers and what I am supposed to do.
All the expressions I wan to apply this to have the form
int[ .... , x]
Does there exist a simple solution to this problem?
I thought about just parsing int[..., x]
myself to pick up the ...
and the x
using pattern matching, and then converting everything manually to Defer[ Integrate[...,x]]
and then I can do
TeXForm[Defer[Integrate[ 1-x+x^2-x^3+x^4-x^5 , x]]]
And this now works. it gives
\int \left(-x^5+x^4-x^3+x^2-x+1\right) \, dx
But was hoping to avoid parsing it myself and let Format
do it if possible.
V 13.3.1 on windows 10
int
withIntegrate
right before converting toTeXForm
? $$ $$ClearAll[int, x]
expr = int[1 - x + x^2 - x^3 + x^4 - x^5, x]
new = Apply[Inactive[Integrate][#, x] &, expr];
TeXForm[new]
(*\int \left(-x^5+x^4-x^3+x^2-x+1\right)dx*)
$\endgroup$expr // StandardForm // TeXForm
produces your desired output with no changes to your MWE. I am worried I am missing something in the problem though as this seems too simple...maybe I haven't had enough coffee yet. $\endgroup$Format
? Because you can also do something likeint[1 - x + x^2 - x^3 + x^4 - x^5, x] /. int -> Inactive[Integrate] // TeXForm
. That makes it so that you don't manually have to do copies and replace, and of course you can always package that into your owntExfORM[expr_] := expr /. int -> Inactive[Integrate] // TeXForm
. Alternatively, perhaps you can just setint = Inactive[Integrate]
and do whatever algebraic manipulations you need to do, then justTeXForm
at the end. $\endgroup$TeXForm[StandardForm[expr]]
works. If I leaveStandardForm
form, then it does not work. Thanks. $\endgroup$Format
before and this is first time I try it. $\endgroup$