In How to set vertical alignment for NiceMatrix block?, a user asked as to whether it is possible to redefine \Block in such a way it aligns text vertically to the top
The solution suggested by F. Pantigny (the author of the awesome package nicematrix) is to redefine a custom commands called \HighBlock and \LowBlock
My notes on this solution
\HighBlockdoes not align with the contents of the row it appears in\LowBlockand\Blockseem to be unaligned with the baseline. In fact, it seems thatNiceTabularenvironment does not align contents of first row with the baseline of the text, but this is solved with the option[baseline=1](only when\Blockis not used)
I think it is good thing to provide new options which explicitly allow for vertical alignment of block contents in such a way baselines are exactly the same
\documentclass{article}
\usepackage{calc}
\newlength{\widthWidest}
\usepackage{nicematrix}
\NiceMatrixOptions{rules/color=[gray]{0.75}, hvlines}
\begin{document}
\ExplSyntaxOn
\makeatletter
\NewDocumentCommand { \HighBlock } { m m }
{
\tl_gput_right:Nx \g_nicematrix_code_after_tl
{
\__Alan_high_block:nnnn
{ \int_use:c { c@iRow } }
{ \int_use:c { c@jCol } }
{ #1 }
{ \exp_not:n { #2 } }
}
}
\cs_new_protected:Nn \__Alan_high_block:nnnn
{
\__Alan_analyze:w #3 \q_stop
\pgfpicture
\pgfrememberpicturepositiononpagetrue
\pgf@relevantforpicturesizefalse
\__nicematrix_qpoint:n { row - #1 }
\dim_set_eq:NN \l_tmpa_dim \pgf@y
\__nicematrix_qpoint:n { col - #2 }
\dim_set_eq:NN \l_tmpb_dim \pgf@x
\__nicematrix_qpoint:n { col - \int_eval:n { #2 + \l_tmpb_int } }
\pgftransformshift { \pgfpoint { \dim_eval:n { (\l_tmpb_dim + \pgf@x) / 2 } } { \l_tmpa_dim } }
\pgfnode
{ rectangle }
{ north }
{ #4 }
{ }
{ }
\endpgfpicture
}
\NewDocumentCommand { \LowBlock } { m m }
{
\tl_gput_right:Nx \g_nicematrix_code_after_tl
{
\__Alan_low_block:nnnn
{ \int_use:c { c@iRow } }
{ \int_use:c { c@jCol } }
{ #1 }
{ \exp_not:n { #2 } }
}
}
\cs_new_protected:Nn \__Alan_low_block:nnnn
{
\__Alan_analyze:w #3 \q_stop
\pgfpicture
\pgfrememberpicturepositiononpagetrue
\pgf@relevantforpicturesizefalse
\__nicematrix_qpoint:n { row - \int_eval:n { #1 + \l_tmpa_int } }
\dim_set_eq:NN \l_tmpa_dim \pgf@y
\__nicematrix_qpoint:n { col - #2 }
\dim_set_eq:NN \l_tmpb_dim \pgf@x
\__nicematrix_qpoint:n { col - \int_eval:n { #2 + \l_tmpb_int } }
\pgftransformshift { \pgfpoint { \dim_eval:n { (\l_tmpb_dim + \pgf@x) / 2 } } { \l_tmpa_dim } }
\pgfnode
{ rectangle }
{ south }
{ #4 }
{ }
{ }
\endpgfpicture
}
\cs_new_protected:Npn \__Alan_analyze:w #1 - #2 \q_stop
{
\int_set:Nn \l_tmpa_int { #1 }
\int_set:Nn \l_tmpb_int { #2 }
}
\makeatother
\ExplSyntaxOff
\setlength{\widthWidest}{\widthof{Test}}
\setlength{\tabcolsep}{0pt}
\begin{NiceTabular}{p{\widthWidest} p{\widthWidest} p{\widthWidest}}
\HighBlock{3-2}{\smash{\rlap{\rule{4cm}{0.1pt}}}Test} & & Test
\\
& & 0
\\
0 & 0 & 0
\end{NiceTabular}
\medskip
\begin{NiceTabular}{p{\widthWidest} p{\widthWidest} p{\widthWidest}}
\LowBlock{3-2}{\smash{\rlap{\rule{4cm}{0.1pt}}}Test} & & 0
\\
& & 0
\\
& & Test
\\
\end{NiceTabular}
\medskip
\begin{NiceTabular}{p{\widthWidest} p{\widthWidest} p{\widthWidest}}
\Block{3-1}{\smash{\rlap{\rule{4cm}{0.1pt}}}Test} & & 0
\\
& & Test
\\
0 & 0 & 0
\end{NiceTabular}
\smash{\rlap{\rule{4cm}{0.1pt}}Test}%
\begin{NiceTabular}[baseline=1]{p{\widthWidest}}
\Block{1-1}{Test}
\end{NiceTabular}
\end{document}


\smash{\rlap{\rule{4cm}{0.1pt}}Test}\begin{NiceTabular}[baseline=1]{p{\widthWidest}}Test\end{NiceTabular}– Al-Motasem Aldaoudeyeh Feb 21 '23 at 11:24tandbhas been modified in the latest version ofnicematrix(v. 6.15 2023-02-25). I think that now,tandbare as precise asbaseline=1. – F. Pantigny Feb 27 '23 at 09:50