# Bug in Mathematica TeXForm generating \left\left

Bug introduced in 9.0 and persisting through 11.3 or later

ClearAll[y,x];
ode = D[y[x], x] - (y[x]^2 + 1)/(Abs[y[x] + (1 + y[x])^(1/2)]*(1 + x)^(3/2));
TeXForm[ode]


The output is

y'(x)-\frac{y(x)^2+1}{(x+1)^{3/2} \left\left| y(x)+\sqrt{y(x)+1}\right\right| }


Using \left\left is illegal. TeXLive 2014 will not compile it. MWE

\documentclass{article}
\usepackage{amsmath,mathtools}
\begin{document}
\begin{equation}

y'(x)-\frac{y(x)^2+1}{(x+1)^{3/2} \left\left| y(x)+\sqrt{y(x)+1}\right\right| }

\end{equation}
\end{document}


Compile:

 pdflatex foo.tex

! Missing delimiter (. inserted).
\left
l.7 ...eft\left| y(x)+\sqrt{y(x)+1}\right\right| }

?


Just wanted to confirm with others if this is a bug before send email to [email protected] unless someone finds a smart fix or an option to solve this.

V9.01 and V10.

Update:

If any one gets such a case, the fix in this example is to simply remove the extra \left at the outers. Like this

\documentclass{article}
\usepackage{amsmath,mathtools}

$$\begin{document} \begin{equation} y'(x)-\frac{y(x)^2+1}{(x+1)^{3/2} \left| y(x)+\sqrt{y(x)+1}\right| } \end{equation} \end{document}$$


Compiles now ok. The output is fyi, just send an email to [email protected] as well. Experts in Latex at TeX forum confirmed the code generated by Mathematica is wrong.

• The same here using 9.0.1.0 on Linux. Jul 15, 2014 at 1:33
• The problematic part seems to be that inside Abs is an expression which possibly requires scaled boundaries, because the expression is too large. Therefore, this works TeXForm[Abs[x + 1]] while this TeXForm[Abs[x + 1/2]] is introducing the extra left/right pair. Can find more notation like Abs and Norm that surround an expression? Jul 15, 2014 at 1:46

# 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 SystemConvertTeXFormDumpDelimiterBoxQ function.

If SystemConvertTeXFormDumpDelimiterBoxQ returns False, then "ordinary translation" to $\TeX$ occurs and delimiters are converted using SystemConvertTeXFormDumpmaketex 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 SystemConvertTeXFormDumpDelimiterBoxQ returns, True then delimiters are converted using SystemConvertTeXFormDumpInsertDelimiters function which adds \\left or \\right to result of conversion of delimiter with SystemConvertTeXFormDump$TeXDelimiterReplacements rules. SystemConvertTeXFormDump$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:

SystemConvertTeXFormDump$TeXDelimiterReplacements // TableForm (* ... \[LeftBracketingBar] -> {\left| } \[LeftDoubleBracketingBar] -> {\left\| } ... \[RightBracketingBar] -> {\right| } \[RightDoubleBracketingBar] -> {\right\| } ... *)  In case of "\[LeftBracketingBar]", "\[LeftDoubleBracketingBar]" and their right counterparts, SystemConvertTeXFormDumpInsertDelimiters function adds additional \\left and \\right to delimiters that already have them from SystemConvertTeXFormDump$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 SystemConvertTeXFormDump$TeXDelimiterReplacements rules, nor in SystemConvertTeXFormDumpmaketex function. # How to fix this bug Fixing this bug is easy, we just need to patch SystemConvertTeXFormDump$TeXDelimiterReplacements rules:

SystemConvertTeXFormDump$TeXDelimiterReplacements = SystemConvertTeXFormDump$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|} *)


This seems to be fixed in 12.1.1:

\$Version

(* 12.1.1 for Mac OS X x86 (64-bit) (June 9, 2020) *)

ClearAll[y, x];
ode = D[y[x], x] - (y[x]^2 + 1)/(Abs[y[x] + (1 + y[x])^(1/2)]*(1 + x)^(3/2));
TeXForm[ode]

(* y'(x)-\frac{y(x)^2+1}{(x+1)^{3/2} \left|y(x)+\sqrt{y(x)+1}\right| } *)