tabularray icon indicating copy to clipboard operation
tabularray copied to clipboard

How to let one of columns be with the `m` vertical alignment?

Open DaftDogeee opened this issue 2 years ago • 7 comments

In the following MWE, I just want to let the first column be with the m vertical alignment. I have tried all alignment options, and there is no effects.

\documentclass[journal]{IEEEtran}

\usepackage{blindtext}
\usepackage{tabularray}
\UseTblrLibrary{varwidth}

\begin{document}

\begin{table*}
\centering
\caption{Test}
\begin{tblr}{
    width = 0.95\linewidth,
    colspec = {X[1,c,m]X[2,l,t]X[2,l,t]}, %% Notice
    hlines,
    hline{2} = {1}{-}{},
    hline{2} = {2}{-}{},
    vline{2-3},
    row{1} = {c,font=\bfseries},
    column{1} = {font=\bfseries},
    measure=vbox,
}
    & test & test \\
    test & \blindlist{itemize} & \blindlist{itemize} \\
    test & \blindlist{itemize} & \blindlist{itemize} \blindlist{itemize} \\
\end{tblr}
\end{table*}

\end{document}

截屏2023-08-18 14 06 25

DaftDogeee avatar Aug 18 '23 18:08 DaftDogeee

It seems currently there's no user interface to align the top of cells in columns 2 and 3 with each other and align the middle of cells in column 1 with the middle of highest cell in same row (but other columns). Horizontal spanning cells (with r=<n>, n >= 2) do have the effect you want, but that doesn't work for r=1 cells.

muzimuzhi avatar Aug 21 '23 17:08 muzimuzhi

A proof-of-concept attempt that adds valign=M.

Update: It's tested with tabularray 2023A (2023-03-01) and 2024A (2024-02-16).

image image

Full example

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\UseTblrLibrary{varwidth}

\usepackage{blindtext}
\usepackage{regexpatch} % for patching

\ExplSyntaxOn
\makeatletter
% tested with tabularray 2023A (2023-03-01) and 2024A (2024-02-16) only
\IfPackageAtLeastTF{tabularray}{2024-02-17}{
  \PackageWarning { patch-tabularray }
    { Experimental~patching~to~support~"valign=M"~may~fail~in~newer~versions. }
}{}

\tl_const:Nn \c__tblr_valign_M_tl {M}
\tl_new:N    \g__tblr_cell_valign_sub_tl

\cs_undefine:N \__tblr_get_cell_alignments:nn
\cs_new_protected:Npn \__tblr_get_cell_alignments:nn #1 #2
  {
    \group_begin:
    \tl_gset:Nx \g__tblr_cell_halign_tl
      { \__tblr_data_item:neen { cell } {#1} {#2} { halign } }
    \tl_set:Nx \l__tblr_v_tl
      { \__tblr_data_item:neen { cell } {#1} {#2} { valign } }
    \tl_case:NnF \l__tblr_v_tl
      {
        \c__tblr_valign_t_tl
          {
            \tl_gset:Nn \g__tblr_cell_valign_tl {m}
            \tl_gset:Nn \g__tblr_cell_middle_tl {t}
            \tl_gclear:N \g__tblr_cell_valign_sub_tl
          }
        \c__tblr_valign_m_tl
          {
            \tl_gset:Nn \g__tblr_cell_valign_tl {m}
            \tl_gset:Nn \g__tblr_cell_middle_tl {m}
            \tl_gclear:N \g__tblr_cell_valign_sub_tl
          }
        \c__tblr_valign_M_tl
          {
            \tl_gset:Nn \g__tblr_cell_valign_tl {m}
            \tl_gset:Nn \g__tblr_cell_valign_sub_tl {M}
            \tl_gset:Nn \g__tblr_cell_middle_tl {t}
          }
        \c__tblr_valign_b_tl
          {
            \tl_gset:Nn \g__tblr_cell_valign_tl {m}
            \tl_gset:Nn \g__tblr_cell_middle_tl {b}
            \tl_gclear:N \g__tblr_cell_valign_sub_tl
          }
      }
      {
        \tl_gset_eq:NN \g__tblr_cell_valign_tl \l__tblr_v_tl
        \tl_gclear:N \g__tblr_cell_middle_tl
      }
    \group_end:
  }

% \tracingxpatches

% make \__tblr_build_cell_content:NN patchable
\char_set_catcode_space:n {32}
\xpatchcmd \__tblr_build_cell_content:NN
  {\hrule height 0pt}
  {\hrule height0pt}
  {} {\PackageError{patch-tabularray}{Patch~failed.}{}}
\char_set_catcode_ignore:n {32}

\IfPackageAtLeastTF{tabularray}{2024-02-16}{
  % 2024A or newer
  % use a longer patten in the hope of failing fast
  \xpatchcmd \__tblr_build_cell_content:NN
    {
      \int_compare:nNnT { \lTblrCellRowSpanTl } < {2}
        {
          \box_set_ht:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-upper } }
          \box_set_dp:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-lower } }
        }
    }
    {
      \bool_lazy_and:nnT
        { \int_compare_p:nNn { \lTblrCellRowSpanTl } < {2} }
        { ! \tl_if_eq_p:NN \c__tblr_valign_M_tl \g__tblr_cell_valign_sub_tl }
        {
          \box_set_ht:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-upper } }
          \box_set_dp:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-lower } }
        }
    }
    { } { \PackageError { patch-tabularray } { Patch~failed } {} }
}{
  % prior to 2024A
  \xpatchcmd \__tblr_build_cell_content:NN
    {
      \int_compare:nNnT { \l__tblr_cell_rowspan_tl } < {2}
        {
          \box_set_ht:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-upper } }
          \box_set_dp:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-lower } }
        }
    }
    {
      \bool_lazy_and:nnT
        { \int_compare_p:nNn { \l__tblr_cell_rowspan_tl } < {2} }
        { ! \tl_if_eq_p:NN \c__tblr_valign_M_tl \g__tblr_cell_valign_sub_tl }
        {
          \box_set_ht:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-upper } }
          \box_set_dp:Nn \l__tblr_a_box
            { \__tblr_data_item:nen { row } {#1} { @row-lower } }
        }
    }
    { } { \PackageError { patch-tabularray } { Patch~failed } {} }
}

\keys_define:nn { tblr-column }
  {
    M .meta:n = { valign = M },
  }

\makeatother
\ExplSyntaxOff

\parindent=0pt

\begin{document}
\def\test#1{%
  \begin{tblr}{
    hlines, vlines,
    colspec={X[1,c,#1] *2{X[2,t]}},
    column{1}={bg=cyan!20, font=\ttfamily},
    measure=vbox
  }
       #1 & {test \par test} & {test \par test \par test} \\
       #1 & {test \par test} & {test \par test \par test} \\
  \end{tblr}
  \par\medskip
}

\test{valign=m}
\test{valign=M}

\begin{tblr}{
    width = 0.95\linewidth,
    colspec = {X[1,c,M]X[2,l,t]X[2,l,t]}, %% Notice
    hlines,
    hline{2} = {1}{-}{},
    hline{2} = {2}{-}{},
    vline{2-3},
    row{1} = {c,font=\bfseries},
    column{1} = {font=\bfseries},
    measure=vbox,
}
         & test                & test                                    \\
    test & \blindlist{itemize} & \blindlist{itemize}                     \\
    test & \blindlist{itemize} & \blindlist{itemize} \blindlist{itemize} \\
\end{tblr}
\end{document}

muzimuzhi avatar Aug 21 '23 19:08 muzimuzhi

A proof-of-concept attempt that adds valign=M.

image

Many thanks for your solution! I also test your code with Package: tabularray 2022-06-01 v2022B Typeset tabulars and arrays with LaTeX3, and it still works well.

DaftDogeee avatar Aug 21 '23 20:08 DaftDogeee

@muzimuzhi I notice that there is another solution for the similar issue #436. I just want to know which method is better.

DaftDogeee avatar Aug 29 '23 16:08 DaftDogeee

The situation here is more general than in #436, hence the solution here is more general too. That is, valign=M is expected to work for both issues, but \rotatebox[origin=c]{<degree>}{<text>} only works for #436.

muzimuzhi avatar Aug 29 '23 16:08 muzimuzhi

It seems that the new version of tabularray (2024A) has broken this script, where notably \l__tblr_cell_rowspan_tl no longer exists. Did valign=M get a sequel, or do we need to adapt the new script?

! Undefined control sequence.
<argument> \l__tblr_cell_rowspan_tl 
                         
l.328 \end
        {longtblr}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Missing number, treated as zero.
<to be read again> 
<
l.328 \end
        {longtblr}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

BenjaminGalliot avatar Mar 27 '24 14:03 BenjaminGalliot

@BenjaminGalliot I updated the experimental patching in https://github.com/lvjr/tabularray/issues/434#issuecomment-1686957209 to track changes in tabularray 2024A (2024-02-16). It now works with both 2023A and 2024A.

muzimuzhi avatar Mar 27 '24 16:03 muzimuzhi