0

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

user64494
  • 26,149
  • 4
  • 27
  • 56
Nasser
  • 143,286
  • 11
  • 154
  • 359
  • 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*)

    – ydd Nov 21 '23 at 16:23
  • 1
    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. – ydd Nov 21 '23 at 16:32
  • 1
    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. – march Nov 21 '23 at 16:35
  • @ydd You are right, I have not tried that. TeXForm[StandardForm[expr]] works. If I leave StandardForm form, then it does not work. Thanks. – Nasser Nov 21 '23 at 16:39
  • @march you are right, that is also good workaround. I never used Format before and this is first time I try it. – Nasser Nov 21 '23 at 16:40
  • TeXForm uses TraditionalForm formatting, so define Format[int[e_,x_],TraditionalForm] := Defer[Integrate[e, x]] instead – Carl Woll Nov 21 '23 at 17:07
  • @ydd FYI, I found standardForm breaks things. See this screen shot Mathematica graphics you see how it does not translate correctly when using standardForm in this example! I am on 13.3 windows 10. – Nasser Dec 04 '23 at 03:45

0 Answers0