0
$\begingroup$

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]

Mathematica graphics

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

enter image description here

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

$\endgroup$
7
  • $\begingroup$ I am just adding this as a comment as I think this may not be what you want, but why not replace int with Integrate right before converting to TeXForm? $$ $$ 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$
    – ydd
    Commented Nov 21, 2023 at 16:23
  • 1
    $\begingroup$ Also 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$
    – ydd
    Commented Nov 21, 2023 at 16:32
  • 1
    $\begingroup$ Is there a reason why you want to use Format? Because you can also do something like int[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 own tExfORM[expr_] := expr /. int -> Inactive[Integrate] // TeXForm. Alternatively, perhaps you can just set int = Inactive[Integrate] and do whatever algebraic manipulations you need to do, then just TeXForm at the end. $\endgroup$
    – march
    Commented Nov 21, 2023 at 16:35
  • $\begingroup$ @ydd You are right, I have not tried that. TeXForm[StandardForm[expr]] works. If I leave StandardForm form, then it does not work. Thanks. $\endgroup$
    – Nasser
    Commented Nov 21, 2023 at 16:39
  • $\begingroup$ @march you are right, that is also good workaround. I never used Format before and this is first time I try it. $\endgroup$
    – Nasser
    Commented Nov 21, 2023 at 16:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.