在如何设置 NiceMatrix 块的垂直对齐?,有用户询问是否可以重新定义,\Block
使文本垂直对齐到顶部
建议的解决方案F·潘蒂尼(awesome 包的作者nicematrix
)是重新定义一个名为的自定义\HighBlock
命令\LowBlock
我对这个解决方案的注释
\HighBlock
与它所在行的内容不对齐\LowBlock
并且\Block
似乎与基线不对齐。事实上,似乎NiceTabular
环境没有将第一行的内容与文本的基线对齐,但这可以通过选项解决[baseline=1]
(仅在\Block
未使用时)
我认为提供新的选项明确允许块内容垂直对齐,这样基线就完全相同,这是件好事
\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}
答案1
您说得对:该环境{HighBlock}
不会将块的基线与顶部涉及的第一行的基线对齐。但这是设计使然。在该环境下,块标签的顶部边框放在该块所涉及的单元格的矩形的顶线上。只有等于 PGF/Tikz 参数的边距inner sep
。
以下是一个例子:
\documentclass{article}
\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
}
\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
\begin{NiceTabular}{ccc}[hvlines]
\HighBlock{4-2}{\Huge x}
\Block[fill=red!10]{4-2}{}
& & first row \\
& & second row \\
& & third row \\
& & fourth row \\
text & text \\
\end{NiceTabular}
\end{document}
我已用 突出显示该块\Block[fill=red!10]{4-2}{}
。
最新版本nicematrix
(2023/02/18 v. 6.14)提供了命令的键t
、T
、b
和用于块内容的垂直对齐。B
v-center
\Block
这是使用按键的示例t
,b
它与 OP 所期望的行为相对应。
\documentclass{article}
\usepackage{calc}
\newlength{\widthWidest}
\usepackage{nicematrix}
\NiceMatrixOptions{rules/color=[gray]{0.75}, hvlines}
\begin{document}
\setlength{\widthWidest}{\widthof{Test}}
\setlength{\tabcolsep}{0pt}
With the key \verb|t|:
\begin{NiceTabular}{p{\widthWidest} p{\widthWidest} p{\widthWidest}}
\Block[t]{3-2}{\smash{\rlap{\rule{4cm}{0.1pt}}}Test} & & Test
\\
& & 0
\\
0 & 0 & 0
\end{NiceTabular}
\vspace{1cm}
With the key \verb|b|:
\begin{NiceTabular}{p{\widthWidest} p{\widthWidest} p{\widthWidest}}
\Block[b]{3-2}{\smash{\rlap{\rule{4cm}{0.1pt}}}Test} & & 0
\\
& & 0
\\
& & Test
\\
\end{NiceTabular}
\end{document}