# Get LaTeX form from step by step solution

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?

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}$$

• nice :) Now I am sure all students will thank you for writing their HW's in Latex much easier than before. May 28, 2019 at 13:11
• 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 May 28, 2019 at 13:18
• @VirtualUser, I added the line needed for making TeXForm work with Row. (see also: Incompatibility of Row and TeXForm
– kglr
May 28, 2019 at 13:30
• You have a spurious set of parentheses in the Answer box. May 28, 2019 at 16:44
• Your code doesn't work for the input "integrate 1/(1+x^2) dx". May 28, 2019 at 21:23

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:

TeXForm

(* the following modification just flattens out options *)
SystemConvertTeXFormDumpmaketex[(StyleBox|Cell)[SystemConvertCommonDumpstr_,SystemConvertCommonDumpsty_String:"",SystemConvertCommonDumpopts__?OptionQ]] := Module[
{SystemConvertTeXFormDumpfv,SystemConvertTeXFormDumpund,SystemConvertTeXFormDumpfw,SystemConvertTeXFormDumpfs,SystemConvertCommonDumppre="",SystemConvertCommonDumppost="",SystemConvertTeXFormDumpmid},
SystemConvertTeXFormDumpfv=FontVariations/. Flatten@{SystemConvertCommonDumpopts}/. FontVariations->{};
SystemConvertTeXFormDumpund="Underline"/. SystemConvertTeXFormDumpfv/. "Underline"->False;
{SystemConvertTeXFormDumpfw,SystemConvertTeXFormDumpfs}={FontWeight,FontSlant}/. Flatten@{SystemConvertCommonDumpopts}/. {FontWeight|FontSlant->"Plain"};
SystemConvertTeXFormDumpmid=SystemConvertTeXFormDumpMakeTeX[StyleBox[SystemConvertCommonDumpstr,SystemConvertCommonDumpsty]];
If[SystemConvertTeXFormDumpfs==="Italic",SystemConvertTeXFormDumpmid=If[StringMatchQ[SystemConvertTeXFormDumpmid,"\\text{*}"],StringTake[SystemConvertTeXFormDumpmid,{7,-2}],"$$"<>SystemConvertTeXFormDumpmid<>"$$"];SystemConvertCommonDumppre="\\text{\\textit{"<>SystemConvertCommonDumppre;SystemConvertCommonDumppost=SystemConvertCommonDumppost<>"}}";];
If[SystemConvertTeXFormDumpund,SystemConvertCommonDumppre="\\underline{"<>SystemConvertCommonDumppre;SystemConvertCommonDumppost=SystemConvertCommonDumppost<>"}";];
If[SystemConvertTeXFormDumpfw==="Bold",SystemConvertCommonDumppre="\\pmb{"<>SystemConvertCommonDumppre;SystemConvertCommonDumppost=SystemConvertCommonDumppost<>"}";];
SystemConvertCommonDumppre<>SystemConvertTeXFormDumpmid<>SystemConvertCommonDumppost
]

(* the following modification just removes the initial "\n" *)
SystemConvertTeXFormDumpmaketex[GridBox[SystemConvertTeXFormDumpgrid_,SystemConvertCommonDumpopts___?OptionQ]] := Module[
{SystemConvertTeXFormDumpcolaln,SystemConvertTeXFormDumprowdivs,SystemConvertTeXFormDumpoutstr,SystemConvertTeXFormDumpi,SystemConvertTeXFormDumpcols,SystemConvertTeXFormDumprows},
SystemConvertCommonDumpDebugPrint["------------------------------------"];
SystemConvertCommonDumpDebugPrint["maketex[GridBox[grid_, opts___?OptionQ]]"];
SystemConvertCommonDumpDebugPrint["grid: ",SystemConvertTeXFormDumpgrid];
SystemConvertTeXFormDumpcols=Dimensions[SystemConvertTeXFormDumpgrid][[2]];
SystemConvertTeXFormDumprows=Dimensions[SystemConvertTeXFormDumpgrid][[1]];
SystemConvertTeXFormDumpcolaln=SystemConvertTeXFormDumpprocessColumnOptions[{SystemConvertCommonDumpopts},SystemConvertTeXFormDumpcols];
SystemConvertTeXFormDumprowdivs=SystemConvertTeXFormDumpprocessRowOptions[{SystemConvertCommonDumpopts},SystemConvertTeXFormDumprows];
SystemConvertTeXFormDumpoutstr="\\begin{array}{"<>SystemConvertTeXFormDumpcolaln<>"}"<>"\n";
For[SystemConvertTeXFormDumpi=1,SystemConvertTeXFormDumpi<=SystemConvertTeXFormDumprows,SystemConvertTeXFormDumpi++,If[SystemConvertTeXFormDumprowdivs[[SystemConvertTeXFormDumpi]]==True,SystemConvertTeXFormDumpoutstr=SystemConvertTeXFormDumpoutstr<>"\\hline"<>"\n"];SystemConvertTeXFormDumpoutstr=SystemConvertTeXFormDumpoutstr<>SystemConvertTeXFormDumpMakeRow[SystemConvertTeXFormDumpgrid[[SystemConvertTeXFormDumpi]]];];
If[Last[SystemConvertTeXFormDumprowdivs]==True,SystemConvertTeXFormDumpoutstr=SystemConvertTeXFormDumpoutstr<>"\\hline"<>"\n"];
SystemConvertTeXFormDumpoutstr=SystemConvertTeXFormDumpoutstr<>"\\end{array}"<>"\n";
SystemConvertTeXFormDumpoutstr
]

(* The following modification prevents some spaces from getting lost *)
SystemConvertTeXFormDumpmaketex["\" \""] = "\\text{ }"

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:
| = \[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

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

[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:
| = 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"}`
• 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. May 28, 2019 at 11:54