Tabularray Alignment – How to Center a Tabularray of Math Columns

horizontal alignmenttabularray

I have an environment that relies on tabularray and the environment is not centered as I would like. I paste an example below, but first I give some preliminary info: I use this answer to show one line of a set of relations at a time in Beamer, alerting along the way. It works great, except that the table as a whole is not horizontally centered and causes problems in some of my use cases. I compare "myalign" (a new environment based on tabularray) to "align" to show how the centring differs, and the consequences (e.g., line breaks).

I use the "handout" class option in the example just to focus on the alignment. In practice, I don't use it and instead use the overlays. I also reset the equation counter to 0 to make the comparison between align and myalign closer.

\documentclass[handout]{beamer}

\usepackage{tabularray}
\UseTblrLibrary{counter}
\NewDocumentEnvironment{myalign}{+b}{
\begin{tblr}{
  column{1}={co=1},
  column{Z}={co=1},
  column{odd}={halign=r},
  column{even}={halign=l},
  colsep = 0pt,
  cells={mode=dmath},
  cell{1-Z}{1}={cmd=\action<+->},
  cell{1-Z}{2-Z}={cmd=\action<.->},
  cell{1-Z}{Z}={appto={\hfill \refstepcounter{equation}(\theequation)}}
}
#1
\end{tblr}
}{}

\begin{document}

\begin{frame}{align}
\setcounter{equation}{0}

\begin{align}
\sum_{i=1}^{n}\left(x_{i}-\overline{x}\right)^{2} & =\sum_{i=1}^{n}\left(x_{i}^{2}-2x_{i}\overline{x}+\overline{x}^{2}\right)\\
 & =\sum_{i=1}^{n}x_{i}^{2}-\sum_{i=1}^{n}2x_{i}\overline{x}+\sum_{i=1}^{n}\overline{x}^{2}
\end{align}

\end{frame}

\begin{frame}{myalign}
\setcounter{equation}{0}

\begin{myalign}
\sum_{i=1}^{n}\left(x_{i}-\overline{x}\right)^{2} & =\sum_{i=1}^{n}\left(x_{i}^{2}-2x_{i}\overline{x}+\overline{x}^{2}\right)\\
 & =\sum_{i=1}^{n}x_{i}^{2}-\sum_{i=1}^{n}2x_{i}\overline{x}+\sum_{i=1}^{n}\overline{x}^{2}
\end{myalign}

\end{frame}

\begin{frame}{align}
\setcounter{equation}{0}

\begin{align}
\pi(\omega) & =\pi(w_{L},w_{B})\\
 & =R(w_{L},w_{B})-C(w_{L})\\
 & =p_B\cdot\min(q_D(w_{B}),q_S(w_{L}))-p_S\cdot q_S(w_{L}).
\end{align}
\end{frame}

\begin{frame}{myalign}
\setcounter{equation}{0}

\begin{myalign}
\pi(\omega) & =\pi(w_{L},w_{B})\\
 & =R(w_{L},w_{B})-C(w_{L})\\
 & =p_B\cdot\min(q_D(w_{B}),q_S(w_{L}))-p_S\cdot q_S(w_{L}).
\end{myalign}
\end{frame}

\end{document}

(novice hacks I attempted that you can skip reading): I cannot tell if the problem is the centering outside the tabularray, or an issue with the column specifications. I tried adding \centering in various places, which did not seem to change anything. I tried experimenting with various column keys, as specified in the tabularray documentation. Removing the column{1}={co=1} has a noticeable effect, but then the tblrs seem so be too far to the left. I've tried setting halign to "c" (instead of the default "j" for justify), but that doesn't seem to fix things. Changing the values of rightsep also does not seem to improve on the issue.

Best Answer

What you need to do is to measure the width of the tabular with just the equations, no formula numbering or horizontal fills, then add half of \textwidth minus half the tabular width measured before as a leftsep to the first column.

\documentclass[handout]{beamer}

\usepackage{tabularray}
\UseTblrLibrary{counter}
\newsavebox\myalignbox
\NewDocumentEnvironment{myalign}{+b}{
    \sbox\myalignbox{%  
        \begin{tblr}{
                column{odd}={halign=r},
                column{even}={halign=l},
                colsep = 0pt,
                cells={mode=dmath}
            }
            #1
    \end{tblr}}
    \begin{tblr}{
            column{Z}={co=1},
            column{odd}={halign=r},
            column{even}={halign=l},
            colsep = 0pt,
            cells={mode=dmath},
            cell{1-Z}{1}={cmd=\action<+->},
            cell{1-Z}{2-Z}={cmd=\action<.->},
            cell{1-Z}{Z}={appto={\hfill \refstepcounter{equation}(\theequation)}},
            column{1}={leftsep={\dimexpr 0.5\textwidth-0.5\wd\myalignbox}}
        }
        #1
    \end{tblr}
}{}

\begin{document}
    
    \begin{frame}{align}
        \setcounter{equation}{0}
        
        \begin{align}
            \sum_{i=1}^{n}\left(x_{i}-\overline{x}\right)^{2} & =\sum_{i=1}^{n}\left(x_{i}^{2}-2x_{i}\overline{x}+\overline{x}^{2}\right)\\
            & =\sum_{i=1}^{n}x_{i}^{2}-\sum_{i=1}^{n}2x_{i}\overline{x}+\sum_{i=1}^{n}\overline{x}^{2}
        \end{align}
        
    \end{frame}
    
    \begin{frame}{myalign}
        \setcounter{equation}{0}
        
        \begin{myalign}
            \sum_{i=1}^{n}\left(x_{i}-\overline{x}\right)^{2} & =\sum_{i=1}^{n}\left(x_{i}^{2}-2x_{i}\overline{x}+\overline{x}^{2}\right)\\
            & =\sum_{i=1}^{n}x_{i}^{2}-\sum_{i=1}^{n}2x_{i}\overline{x}+\sum_{i=1}^{n}\overline{x}^{2}
        \end{myalign}
        
    \end{frame}
    
    \begin{frame}{align}
        \setcounter{equation}{0}
        
        \begin{align}
            \pi(\omega) & =\pi(w_{L},w_{B})\\
            & =R(w_{L},w_{B})-C(w_{L})\\
            & =p_B\cdot\min(q_D(w_{B}),q_S(w_{L}))-p_S\cdot q_S(w_{L}).
        \end{align}
    \end{frame}
    
    \begin{frame}{myalign}
        \setcounter{equation}{0}
        
        \begin{myalign}
            \pi(\omega) & =\pi(w_{L},w_{B})\\
            & =R(w_{L},w_{B})-C(w_{L})\\
            & =p_B\cdot\min(q_D(w_{B}),q_S(w_{L}))-p_S\cdot q_S(w_{L}).
        \end{myalign}
    \end{frame}
    
\end{document}

Edit:

To fix the problem with nesting myalign inside lists replace \textwidth with \linewidth.

It seems you need to put the \refstepcounter in preto instead of appto, and the reference will be correct only if the \label is inside a cell that has \refstepcounter.

The following layout will allow the use of \label at the first column

\NewDocumentEnvironment{myalign}{+b}{
    \sbox\myalignbox{%  
        \begin{tblr}{
                column{odd}={halign=r},
                column{even}={halign=l},
                colsep = 0pt,
                cells={mode=dmath}
            }
            #1
    \end{tblr}}
    \begin{tblr}{
            column{Z}={co=1},
            column{odd}={halign=r},
            column{even}={halign=l},
            colsep = 0pt,
            cells={mode=dmath},
            cell{1-Z}{1}={cmd=\action<+->},
            cell{1-Z}{2-Z}={cmd=\action<.->},
            cell{1-Z}{Z}={appto={\hfill (\theequation)}},
            column{1}={leftsep={\dimexpr 0.5\linewidth-0.5\wd\myalignbox}},
            column{1}={preto={\refstepcounter{equation}}},
        }
        #1
    \end{tblr}
}{}

and if you want to be able to use \label in every cell add cell{1-Z}{2-Z}={preto={\addtocounter{equation}{-1}\refstepcounter{equation}}}, to the tblr options,