Bug in Mathematica TeXForm generating \left\left

Why we're getting this buggy result

In process of conversion to $\TeX$, whenever Mathematica encounters "something delimited" i.e. RowBox with something surrounded with Strings matching:

"(" | "[" | "\[LeftModified]" | "\[LeftDoubleBracket]" | "{" | "\[Piecewise]" | "\[LeftFloor]" | "\[LeftCeiling]" | "\[LeftAngleBracket]" | "\[LeftSkeleton]" | "«" | "\[LeftBracketingBar]" | "\[LeftDoubleBracketingBar]" | ")" | "]" | "\[RightModified]" | "\[RightDoubleBracket]" | "}" | "\[RightFloor]" | "\[RightCeiling]" | "\[RightAngleBracket]" | "\[RightSkeleton]" | "»" | "\[RightBracketingBar]" | "\[RightDoubleBracketingBar]" | "/" | "\\" | "|" | "\[VerticalSeparator]" | "||"`

then it tests, whether those delimited boxes can potentially result in something higher then line height, using System`Convert`TeXFormDump`DelimiterBoxQ function.

If System`Convert`TeXFormDump`DelimiterBoxQ returns False, then "ordinary translation" to $\TeX$ occurs and delimiters are converted using System`Convert`TeXFormDump`maketex function, which for Abs TraditionalForm delimiters: "\[LeftBracketingBar]", "\[RightBracketingBar]" returns "\\left| " and "\\right| " respectively.

That's why we get:

Abs[x + 1]//TeXForm
(* \\left| x+1\\right| *)

If System`Convert`TeXFormDump`DelimiterBoxQ returns, True then delimiters are converted using System`Convert`TeXFormDump`InsertDelimiters function which adds \\left or \\right to result of conversion of delimiter with System`Convert`TeXFormDump`$TeXDelimiterReplacements rules.

System`Convert`TeXFormDump`$TeXDelimiterReplacements contains replacement rules for delimiters like "\[LeftAngleBracket]" -> {"\\langle "}. Among them, for unknown reason, two pairs of $\TeX$ delimiters contain additional "\\left" and "\\right" commands:

System`Convert`TeXFormDump`$TeXDelimiterReplacements // TableForm
(*
    ...
    \[LeftBracketingBar] -> {\left| }
    \[LeftDoubleBracketingBar] -> {\left\| }
    ...
    \[RightBracketingBar] -> {\right| }
    \[RightDoubleBracketingBar] -> {\right\| }
    ...
*)

In case of "\[LeftBracketingBar]", "\[LeftDoubleBracketingBar]" and their right counterparts, System`Convert`TeXFormDump`InsertDelimiters function adds additional \\left and \\right to delimiters that already have them from System`Convert`TeXFormDump`$TeXDelimiterReplacements rules.

That's why we get:

Abs[x + 1/2]//TeXForm
(* \left\left| x+\frac{1}{2}\right\right| *)

This bug was introduced in Mathematica version 9. In version 8 there are no additional \\left and \\right commands neither in System`Convert`TeXFormDump`$TeXDelimiterReplacements rules, nor in System`Convert`TeXFormDump`maketex function.


How to fix this bug

Fixing this bug is easy, we just need to patch System`Convert`TeXFormDump`$TeXDelimiterReplacements rules:

System`Convert`TeXFormDump`$TeXDelimiterReplacements =
    System`Convert`TeXFormDump`$TeXDelimiterReplacements /. {
        "\\left| " | "\\right| " -> "|",
        "\\left\\| " | "\\right\\| " -> "\\| "
    }

Now we get correct $\TeX$ code:

Abs[x] // TeXForm
(* \left| x\right| *)

Abs[x + 1/2] // TeXForm
(* \left|x+\frac{1}{2}\right| *)

D[y[x], x] - (y[x]^2 + 1)/(Abs[y[x] + (1 + y[x])^(1/2)]*(1 + x)^(3/2)) // TeXForm
(* y'(x)-\frac{y(x)^2+1}{(x+1)^{3/2} \left|y(x)+\sqrt{y(x)+1}\right|} *)

Tags:

Latex

Bugs