23
$\begingroup$

Bug introduced in 9.0 and persisting through 11.3 or later


I asked about this in the TeX chat. It seems Mathematica generate invalid Latex in this example.

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).
<to be read again> 
                   \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

Mathematica graphics

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

$\endgroup$
2
  • 2
    $\begingroup$ The same here using 9.0.1.0 on Linux. $\endgroup$
    – Sigur
    Commented Jul 15, 2014 at 1:33
  • 3
    $\begingroup$ 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? $\endgroup$
    – halirutan
    Commented Jul 15, 2014 at 1:46

2 Answers 2

25
$\begingroup$

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|} *)
$\endgroup$
1
$\begingroup$

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| } *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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