# How can I convert from a set of lists to equations in the form LaTeX? [duplicate]

I have a set

  {{1, -9, 17, 2, -12, 19}, {1, -1, -3, 2, -8, 9}, {1, 1, -8, 3, -13,
16}, {2, -4, -1, 3, -11, 11}, {3, -7, 1, 4, -14, 13}, {3, -5, -4,
5, -19, 20}, {4, -10, 3, 5, -17, 15}, {5, -13, 5, 6, -20, 17}}


I want to convert each list to an equation in the form

$$\left |ax^2 + bx + c \right | = dx^2 + ex + f.$$ For example, with $\{5, -13, 5, 6, -20, 17\}$, I write to $$\left |5x^2 - 13x + 5 \right | = 6x^2 -20x + 17.$$ I tried

v = {5, -13, 5, 6, -20, 17};
u = {x^2, x , 1};
Abs[{v[[1]]
, v[[2]]
, v[[3]]
}. {x^2, x , 1}] == {v[[4]]
, v[[5]]
, v[[6]]}.{x^2, x , 1}


I got

Abs[5 - 13 x + 5 x^2] == 17 - 20 x + 6 x^2

If I used TeXForm

v = {5, -13, 5, 6, -20, 17};
u = {x^2, x , 1};
Abs[{v[[1]]
, v[[2]]
, v[[3]]
}. {x^2, x , 1}] == {v[[4]]
, v[[5]]
, v[[6]]}.{x^2, x , 1} // TeXForm


I got

\left\left| 5 x^2-13 x+5\right\right| =6 x^2-20x+17

When I copy this line into TexMaker, it can not comppile.

My two questions are:

1) How can I get \left| 5 x^2-13 x+5\right| = 6 x^2-20x+17 when I use

v = {5, -13, 5, 6, -20, 17};
u = {x^2, x , 1};
Abs[{v[[1]]
, v[[2]]
, v[[3]]
}. {x^2, x , 1}] == {v[[4]]
, v[[5]]
, v[[6]]}.{x^2, x , 1} // TeXForm


2) How can I convert all list in the set?

{{1, -9, 17, 2, -12, 19}, {1, -1, -3, 2, -8, 9}, {1, 1, -8, 3, -13,
16}, {2, -4, -1, 3, -11, 11}, {3, -7, 1, 4, -14, 13}, {3, -5, -4,
5, -19, 20}, {4, -10, 3, 5, -17, 15}, {5, -13, 5, 6, -20, 17}}


## marked as duplicate by Jens, Community♦Jul 11 '16 at 4:24

• It appears to be a bug in TeXForm. – Mark Adler Jul 11 '16 at 2:25
• This is known bug. possible-bug-in-mathematica-texform-generating-left-left proposed fix also posted. WRI should really fix this. This was reported 2 years ago. – Nasser Jul 11 '16 at 4:21
• @Nasser You're right - I had even upvoted your question and the answer back then, but forgot... I guess this means the question can be closed. – Jens Jul 11 '16 at 4:22

It's a bug in TeXForm. It shows up when there are exponents in the argument for Abs[], suggesting that it adds \left and \right when it thinks it needs to to make the absolute value bars bigger for the exponent, not realizing that Abs[] already generated \left| and \right|.

This applies the equation generation, the TeXForm, and a workaround for the bug to all of the list items:

StringReplace[ToString[TeXForm[Abs[#1 x^2+#2 x+#3]==#4 x^2+#5 x+#6]],
{"\\left\\left"->"\\left","\\right\\right"->"\\right"}]&@@@
{{1,-9,17,2,-12,19},{1,-1,-3,2,-8,9},{1,1,-8,3,-13,16},{2,-4,-1,3,-11,11},
{3,-7,1,4,-14,13},{3,-5,-4,5,-19,20},{4,-10,3,5,-17,15},{5,-13,5,6,-20,17}}

{\left| x^2-9 x+17\right| =2 x^2-12 x+19,
\left| x^2-x-3\right| =2 x^2-8 x+9,
\left| x^2+x-8\right| =3 x^2-13 x+16,
\left| 2 x^2-4 x-1\right| =3 x^2-11 x+11,
\left| 3 x^2-7 x+1\right| =4 x^2-14 x+13,
\left| 3 x^2-5 x-4\right| =5 x^2-19 x+20,
\left| 4 x^2-10 x+3\right| =5 x^2-17 x+15,
\left| 5 x^2-13 x+5\right| =6 x^2-20 x+17}


$$\left| x^2-9 x+17\right| =2 x^2-12 x+19$$ $$\left| x^2-x-3\right| =2 x^2-8 x+9$$ $$\left| x^2+x-8\right| =3 x^2-13 x+16$$ $$\left| 2 x^2-4 x-1\right| =3 x^2-11 x+11$$ $$\left| 3 x^2-7 x+1\right| =4 x^2-14 x+13$$ $$\left| 3 x^2-5 x-4\right| =5 x^2-19 x+20$$ $$\left| 4 x^2-10 x+3\right| =5 x^2-17 x+15$$ $$\left| 5 x^2-13 x+5\right| =6 x^2-20 x+17$$

• How Can I add symbol  \ \  inorder break line of each equation? – minthao_2011 Jul 11 '16 at 3:27
• StringRiffle[{"s1","s2","s3"},"\\\\"] gives s1\\s2\\s3. – Mark Adler Jul 11 '16 at 3:30
• But I got StringRiffle[{"s1", "s2", "s3"}, "\\\\"] – minthao_2011 Jul 11 '16 at 3:53
• @minthao_2011 For versions before 10.1, use StringJoin@Riffle[...] for StringRiffle[...] – Michael E2 Jul 11 '16 at 3:55
• @MichaelE2 Thank for your help. I use version 10.02. – minthao_2011 Jul 11 '16 at 3:57

The following also works. It doesn't require any string replacements in the output. All I did is to re-define the set of variables u such that it contains the second power of x as a String:

v={5,-13,5,6,-20,17};
u={"\!$$\*SuperscriptBox[\(x$$, $$2$$]\)",x,1};
Abs[{v[[1]],v[[2]],v[[3]]}.u]=={v[[4]],v[[5]],v[[6]]}.u//TeXForm


\left| 5 x^2-13 x+5\right| =6 x^2-20 x+17

The strange-looking string "\!$$\*SuperscriptBox[\(x$$, $$2$$]\)" above is actually entered by typing $"\!x^2\!"$ as a string. So really, I have $u = \{"\!x^2\!",x,1\}$. What you see in the copyable code above is the ugly InputForm representation of this formatted string.

This is based on the observation by @Mark Adler that the bug occurs when there is a two-dimensional object inside the bracketing bar that may require a height adjustment. What happens in that case is that Mathematica TraditionalForm outputs a TemplateBox with tag Abs, instead of a literal RowBox containing the bracketing bars explicitly. So the TemplateBox is apparently where the bug occurs when going from TraditionalForm to TeXForm (that is the route by which TeXForm is generated).

• I down't understand, I got \left| -13 x+5 \text{x${}^{\wedge}$2}+5\right| =-20 x+6 \text{x${}^{\wedge}$2}+17 – minthao_2011 Jul 11 '16 at 4:06
• Oh yes, I see this was copied incorrectly from my notebook to the answer. The string should be entered as a real superscript, $"x^2"$... – Jens Jul 11 '16 at 4:13
• That's right. Thank you. – minthao_2011 Jul 11 '16 at 4:15