12
$\begingroup$

If I want to copy block as latex I do copy as LaTeX. But when I write:

WolframAlpha["integrate (sin(x))^4 dx"]

How can I copy step by step solution as latex?

$\endgroup$

4 Answers 4

15
$\begingroup$
content = WolframAlpha["integrate (sin(x))^4 dx", {{"IndefiniteIntegral", 2}, "Content"},
     PodStates -> {"IndefiniteIntegral__Step-by-step solution"}][[1]];

BoxForm`$UseTemplateSlotSequenceForRow = False;

TeXForm[Column@(MakeExpression[content //. StyleBox[a_, ___] :> a, 
    StandardForm] /. {{{a_}, {b_, c___}} :> Column[{HoldForm[a], HoldForm[b, c]}],  
      r_Row :> HoldForm[r]})[[1, All, 1]]]

$\small\begin{array}{l} \begin{array}{l} \text{Take the integral:} \\ \int \sin ^4(x) \, dx \\ \end{array} \\ \begin{array}{l} \text{Use }\text{the }\text{reduction }\text{formula, }\int \sin ^m(x) \, dx \text{= }-\frac{\cos (x) \sin ^{m-1}(x)}{m} \text{+ }\frac{m-1}{m}\int \sin ^{-2+m}(x) \, dx, \text{where }m=4: \\ \text{= }-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3}{4} \int \sin ^2(x) \, dx \\ \end{array} \\ \begin{array}{l} \text{Write }\sin ^2(x) \text{as }\frac{1}{2}-\frac{1}{2} \cos (2 x): \\ \text{= }-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3}{4} \int \left(\frac{1}{2}-\frac{1}{2} \cos (2 x)\right) \, dx \\ \end{array} \\ \begin{array}{l} \text{Integrate }\text{the }\text{sum }\text{term }\text{by }\text{term }\text{and }\text{factor }\text{out }\text{constants:} \\ \text{= }-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{8} \int \cos (2 x) \, dx+\frac{3 \int 1 \, dx}{8} \\ \end{array} \\ \begin{array}{l} \text{For }\text{the }\text{integrand }\cos (2 x), \text{substitute }u=2 x \text{and }du=2\, dx: \\ \text{= }-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{16} \int \cos (u) \, du+\frac{3 \int 1 \, dx}{8} \\ \end{array} \\ \begin{array}{l} \text{The }\text{integral }\text{of }\cos (u) \text{is }\sin (u): \\ \text{= }-\frac{1}{16} (3 \sin (u))-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3 \int 1 \, dx}{8} \\ \end{array} \\ \begin{array}{l} \text{The }\text{integral }\text{of }1 \text{is }x: \\ \text{= }\left(-\frac{1}{16} (3 \sin (u))+\frac{3 x}{8}-\frac{1}{4} \sin ^3(x) \cos (x)\right)+\text{constant} \\ \end{array} \\ \begin{array}{l} \text{Substitute }\text{back }\text{for }u=2 x: \\ \text{= }\left(\frac{3 x}{8}-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{8} \sin (x) \cos (x)\right)+\text{constant} \\ \end{array} \\ \begin{array}{l} \text{Which }\text{is }\text{equal }\text{to:} \\ \fbox{$\left( \begin{array}{cc} \text{Answer:} & \\ \text{} & \text{= }\frac{1}{32} (12 x-8 \sin (2 x)+\sin (4 x))+\text{constant} \\ \end{array} \right)$} \\ \end{array} \\ \end{array}$

$\endgroup$
7
  • 5
    $\begingroup$ nice :) Now I am sure all students will thank you for writing their HW's in Latex much easier than before. $\endgroup$
    – Nasser
    Commented May 28, 2019 at 13:11
  • $\begingroup$ When I copy your input to my notebook I get: Further output of TeXForm::unspt will be suppressed during this calculation. TeXForm::unspt: TeXForm of TemplateSlotSequence[1,] is not supported. and broken output $\endgroup$ Commented May 28, 2019 at 13:18
  • $\begingroup$ @VirtualUser, I added the line needed for making TeXForm work with Row. (see also: Incompatibility of Row and TeXForm $\endgroup$
    – kglr
    Commented May 28, 2019 at 13:30
  • 1
    $\begingroup$ You have a spurious set of parentheses in the Answer box. $\endgroup$
    – Carl Woll
    Commented May 28, 2019 at 16:44
  • 1
    $\begingroup$ Your code doesn't work for the input "integrate 1/(1+x^2) dx". $\endgroup$
    – Carl Woll
    Commented May 28, 2019 at 21:23
7
$\begingroup$

You can just apply TeXForm to the content returned by WolframAlpha, although you will encounter 3 issues:

  1. Some of the StyleBoxes use options that TeXForm doesn't recognize. TeXForm will issue messages and ignore them, so you can either use Quiet or strip out those options.

  2. The TeXForm produced has empty lines. This causes problems for the MathJax engine.

  3. Some spaces get removed.

So, one could do the following:

content = WolframAlpha[
    "integrate (sin(x))^4 dx",
    {{"IndefiniteIntegral",2},"Content"},
    PodStates->{"IndefiniteIntegral__Step-by-step solution"}
];

StringReplace[Quiet @ ToString[content, TeXForm], "\n" ~~ Whitespace ~~ "\n" -> "\n"]

which will fix the first 2 issues above. However, it is also possible to fix the internal code. The 3 specific problems with the internal code:

  1. Some spaces need to have a \text wrapper so that they don't disapper.
  2. The internal code has a bug when Style uses lists of options.
  3. The internal code tosses in some superfluous carriage returns ("\n") that break MathJax.

I will provide code to fix these issues at the bottom of my answer. After running that code, simply using TeXForm produces the desired output:

TeXForm @ WolframAlpha[
    "integrate (sin(x))^4 dx",
    {{"IndefiniteIntegral",2},"Content"},
    PodStates->{"IndefiniteIntegral__Step-by-step solution"}
]

$\begin{array}{l} \begin{array}{l} \text{Take the integral:} \\ \int \sin ^4(x) \, dx \\ \end{array} \\ \hline \begin{array}{l} \text{Use }\text{the }\text{reduction }\text{formula, }\int \sin ^m(x) \, dx\text{ }\text{= }-\frac{\cos (x) \sin ^{m-1}(x)}{m}\text{ }\text{+ }\frac{m-1}{m}\int \sin ^{-2+m}(x) \, dx,\text{ }\text{where }m=4: \\ \text{ }\text{= }-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3}{4}\int \sin ^2(x) \, dx \\ \end{array} \\ \begin{array}{l} \text{Write }\sin ^2(x)\text{ }\text{as }\frac{1}{2}-\frac{1}{2} \cos (2 x): \\ \text{ }\text{= }-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3}{4}\int \left(\frac{1}{2}-\frac{1}{2} \cos (2 x)\right) \, dx \\ \end{array} \\ \begin{array}{l} \text{Integrate }\text{the }\text{sum }\text{term }\text{by }\text{term }\text{and }\text{factor }\text{out }\text{constants:} \\ \text{ }\text{= }-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{8}\int \cos (2 x) \, dx+\frac{3}{8}\int 1 \, dx \\ \end{array} \\ \begin{array}{l} \text{For }\text{the }\text{integrand }\cos (2 x),\text{ }\text{substitute }u=2 x\text{ }\text{and }du=2\, dx: \\ \text{ }\text{= }-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{16}\int \cos (u) \, du+\frac{3}{8}\int 1 \, dx \\ \end{array} \\ \begin{array}{l} \text{The }\text{integral }\text{of }\cos (u)\text{ }\text{is }\sin (u): \\ \text{ }\text{= }-\frac{3 \sin (u)}{16}-\frac{1}{4} \sin ^3(x) \cos (x)+\frac{3}{8}\int 1 \, dx \\ \end{array} \\ \begin{array}{l} \text{The }\text{integral }\text{of }1\text{ }\text{is }x: \\ \text{ }\text{= }-\frac{3 \sin (u)}{16}+\frac{3 x}{8}-\frac{1}{4} \sin ^3(x) \cos (x)+\text{constant} \\ \end{array} \\ \begin{array}{l} \text{Substitute }\text{back }\text{for }u=2 x: \\ \text{ }\text{= }\frac{3 x}{8}-\frac{1}{4} \sin ^3(x) \cos (x)-\frac{3}{8} \sin (x) \cos (x)+\text{constant} \\ \end{array} \\ \begin{array}{l} \text{Which }\text{is }\text{equal }\text{to:} \\ \fbox{$\begin{array}{ll} \text{Answer:} & \\ \text{} & \text{ }\text{= }\frac{1}{32} (12 x-8 \sin (2 x)+\sin (4 x))+\text{constant} \\ \end{array} $} \\ \end{array} \\ \end{array}$

As a comparison with kglr's answer, there is no need to use MakeExpression, no spurious parentheses get introduced (his answer has spurious parentheses in the "Answer" box), the spacing issue gets fixed (note the missing space in the line "the integral of 1is x:"), and the output is correct even when a step-by-step solution has only 1 step.

Now, for the code. Note that the usual caveats apply to the following code, which modifies the internal code used by Mathematica:

(* force autoloading of TeXForm code *)
TeXForm 

(* the following modification just flattens out options *)
System`Convert`TeXFormDump`maketex[(StyleBox|Cell)[System`Convert`CommonDump`str_,System`Convert`CommonDump`sty_String:"",System`Convert`CommonDump`opts__?OptionQ]] := Module[
    {System`Convert`TeXFormDump`fv,System`Convert`TeXFormDump`und,System`Convert`TeXFormDump`fw,System`Convert`TeXFormDump`fs,System`Convert`CommonDump`pre="",System`Convert`CommonDump`post="",System`Convert`TeXFormDump`mid},
    System`Convert`TeXFormDump`fv=FontVariations/. Flatten@{System`Convert`CommonDump`opts}/. FontVariations->{};
    System`Convert`TeXFormDump`und="Underline"/. System`Convert`TeXFormDump`fv/. "Underline"->False;
    {System`Convert`TeXFormDump`fw,System`Convert`TeXFormDump`fs}={FontWeight,FontSlant}/. Flatten@{System`Convert`CommonDump`opts}/. {FontWeight|FontSlant->"Plain"};
    System`Convert`TeXFormDump`mid=System`Convert`TeXFormDump`MakeTeX[StyleBox[System`Convert`CommonDump`str,System`Convert`CommonDump`sty]];
    If[System`Convert`TeXFormDump`fs==="Italic",System`Convert`TeXFormDump`mid=If[StringMatchQ[System`Convert`TeXFormDump`mid,"\\text{*}"],StringTake[System`Convert`TeXFormDump`mid,{7,-2}],"$"<>System`Convert`TeXFormDump`mid<>"$"];System`Convert`CommonDump`pre="\\text{\\textit{"<>System`Convert`CommonDump`pre;System`Convert`CommonDump`post=System`Convert`CommonDump`post<>"}}";];
    If[System`Convert`TeXFormDump`und,System`Convert`CommonDump`pre="\\underline{"<>System`Convert`CommonDump`pre;System`Convert`CommonDump`post=System`Convert`CommonDump`post<>"}";];
    If[System`Convert`TeXFormDump`fw==="Bold",System`Convert`CommonDump`pre="\\pmb{"<>System`Convert`CommonDump`pre;System`Convert`CommonDump`post=System`Convert`CommonDump`post<>"}";];
    System`Convert`CommonDump`pre<>System`Convert`TeXFormDump`mid<>System`Convert`CommonDump`post
]

(* the following modification just removes the initial "\n" *)
System`Convert`TeXFormDump`maketex[GridBox[System`Convert`TeXFormDump`grid_,System`Convert`CommonDump`opts___?OptionQ]] := Module[
    {System`Convert`TeXFormDump`colaln,System`Convert`TeXFormDump`rowdivs,System`Convert`TeXFormDump`outstr,System`Convert`TeXFormDump`i,System`Convert`TeXFormDump`cols,System`Convert`TeXFormDump`rows},
    System`Convert`CommonDump`DebugPrint["------------------------------------"];
    System`Convert`CommonDump`DebugPrint["maketex[GridBox[grid_, opts___?OptionQ]]"];
    System`Convert`CommonDump`DebugPrint["grid: ",System`Convert`TeXFormDump`grid];
    System`Convert`TeXFormDump`cols=Dimensions[System`Convert`TeXFormDump`grid][[2]];
    System`Convert`TeXFormDump`rows=Dimensions[System`Convert`TeXFormDump`grid][[1]];
    System`Convert`TeXFormDump`colaln=System`Convert`TeXFormDump`processColumnOptions[{System`Convert`CommonDump`opts},System`Convert`TeXFormDump`cols];
    System`Convert`TeXFormDump`rowdivs=System`Convert`TeXFormDump`processRowOptions[{System`Convert`CommonDump`opts},System`Convert`TeXFormDump`rows];
    System`Convert`TeXFormDump`outstr="\\begin{array}{"<>System`Convert`TeXFormDump`colaln<>"}"<>"\n";
    For[System`Convert`TeXFormDump`i=1,System`Convert`TeXFormDump`i<=System`Convert`TeXFormDump`rows,System`Convert`TeXFormDump`i++,If[System`Convert`TeXFormDump`rowdivs[[System`Convert`TeXFormDump`i]]==True,System`Convert`TeXFormDump`outstr=System`Convert`TeXFormDump`outstr<>"\\hline"<>"\n"];System`Convert`TeXFormDump`outstr=System`Convert`TeXFormDump`outstr<>System`Convert`TeXFormDump`MakeRow[System`Convert`TeXFormDump`grid[[System`Convert`TeXFormDump`i]]];];
    If[Last[System`Convert`TeXFormDump`rowdivs]==True,System`Convert`TeXFormDump`outstr=System`Convert`TeXFormDump`outstr<>"\\hline"<>"\n"];
    System`Convert`TeXFormDump`outstr=System`Convert`TeXFormDump`outstr<>"\\end{array}"<>"\n";
    System`Convert`TeXFormDump`outstr
]

(* The following modification prevents some spaces from getting lost *)
System`Convert`TeXFormDump`maketex["\" \""] = "\\text{ }"
$\endgroup$
2
$\begingroup$

I tried to do this before, I could not find an option.

Alternative is to use "Plaintext" option, then copy the plain text to your latex editor and do some (lots) of manually clean up and editing

r = WolframAlpha[
  "Integrate[x Sin[x],{x,0,Pi}]", {{"Input", 2}, "Plaintext"}, 
  PodStates -> {"Input__Step-by-step solution"}]

gives

Compute the definite integral:
 integral_0^\[Pi] x sin(x) dx
For the integrand x sin(x), integrate by parts, integral f dg = f g - integral g df, where 
 f = x, dg = sin(x) dx, df = dx, g = -cos(x):
 = (-x cos(x)) right bracketing bar _0^\[Pi] + integral_0^\[Pi] cos(x) dx
Evaluate the antiderivative at the limits and subtract.
 (-x cos(x)) right bracketing bar _0^\[Pi] = (-\[Pi] cos(\[Pi])) - (-0 cos(0)) = \[Pi]:
 = \[Pi] + integral_0^\[Pi] cos(x) dx
Apply the fundamental theorem of calculus.
The antiderivative of cos(x) is sin(x):
 = \[Pi] + sin(x) right bracketing bar _0^\[Pi]
Evaluate the antiderivative at the limits and subtract.
 sin(x) right bracketing bar _0^\[Pi] = sin(\[Pi]) - sin(0) = 0:
Answer: | 
 | = \[Pi]

Then clean it to become

\documentclass{article}
\usepackage{amsmath}
\begin {document}

Compute the definite integral:
 $\int_0^\pi x \sin(x) \,dx$

For the integrand $x \sin(x)$, integrate by parts, $\int f dg = f g - \int g df$, where 
 $f = x, dg = \sin(x) dx, df = dx, g = -\cos(x) = (-x \cos(x))$

Evaluate the antiderivative at the limits and subtract. etc...

\end {document}

When done cleaning it by hand, compile it

Mathematica graphics

I could not find a "TeXForm" option to Wolfram Alpha output.

$\endgroup$
2
$\begingroup$

[See here][1]: not $\LaTeX$ but a good start:

WolframAlpha["integrate (sin(x))^4 dx", "PodPlaintext", 
  PodStates -> {"IndefiniteIntegral__Step-by-step solution"}]
{"integral sin^4(x) dx = 1/32 (12 x - 8 sin(2 x) + sin(4 x)) + \
constant", "Take the integral:
  integral sin^4(x) dx
 Use the reduction formula, integral sin^m(x) dx = -(cos(x) sin^(m - \
1)(x))/m + (m - 1)/m integral sin^(-2 + m)(x) dx, where m = 4:
  = -1/4 sin^3(x) cos(x) + 3/4 integral sin^2(x) dx
 Write sin^2(x) as 1/2 - 1/2 cos(2 x):
  = -1/4 sin^3(x) cos(x) + 3/4 integral(1/2 - 1/2 cos(2 x)) dx
 Integrate the sum term by term and factor out constants:
  = -1/4 sin^3(x) cos(x) - 3/8 integral cos(2 x) dx + 3/8 integral1 \
dx
 For the integrand cos(2 x), substitute u = 2 x and du = 2 dx:
  = -1/4 sin^3(x) cos(x) - 3/16 integral cos(u) du + 3/8 integral1 dx
 The integral of cos(u) is sin(u):
  = -(3 sin(u))/16 - 1/4 sin^3(x) cos(x) + 3/8 integral1 dx
 The integral of 1 is x:
  = -(3 sin(u))/16 + (3 x)/8 - 1/4 sin^3(x) cos(x) + constant
 Substitute back for u = 2 x:
  = (3 x)/8 - 1/4 sin^3(x) cos(x) - 3/8 sin(x) cos(x) + constant
 Which is equal to:
 Answer: | 
  | = 1/32 (12 x - 8 sin(2 x) + sin(4 x)) + constant", "(3 x)/8 + 1/8 \
sin(x) cos^3(x) - 1/8 sin^3(x) cos(x) - 1/2 sin(x) cos(x) + \
constant", "(3 x)/8 - 1/8 i e^(-2 i x) + 1/8 i e^(2 i x) + 1/64 i \
e^(-4 i x) - 1/64 i e^(4 i x) + constant", "(3 x)/8 - 1/4 sin(2 x) + \
1/32 sin(4 x) + constant", "x^5/5 - (2 x^7)/21 + x^9/45 - (34 \
x^11)/10395 + O(x^13)
 (Taylor series)", "integral_0^π sin^4(x) dx = (3 π)/8\
≈1.1781", "integral_0^(2 π) sin^4(x) dx = (3 π)/4\
≈2.35619", "integral_0^(2 π) (sin^8(x))/(2 π) dx \
= 35/128≈0.273438"}```
$\endgroup$
1
  • $\begingroup$ After WolframAlpha["integrate (sin(x))^4 dx"] we can just click "show step by step" but I still have doubts how to copy that solution to latex. $\endgroup$ Commented May 28, 2019 at 11:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.