Closed asukaminato0721 closed 4 years ago
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
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:
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.
Yep, this is exactly what I implemented RubiPrintInformation -> False
for so that people can create their own functions for displaying integration steps.
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
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]}
By the way, I modified it to be another display function which doesn't use
TeXForm
butTraditionalForm
: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]
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]}
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]
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]}
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.
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.
This is what now I do :
For example:
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
to make it easier for following steps? For example , using MaTeX to get nice formula.