# Mathematica to $\LaTeX$ in a for loop

Hello I have a For loop which outputs an expression and I want to convert everything to $\LaTeX$ at once (not line by line). This is my code:

K[Q_, n_Integer] :=
Module[{z, x},
SymmetricReduction[
SeriesCoefficient[
Product[ComposeSeries[Series[Q[z], {z, 0, n}],
Series[x[i] z, {z, 0, n}]], {i, 1, n}], n],
Table[x[i], {i, 1, n}], Table[Subscript[c, i], {i, 1, n}]][] //
FactorTerms]

poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, 4] /. c -> p;
primeFactorForm[n_] :=
If[Length@# == 1, First@#, CenterDot @@ #] &[
Superscript @@@ FactorInteger[n]];

For[i = 0, i < 5, i++,
poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, i] /. c -> p;
Print[Subscript[L, i], " = ",
Plus @@ List @@ Distribute[poly] /.
Times[Rational[n_, d_], e__] :>
primeFactorForm[n]/ primeFactorForm[d]*e]]


I am not sure how to convert all the 10 lines to $\LaTeX$. I tried some basic functions (TeXForm for example) but I get \text{Null}. Can someone help me with this? Thank you!

• For a start, use Table instead of For and Print... – Henrik Schumacher Mar 4 '18 at 18:53
• @HenrikSchumacher getting the same result with Table – Silviu Mar 4 '18 at 19:00
• You get \text{Null} from TexForm because you are still using For along with Print instead of Table. Print and For are not meant for returning results; Table is. – Henrik Schumacher Mar 7 '18 at 17:05

In order to produce a string, that can be copied, e.g., to a $\LaTeX$ document, you may use the following. It is important to prevent Set (also known as =) from evaluation; turning it into a string "=" by hand is subobtimal since TeXForm will wrap it with \text{=}. That is why I use Inactive on the InputForm of =.

string = StringJoin["\\begin{gather}",
Riffle[
Table[
poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, i] /. c -> p;

ToString[

Inactive[Set][Subscript[L, i],
Plus @@ List @@ Distribute[poly] /.
Times[Rational[n_, d_], e__] :> primeFactorForm[n]/primeFactorForm[d]*e
],
TeXForm
]
,
{i, 0, 4}] ,
"\\\\"
],
"\n\\end{gather}"
]


$$\begin{gather}L_0=1\\L_1=p_1+\frac{1}{3}\\L_2=\frac{p_1^2 -1^1}{3^2\cdot 5^1}+\frac{p_2 7^1}{3^2\cdot 5^1}\\L_3=\frac{p_1^3 2^1}{3^3\cdot 5^1\cdot 7^1}+\frac{p_2 p_1 -1^1\cdot 13^1}{3^3\cdot 5^1\cdot 7^1}+\frac{p_3 2^1\cdot 31^1}{3^3\cdot 5^1\cdot 7^1}\\L_4=\frac{p_1^4 -1^1}{3^3\cdot 5^2\cdot 7^1}+\frac{p_2 p_1^2 2^1\cdot 11^1}{3^4\cdot 5^2\cdot 7^1}+\frac{p_3 p_1 -1^1\cdot 71^1}{3^4\cdot 5^2\cdot 7^1}+\frac{p_2^2 -1^1\cdot 19^1}{3^4\cdot 5^2\cdot 7^1}+\frac{p_4 127^1}{3^3\cdot 5^2\cdot 7^1} \end{gather}$$

Note that special characters such as "\", """, and newline have to be replaced by appropriate escape sequences "\\", "\"", and \n. Copying this with ctrl+c will most likely lead to errors. However, executing CopyToClipboard[string] (and then pasting ctrl+v into any other editor) will work.

• @Thank you for this. However when I try to copy it in a file text it doesn't work the way I want... – Silviu Mar 6 '18 at 11:02
• Silviu -- you need to specify how you want it to work. What part fails? – bill s Mar 7 '18 at 3:47
• @Silviu I have edited the post. Have a look. – Henrik Schumacher Mar 7 '18 at 6:56
• @HenrikSchumacher Thank you for this, but it still doesn't work. I also edited my post adding the whole code that I want to convert into latex. – Silviu Mar 7 '18 at 16:57
• Thank you so so much for this! – Silviu Mar 7 '18 at 18:20