Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LinqLover opened this issue Sep 19, 2024 · 15 comments
Labels
other package Used with other packages

Comments

@LinqLover
Copy link

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 (krono/swathesis#64). A workaround is available (not loading colortbl), so I'm just reporting this for others.

PS: Thanks for this awesome package!

@muzimuzhi
Copy link
Collaborator

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

@muzimuzhi muzimuzhi added the other package Used with other packages label Sep 23, 2024
@lvjr
Copy link
Owner

lvjr commented Nov 12, 2024

This needs to be fixed in siunitx, since we must not patch private macros in siunitx.

@lvjr lvjr closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
@josephwright
Copy link

@lvjr Is there a marker I can pick up to know we are in a tblr column not a standard array one?

@lvjr
Copy link
Owner

lvjr commented Nov 12, 2024

@josephwright How about checking \g_tblr_level_int (a public integer for the nesting level of tabularray tables)?

@josephwright
Copy link

I have a fix in hand - I'll patch in the next release (probably next week).

@muzimuzhi
Copy link
Collaborator

Would checking nesting level (\g_tblr_level_int) fail in rare cases, for example when an array using S column is nested in tblr? Maybe need some words in doc.

@josephwright
Copy link

Would checking nesting level (\g_tblr_level_int) fail in rare cases, for example when an array using S column is nested in tblr? Maybe need some words in doc.

There's a reason I asked if I can pick up being in a tblr cell :)

@josephwright
Copy link

@lvjr I can't add a test at the moment in siunitx as tabularray has loads of failures when running expl3 debugging :(

@muzimuzhi
Copy link
Collaborator

@josephwright The dev version of tabularray.sty should work with \debug_on:n { check-declarations }, see ecafbe9 and #22.

But \debug_on:n { deprecation } will keep raising errors since Jianrui (lvjr) decided to support the most-recent four TeX Live releases, see 77ca1bc.

@josephwright
Copy link

@muzimuzhi Thanks - I might have to test without deprecation checking for this case then - should be safe enough in this one case.

@lvjr
Copy link
Owner

lvjr commented Nov 12, 2024

Would checking nesting level (\g_tblr_level_int) fail in rare cases, for example when an array using S column is nested in tblr? Maybe need some words in doc.

There's a reason I asked if I can pick up being in a tblr cell :)

But a tblr cell could have an array in it. I can't see what is the difference.

@josephwright
Copy link

@lvjr That's why I was asking about a marker - I wondered if there was flag that was on in a cell but turned off for nested tabulars - I take it 'no'

@lvjr
Copy link
Owner

lvjr commented Nov 13, 2024

That's why I was asking about a marker - I wondered if there was flag that was on in a cell but turned off for nested tabulars - I take it 'no'

For nested tabular/array environments, I guess we can check \@currenvir.

@muzimuzhi
Copy link
Collaborator

Ideally, tabularray can provide something for siunitx like

\prg_new_conditional:Npnn \tblr_if_si_cell: { p, T, F, TF }
  {
    \bool_lazy_and:nnTF
      { \l__tblr_if_si_column_bool } % need to be set true by "si" column type
      { \tblr_if_tblr_env_p: }
      { \prg_return_true: }
      { \prg_return_false: }
  }

\prg_new_conditional:Npnn \tblr_if_tblr_env: { p, T, F, TF }
  {
    \tl_if_exist:cT { l__tblr_default_ \@currenvir _inner_tl }
  }

Alternatively, siunitx can provide a new option that the si column type will always set.

@lvjr
Copy link
Owner

lvjr commented Nov 13, 2024

Alternatively, siunitx can provide a new option that the si column type will always set.

After reading again your workaround code above, I believe this is the most reliable solution: siunitx adds a new option such as disable-colortbl-fix and tabularray applies \sisetup{disable-colortbl-fix} in its S cells.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other package Used with other packages
Projects
None yet
Development

No branches or pull requests

4 participants