Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to directly get the TeXForm of every step? #34

Closed
asukaminato0721 opened this issue May 7, 2020 · 9 comments
Closed

How to directly get the TeXForm of every step? #34

asukaminato0721 opened this issue May 7, 2020 · 9 comments

Comments

@asukaminato0721
Copy link

This is what now I do :

For example:

Int[x Sin[x],x]//Steps

image

click one step, then paste it to .nb, then //TeXForm to get the TeX code. (kind of monkey job)

I've tried FullForm, get nothing, // Trace gives back a lot of things, which seems a bit hard for me to pick out what I want.

So is it possible to add a command to automatically get code like

\begin{aligned}
\int f(x)\,dx&= ......\\
&= ......\\
......
\end{aligned}

to make it easier for following steps? For example , using MaTeX to get nice formula.

@asukaminato0721
Copy link
Author

After some test, this is a half-done job:

Steps[Int[Sin[x]/x^2, x], RubiPrintInformation -> False] //
      Flatten //
     Most //
    Select[Head@# =!= RubiRule &] //
   # /. RubiIntermediateResult[x_] -> x & //
  (*ToString@*TeXForm is also OK*)
  Map["=&" <> (Convert`TeX`ExpressionToTeX[HoldForm @@ #]) <> 
     "\\\\" &] //
 StringRiffle

@asukaminato0721
Copy link
Author

OK, I've done it.(I don't deal with the situations that Rubi failed to calculate the int)

This function returns a string.

SetAttributes[IntWithStepsOfTeXForm, {HoldFirst}];
IntWithStepsOfTeXForm[j_] := 
 With[{TeX2Str = Convert`TeX`ExpressionToTeX},
  Steps[j, RubiPrintInformation -> False] //
         Flatten //
        Most //
       Select[Head@# =!= RubiRule &] //
      # /. RubiIntermediateResult[x_] -> x & //
     (*ToString@*TeXForm is OK*)
     Map["=&" <> (TeX2Str[HoldForm @@ #]) <> "\\\\" &] //
    # /. {a___} -> {
        "\\begin{aligned}",
        TeX2Str@HoldForm@j,
        a,
        "\\end{aligned}"} & // StringRiffle
  ]

Usage:

image

Or

IntWithStepsOfTeXForm[Int[(x + 1)/Sqrt[x^2 + 1], x]] // Print

And then choose and copy to clipboard.

Maybe I will make a pr for this function in the future.

@halirutan
Copy link
Member

Yep, this is exactly what I implemented RubiPrintInformation -> False for so that people can create their own functions for displaying integration steps.

@asukaminato0721
Copy link
Author

asukaminato0721 commented May 10, 2020

I make it better:

IntWithStepsOfTeXForm[formula_, j_]:=
With[{TeX2Str = Convert`TeX`ExpressionToTeX},
Steps[Int[formula, j], RubiPrintInformation -> False] // 
Flatten //
Most // 
Cases[RubiIntermediateResult[x_]:>"=&" <> (TeX2Str[HoldForm @@ x]) <>"\\\\"] //
{"\\begin{aligned}",TeX2Str@HoldForm@Int[formula, j], Sequence @@ #,"\\end{aligned}"} & //
 StringReplace[
  "\\, d" <> ToString[var] -> "\\, \\mathrm{d}" <> ToString[var]]// 
StringRiffle]

Now we can use it just like Integrate

image

@asdasd1dsadsa
Copy link

By the way, I modified it to be another display function which doesn't use TeXForm but TraditionalForm:

IntTraditional[expr_, var_] := Style[Grid[
	{"\[LongEqual]", TraditionalForm@#}& /@ Cases[
		Flatten@First@Steps[Int[expr, var], RubiPrintInformation -> False],
		RubiIntermediateResult[x_] :> HoldForm@@x
	]// Prepend[{"",TraditionalForm@HoldForm@Int[expr,var]}]
, Alignment -> Left], FontFamily -> "Latin Modern Roman 10"];

SetAttributes[IntTraditional, HoldAll]

SyntaxInformation[IntTraditional] = {"LocalVariables" -> {"Integrate", {2, Infinity}}};

Subst /: MakeBoxes[HoldPattern@Subst[expr_,src_,tar_], TraditionalForm] := RowBox@{UnderscriptBox[StyleBox["Subs", FontSize -> Medium], RowBox[{MakeBoxes[src, TraditionalForm], "\[Rule]", MakeBoxes[tar, TraditionalForm]}]], MakeBoxes[expr, TraditionalForm]}

@asukaminato0721
Copy link
Author

By the way, I modified it to be another display function which doesn't use TeXForm but TraditionalForm:

IntTraditional[expr_, var_] := Style[Grid[
	{"\[LongEqual]", TraditionalForm@#}& /@ Cases[
		Flatten@First@Steps[Int[expr, var], RubiPrintInformation -> False],
		RubiIntermediateResult[x_] :> HoldForm@@x
	]// Prepend[{"",TraditionalForm@HoldForm@Int[expr,var]}]
, Alignment -> Left], FontFamily -> "Latin Modern Roman 10"];

SetAttributes[IntTraditional, HoldAll]

SyntaxInformation[IntTraditional] = {"LocalVariables" -> {"Integrate", {2, Infinity}}};

Subst /: MakeBoxes[HoldPattern@Subst[expr_,src_,tar_], TraditionalForm] := RowBox@{UnderscriptBox[StyleBox["Subs", FontSize -> Medium], RowBox[{MakeBoxes[src, TraditionalForm], "\[Rule]", MakeBoxes[tar, TraditionalForm]}]], MakeBoxes[expr, TraditionalForm]}

Actually your don't need to HoldAll, because the Integrate doesn't have that.

Attributes[Integrate]

{Protected, ReadProtected}

Without HoldAll, you can write things like IntTraditional[list[[1]],x]. Or you should use IntTraditional[ Evaluate@list[[1]],x]

@asukaminato0721
Copy link
Author

asukaminato0721 commented May 11, 2020

Wow, amazing, After running your code, the effort is so nice! Thx.

Maybe it's because of this line? Subst /: MakeBoxes[HoldPattern@Subst[expr_,src_,tar_], TraditionalForm] := RowBox@{UnderscriptBox[StyleBox["Subs", FontSize -> Medium], RowBox[{MakeBoxes[src, TraditionalForm], "\[Rule]", MakeBoxes[tar, TraditionalForm]}]], MakeBoxes[expr, TraditionalForm]}

image

IntWithStepsOfTeXForm[expr_, var_] := 
 With[{TeX2Str = Convert`TeX`ExpressionToTeX}, 
  Steps[Int[expr, var], RubiPrintInformation -> False] // Flatten // 
       Most // Cases[
       RubiIntermediateResult[x_] :> 
        "=&" <> (TeX2Str[HoldForm @@ x]) <> 
         "\\\\"] // {"\\begin{aligned}", 
       TeX2Str@HoldForm@Int[expr, var], Sequence @@ #, 
       "\\end{aligned}"} & //
 StringReplace[{"\\, d" <> ToString[var] ->  "\\, \\mathrm{d}" <> ToString[var],  "\\int" -> "\\displaystyle \\int"}]//
   StringRiffle]

@asdasd1dsadsa
Copy link

Wow, amazing, After running your code, the effort is so nice! Thx.

Maybe it's because of this line? Subst /: MakeBoxes[HoldPattern@Subst[expr_,src_,tar_], TraditionalForm] := RowBox@{UnderscriptBox[StyleBox["Subs", FontSize -> Medium], RowBox[{MakeBoxes[src, TraditionalForm], "\[Rule]", MakeBoxes[tar, TraditionalForm]}]], MakeBoxes[expr, TraditionalForm]}

image

IntWithStepsOfTeXForm[expr_, var_] := 
 With[{TeX2Str = Convert`TeX`ExpressionToTeX}, 
  Steps[Int[expr, var], RubiPrintInformation -> False] // Flatten // 
       Most // Cases[
       RubiIntermediateResult[x_] :> 
        "=&" <> (TeX2Str[HoldForm @@ x]) <> 
         "\\\\"] // {"\\begin{aligned}", 
       TeX2Str@HoldForm@Int[expr, var], Sequence @@ #, 
       "\\end{aligned}"} & //
 StringReplace[{"\\, d" <> ToString[var] ->  "\\, \\mathrm{d}" <> ToString[var],  "\\int" -> "\\displaystyle \\int"}]//
   StringRiffle]

Yes, Convert`TeX`ExpressionToTeX MakeBoxes with TraditionalForm at first, so this definition for display affects the TeX output.

asukaminato0721 referenced this issue in miRoox/MirooxUtils May 11, 2020
Gravifer added a commit to Gravifer/Rubi that referenced this issue Feb 10, 2021
This modification is a result from [issue#34](RuleBasedIntegration#34 (comment)); 
the additional `IntWithStepsOfTeXForm` by [@wuyudi](https://github.com/wuyudi) and `IntTraditional` by [@asdasd1dsadsa](https://github.com/asdasd1dsadsa) are integrated (with some change) as accepted values (resp., `TeXForm` and `TraditionalForm`) for the `RubiPrintInformation` option to the `Steps` function.
The corresponding `::usage` message is modified as well.

`MakeBox` formatting for `Subst` is added as suggested by [@asdasd1dsadsa](https://github.com/asdasd1dsadsa), with minor modification. 
`SyntaxInformation` is added for `Int` and `Steps`; this covers the [pull-request#43](RuleBasedIntegration#43 (comment)).
@Gravifer
Copy link

I made a PR (pull-request#44) for this thread. Modifications were made to the suggestions above, and I do hope this is not offensive to the original contributors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants