4
$\begingroup$

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}}
$\endgroup$
  • $\begingroup$ It appears to be a bug in TeXForm. $\endgroup$ – Mark Adler Jul 11 '16 at 2:25
  • 1
    $\begingroup$ 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. $\endgroup$ – Nasser Jul 11 '16 at 4:21
  • 1
    $\begingroup$ @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. $\endgroup$ – Jens Jul 11 '16 at 4:22
6
$\begingroup$

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

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

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).

$\endgroup$
  • $\begingroup$ I down't understand, I got \left| -13 x+5 \text{x${}^{\wedge}$2}+5\right| =-20 x+6 \text{x${}^{\wedge}$2}+17 $\endgroup$ – minthao_2011 Jul 11 '16 at 4:06
  • $\begingroup$ 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"$... $\endgroup$ – Jens Jul 11 '16 at 4:13
  • $\begingroup$ That's right. Thank you. $\endgroup$ – minthao_2011 Jul 11 '16 at 4:15

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