lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
254 stars 22 forks source link

Incompatibility with `colortbl` package breaks `table-number-alignment` of siunitx columns #512

Open LinqLover opened 2 weeks ago

LinqLover commented 2 weeks ago

MWE:

\documentclass{scrbook}
\usepackage{colortbl}

\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\begin{document}

\begin{table}
\begin{tblr}{
    |
    X[si={table-alignment-mode=format,table-number-alignment=right}]
    |
    X[si={table-alignment-mode=format,table-number-alignment=center}]
    |
    X[si={table-alignment-mode=format,table-number-alignment=left}]
    |
}
    1.1 & 2.2 & 3.3 \\
\end{tblr}
\caption{Foo}
\end{table}

\end{document}

Unless you disable \usepackage{colortbl}, all columns will be incorrectly left-aligned. Other alignment options such as c seem to work. Package loading order does not matter.

I have not found a prior report of this issue. For practical relevance, I use a documentclass that does \usepackage[table]{xcolor} and this breaks my table alignment (https://github.com/krono/swathesis/issues/64). A workaround is available (not loading colortbl), so I'm just reporting this for others.

PS: Thanks for this awesome package!

muzimuzhi commented 1 week ago

It seems it's caused by the special alignment code in siunitx, when colortbl is loaded. Such special treatment is for uses in traditional tables which are affected by array package. But unfortunately, interferes with the default column alignment halign=j (justify) in tabularray.

Following example is an attempt to cancel that special treatment for tabularray tables. The third table in it also shows that you can always use l/c/r options to alter/overwrite the column alignment.

\documentclass{article}
\usepackage{colortbl}

\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\ExplSyntaxOn
\makeatletter
\cs_undefine:N \__tblr_siunitx_process:Nn
\cs_new_protected:Npn \__tblr_siunitx_process:Nn #1 #2
  {
    \__tblr_siunitx_restore_align: % <<< added
    \tl_if_head_is_group:nTF {#2}
      { #2 }
      {
        \group_begin:
        \tl_set:Nx \l_tmpa_tl
          {
            \__tblr_data_item:neen { cell }
              { \int_use:N \c@rownum } { \int_use:N \c@colnum } { si }
          }
        \exp_args:NV \sisetup \l_tmpa_tl
        #1 {#2}
        \group_end:
      }
  }

\hook_gput_code:nnn {begindocument} {tabularray}
  {
    \@ifpackageloaded { colortbl }
      {
        % quite fragile
        % see https://github.com/josephwright/siunitx/blob/2bf3bd790e58894d8de2cafcc1f8f0da9eb2af19/siunitx-table.dtx#L673-L688
        \cs_new_protected:Npn \__tblr_siunitx_restore_align:
          { \cs_set_eq:NN \__siunitx_table_align_auxi:nn \__siunitx_table_align_auxii:nn }
      }
      {
        \cs_new_eq:NN \__tblr_siunitx_restore_align: \prg_do_nothing:
      }
  }
\makeatother
\ExplSyntaxOff

\begin{document}
\parindent=-\parindent

tabular, \verb|S[...]| \\
\begin{tabular}{
  |S[table-alignment-mode=format,table-number-alignment=right]
  |S[table-alignment-mode=format,table-number-alignment=center]
  |S[table-alignment-mode=format,table-number-alignment=right]
  |
}
  {Some values} & {Some values} & {Some values} \\
  1.1           & 2.2           & 3.3           \\
  11.1          & 22.2          & 33.3
\end{tabular}

tblr \verb|S[...]|, \verb|halign=c| applied \\
\begin{tblr}{
  |S[table-alignment-mode=format,table-number-alignment=right]
  |S[table-alignment-mode=format,table-number-alignment=center]
  |S[table-alignment-mode=format,table-number-alignment=left]
  |
}
  {{{Some values}}} & {{{Some values}}} & {{{Some values}}} \\
  1.1             & 2.2             & 3.3             \\
  11.1            & 22.2            & 33.3
\end{tblr}

tblr, \verb|X[<halign>, si={...}]| \\
\begin{tblr}{
  |X[r,si={table-alignment-mode=format,table-number-alignment=right}]
  |X[c,si={table-alignment-mode=format,table-number-alignment=center}]
  |X[l,si={table-alignment-mode=format,table-number-alignment=left}]
  |
}
    1.1             & 2.2             & 3.3             \\
    11.1            & 22.2            & 33.3
\end{tblr}

tblr, \verb|X[si={...}]| \\
\begin{tblr}{
  |X[si={table-alignment-mode=format,table-number-alignment=right}]
  |X[si={table-alignment-mode=format,table-number-alignment=center}]
  |X[si={table-alignment-mode=format,table-number-alignment=left}]
  |
}
    1.1             & 2.2             & 3.3             \\
    11.1            & 22.2            & 33.3
\end{tblr}

\end{document}

image