如何设置 NiceMatrix 块的垂直对齐?

如何设置 NiceMatrix 块的垂直对齐?

在 中NiceMatrix,有没有办法设置块的垂直位置?

例如,

\documentclass{standalone}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{ccc}
\Block{3-2}{A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}

\end{document}

此代码产生:

在此处输入图片描述

我可以使用 l、c 和 r 选项设置水平位置,但是有没有办法设置块的垂直位置?

答案1

更新

\HighBlock该答案中给出的命令和代码所实现的功能\LowBlock已集成到 6.14 版nicematrix(2023/02/18)中,其中包含命令 的键T 和。B\Block

\documentclass{article}
\usepackage{nicematrix}

\begin{document}


With the key \verb|T|

\begin{NiceTabular}{ccc}
\Block[T]{3-2}{\Huge A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}

\bigskip
With the key \verb|B|

\begin{NiceTabular}{ccc}
\Block[B]{3-2}{\Huge A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}
    
\end{document}

与往常一样nicematrix,您需要进行多次编译。

UPDATE 代码的输出

对于这些键TB边距(上方和下方)等于inner ysepPGF/Tikz 的参数。下面是将该参数设置为 0 pt 的示例。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\NiceMatrixOptions{hvlines,rules/color=gray!15}

\pgfset{inner sep = 0pt}

With the key \verb|T|

\begin{NiceTabular}{ccc}
\Block[T]{3-2}{\Huge A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}

\bigskip
With the key \verb|B|

\begin{NiceTabular}{ccc}
\Block[B]{3-2}{\Huge p} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}
    
\end{document}

第二段代码的输出('inner ysep' 设置为 0 pt)


目前,命令中没有\Block设置垂直对齐的选项。我可能会在将来的版本中添加该功能nicematrix

就目前而言,可以定义两个命令\HighBlock\LowBlock执行该作业(没有内置命令的选项\Block...)。

代码没有使用包的任何私有命令nicematrix

\documentclass{article}
\usepackage{nicematrix}

\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

With \verb|\HighBlock{3-2}{\Huge A}|

\begin{NiceTabular}{ccc}
\HighBlock{3-2}{\Huge A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}

\bigskip
With \verb|\LowBlock{3-2}{\Huge A}|

\begin{NiceTabular}{ccc}
\LowBlock{3-2}{\Huge A} & & 0 \\
& & 0 \\
& & 0 \\
0 & 0 & 0
\end{NiceTabular}

\end{document}

与往常一样nicematrix,您需要进行多次编译(因为 PGF/Tikz 节点)。

上述代码的输出


这部分是对 Avi Vajpeyi 在评论中提出的问题的回答。该版本的命令\HighBlock在块中排版一个段落。代码不使用 的内部结构nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}


\ExplSyntaxOn
\makeatletter

\NewDocumentCommand { \HighBlock } { O { } m m }
  {
    \HighBlockAux { #2 } { #3 }
    \Block [ #1 ] { #2 } { } 
  }

\NewDocumentCommand { \HighBlockAux } { > { \SplitArgument { 1 } { - } } m m }
  {
    \tl_gput_right:Nx \g_nicematrix_code_after_tl 
      {
        \__Avi_high_block:nnnnn
          { \int_use:c { c@iRow } }  % row of first cell
          { \int_use:c { c@jCol } }  % col of first cell
          #1                         % { i } { j } where i/j is the number of rows/cols
          { \exp_not:n { #2 } }      % contents of the block
      }
  }

\cs_new_protected:Nn \__Avi_high_block:nnnnn
  {
    \dim_zero_new:N \l__Avi_y_top_line_dim 
    \dim_zero_new:N \l__Avi_left_dim
    \dim_zero_new:N \l__Avi_right_dim
    \dim_zero_new:N \l__Avi_width_block_dim 
    %
    \pgfpicture
    \pgfrememberpicturepositiononpagetrue
    \pgf@relevantforpicturesizefalse
    \pgfpointanchor { nm - \NiceMatrixLastEnv - row - #1 - base } { center }
    \dim_set_eq:NN \l__Avi_y_top_line_dim \pgf@y    
    \pgfpointanchor { nm - \NiceMatrixLastEnv - #2 } { center }
    \dim_set_eq:NN \l__Avi_left_dim \pgf@x  
    \pgfpointanchor { nm - \NiceMatrixLastEnv - \int_eval:n { #2 + #4 } } { center }
    \dim_set_eq:NN \l__Avi_right_dim \pgf@x 
    \pgftransformshift 
      { 
        \pgfpoint 
          { \dim_eval:n { (\l__Avi_left_dim + \l__Avi_right_dim) / 2 } } 
          { \l__Avi_y_top_line_dim } 
      }
    \dim_set:Nn \l__Avi_width_block_dim 
      { \l__Avi_right_dim - \l__Avi_left_dim - 2 \tabcolsep }
    \pgfnode
      { rectangle }
      { base }
      { 
        \begin { minipage } [ t ] { \l__Avi_width_block_dim }
        #5
        \end { minipage }
      }
      { }
      { }
    \endpgfpicture
  }

\makeatother
\ExplSyntaxOff

\begin{NiceTabular}{ccccccccc}[hvlines]
\HighBlock{7-8}{The text is that block is automaticaly 
wrapped to the size of the block (but no space
is created by the block itself!} 
  &   &   &   &   &   &   &   & 1 \\
  &   &   &   &   &   &   &   & 2 \\
  &   &   &   &   &   &   &   & 3 \\
  &   &   &   &   &   &   &   & 4 \\
  &   &   &   &   &   &   &   & 5 \\
  &   &   &   &   &   &   &   & 6 \\
  &   &   &   &   &   &   &   & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 0
\end{NiceTabular}

\end{document}

上述代码的输出

相关内容