1

A description of the problem by way of a minimal working example

I will describe two experiments: "control", and "treatment". I will first describe a "control" experiment, which demonstrates what I consider to be normal behavior. I will then describe a second, "treatment", experiment, which exhibits a different behavior to that observed in the first case. The behavior exhibited in the second experiment is the one I consider problematic.

1. Control

I saved the following LaTeX code in the file ~/Test.tex.

\documentclass{article}
%\usepackage[bidi=basic,hebrew,provide=*]{babel}
%\babelfont{rm}[Renderer=HarfBuzz]{FreeSerif}
\usepackage{amsmath}
\usepackage{lipsum}
\begin{document}
\lipsum[1-1][1-2]

\begin{alignat}{2} a&=b&&+c\ d&=e&&+f\ ֿ\intertext{and} x&=y&&+z \end{alignat}

\lipsum[1-1][3-4] \end{document}

I then executed the following commands in the Terminal.

> cd ~
> lualatex Test

This resulted in the file ~/Test.pdf being created. When opened in a PDF viewer, the file displayed as follows. (I screenshot only the relevant part of the display.)

alignat* in an English document

Note that the equations are centered in the text area, and that the intertext is left-aligned.

2. Treatment

Repeat the first experiment but in a Hebrew document whose language is set via the babel package. In other words, in the code above uncomment the two commented-out lines, and replace the argument to the \intertext command (i.e. and) by \foreignlanguage{hebrew}{and}. The PDF output is now

alignat* in a Hebrew document

Note that

  1. The equations are right-aligned.
  2. The intertext is left-aligned.

Considering that Hebrew is a right-to-left language in which mathematics is treated as embedded English (hence left-to-right) text, this output is incorrect. The correct behavior would be that the equations be centered, as in the "control" experiment, and that the word "and" be right-aligned.

Questions

  1. Why weren't the equations centered in the Hebrew document? How can this be corrected?
  2. Why wasn't the intertext right-aligned in the Hebrew document? How can this be corrected?

Remarks

  1. No warnings were found in ~/Test.log at the end of the Hebrew experiment.

  2. The right-alignment of the equations in the Hebrew experiment occurs even when no intertext is present. However, in this case there's a workaround: to replace the alignat* environment by an equation* environment with an embedded alignedat environment:

     \begin{equation*}
        \begin{alignedat}{2}
        a&=b&&+c\\
        d&=e&&+f\\
        x&=y&&+z
        \end{alignedat}
     \end{equation*}
    

    This produces the correct output:

    alignedat in a Hebrew document

    However, the \intertext command cannot be embedded in an alignedat environment. But even in the absence of an \intertext command, I would prefer not to resort to the equation* workaround; I'd like the alignat* environment to behave as it does in an English document.

  3. I tried to center the equations with adjustbox as follows.

     ...
     \usepackage{adjustbox}
     ...
     \begin{document}
     ...
     \begin{adjustbox}{center}
     \begin{alignat*}{2}
        a&=b&&+c\\
        d&=e&&+f\\
        x&=y&&+z
     \end{alignat*}
     \end{adjustbox}{center}
    ...
    

    However, this resulted in a compilation failure, and the following message appeared in ~/Test.log.

     ! Missing \endgroup inserted.
     <inserted text> 
     \endgroup 
     l.17 \end{alignat*}
    

    ? ! Emergency stop. <inserted text> \endgroup l.17 \end{alignat*}

    End of file on the terminal!

  4. In contrast to the alignat* environment, the alignat environment (without an asterisk) works properly in the absence of intertext:

     \begin{alignat}{2}
        a&=b&&+c\\
        d&=e&&+f\\
        x&=y&&+z
     \end{alignat}
    

    produces

    alignat in a Hebrew document

    However, Hebrew intertext is still wrongly aligned:

     \begin{alignat}{2}
        a&=b&&+c\\
        d&=e&&+f\\
        \intertext{\foreignlanguage{hebrew}{and}}
        x&=y&&+z
     \end{alignat}
    

    produces

    alignat with intertext in a Hebrew document

  5. The align* environment behaves as it ought to. For instance the following code

     \begin{align*}
         a&=b&&+c\nonumber\\
         d&=e&&+f\nonumber\\
         \intertext{\foreignlanguage{hebrew}{and}}
         x&=y&&+z\nonumber
     \end{align*}
    

    produces

    align* with intertext in a Hebrew document

    In contrast, the align environment right-aligns both the equations and the intertext.

Evan Aad
  • 11,066

1 Answers1

5

Attention The following is not meant as complete solution, only as tips for further investigations.

alignat* is internally a tabular (a \halign in TeX speech). amsmath centers it by first measuring the width, then it calculates the free space on the line, divide it by 2 and adds the result as a tabskip to the left of the first column. In left-to-right mode that works fine, in right-to-left mode it fails as there you would need the tabskip at the right of the tabular (or its last column). The unstarred version works as there the right column has already the correct size.

Even if one understand what's happening it is not trivial to fix. amsmath shares code between environments, so changing something can affect other environments too. Also for every change one has to test various combinations (starred/unstarred, in TLT, in TRT, with and without fleqn, with pdflatex, xelatex and other engines etc ...). Which means the following can work but it can also fails in some yet untested case ...

\documentclass{article}

\usepackage{amsmath} \usepackage{lipsum} \usepackage[bidi=basic,hebrew,provide=*]{babel} \babelfont{rm}[Renderer=HarfBuzz]{FreeSerif} \makeatletter \newcommand\bidiendalignat{% \ifst@rred \kern\eqnshift@ % additional kern \fi \math@cr \black@\totwidth@ \egroup
\ifingather@ \restorealignstate@ \egroup \nonumber \ifnum0=`{\fi\iffalse}\fi \else $$% \fi
\ignorespacesafterend}

\renewenvironment{alignat*}{% \start@align\z@\st@rredtrue }{% \bidiendalignat }

\def\intertext@{% \def\intertext##1{% \ifvmode\else\@empty\fi \noalign{% \penalty\postdisplaypenalty\vskip\belowdisplayskip
\vbox{\normalbaselines \textdir TRT \pardir TRT %wrong in TLT, so test for bidi needed ... \ifdim\linewidth=\columnwidth \else \parshape@ne @totalleftmargin \linewidth \fi \noindent\ignorespaces##1\par}% \penalty\predisplaypenalty\vskip\abovedisplayskip% }% }}

\makeatother

\begin{document} \lipsum[1]

\begin{alignat}{2} a&=b&&+c\ d&=e&&+f\ ֿ\intertext{and} x&=y&&+z \end{alignat}

\begin{alignat}{2} a&=b&&+c\ d&=e&&+f\ ֿ\intertext{and} x&=y&&+z \end{alignat}

\end{document}

enter image description here

Ulrike Fischer
  • 327,261