# Bad position equation number Lualatex

If a math display is too wide, then the \eqno appears on the next line on the left instead of on the right as it should. Does anyone know how to solve this problem? This is an example:

\documentclass[a4paper]{article}
\usepackage[cmex10]{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{mathptmx}

\newcommand{\E}{\mathbb{E}}
\newcommand{\si}[1]{\ensuremath{\sigma_{#1}^{2}}}

\begin{document}

$$C= \begin{bmatrix} {C_{11} } & {C_{12} } \\ {C_{21} } & {C_{22} } \\ \end{bmatrix}= \begin{bmatrix} \E\left[ {\left( {X-m_{X}} \right)^{2}} \right] & \E\left[ {\left( {X-m_{X}} \right)\left( {Y-m_{Y}} \right)} \right] \\ \E\left[ {\left( {X-m_{X}} \right)\left( {Y-m_{Y}} \right)} \right] & \E\left[ {\left( {Y-m_{Y}} \right)^{2}} \right] \end{bmatrix}= \begin{bmatrix} \si{X} & C_{XY} \\ C_{XY} & \si{Y} \\ \end{bmatrix}$$

\end{document}


The same happens with plain LuaTeX.

\hsize=2cm
$$a^2+b^2=c^2\eqno(1)$$
\bye


Ouput after compilation with luatex:

Ouput after compilation with pdftex:

Dear friends: I think the next code is a posible, and provisional solution:

\usepackage{environ}

\NewEnviron{equationw}
{
$$\begin{split} \BODY \end{split}$$
}

-
Welcome to TeX.sx! – hpesoj626 Feb 12 '13 at 9:14
I took the freedom to add another MWE with sample outputs. (To me it seems like a bug in the LuaTeX engine.) – Hendrik Vogt Feb 12 '13 at 14:31
it is correct with xelatex. However you should use package unicode-math and \setmathfont{texgyretermes-math.otf} and not loading mathptmx when running lualatex – Herbert Feb 12 '13 at 14:36
Just to let you know, if my answer helped you, then you can "accept" it by clicking the checkmark at the top left :-) – Hendrik Vogt Feb 16 '13 at 20:24

This appears to be a bug in the LuaTeX engine, see below. For a workaround, use align instead of equation; the align environment doesn't rely on the TeX engine for placement of the equation number. This fixes your problem (but can influence the vertical spacing for shorter equations).

I think I could locate the bug in texmath.w, lines 2220 to 2225. There it says

if ((eqno_box != null) && (eqno_w == 0) && !l) {
tail_append(new_penalty(inf_penalty));
shift_amount(eqno_box) = line_s;
append_to_vlist(eqno_box);
g2 = 0;
}


Of interest is the third line: the shift amount of the box containing the equation number is line_s, which previously was set to \displayindent (which usually is zero). Thus, the equation number is flush left.

Here's the corresponding lines from Knuth's tex.web, lines 22580 to 22585:

if (a<>null)and(e=0)and not l then
begin tail_append(new_penalty(inf_penalty));
shift_amount(a):=s+z-width(a);
append_to_vlist(a);
g2:=0;
end;


You can see that this is the very same code in a different language, eqno_box != null translates to a<>null, and so on. Here, the shift amount of the box containing the equation number is s+z-width(a), where s is the same as line_s above, z is the line width and width(a), well, the width of the box a with the equation number. And this is the correct shift amount for having the equation number flush right!

Maybe I should explain the conditions in the first line of the code: the box with the equation number should be non-empty, it should have width 0(!), and we shouldn't be in the case of a left equation number as in \leqno. And why the width 0? This is the "flag" that was set before to indicate that the box should be placed in the next line because it didn't fit into the line with the equation.

-
Could you please open a bug at the LuaTeX bug tracker? – Martin Schröder Feb 13 '13 at 11:53
@Martin: Hmm, I had a look and didn't find where I can report this. Would I have to sign up? – Hendrik Vogt Feb 13 '13 at 11:59
tracker.luatex.org/bug_report_page.php - and yes, you need an account. – Martin Schröder Feb 13 '13 at 13:04
@Martin: I don't really want yet another account somewhere. I don't even use LuaTeX ... May I ask you to do the report for me? Maybe it suffices to point to my answer here. – Hendrik Vogt Feb 13 '13 at 14:21
Agreed, it is a (previously unknown) engine bug. Submitted as tracker.luatex.org/view.php?id=807 – Taco Hoekwater Feb 16 '13 at 13:03