4

I am trying to achieve a table similar to the picture below. I have almost made it, but I can't figure out how to put the row with "a_{1n} ..." below the matrix so that it matches to rows.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,matrix}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,text width=3em,align=center},
  text depth=1.25ex,
  text height=2.5ex,
  nodes in empty cells,
  left delimiter=[,
  right delimiter={]},
  ampersand replacement=\&
}
}

\begin{document}

\[
A=
\begin{tikzpicture}[baseline,decoration=brace]
\matrix (m) [table] {
    1 \& 0 \& \cdots \& 0 \& \lvert \& 1 \& 1 \& \cdots \& 1 \\
    0 \& 1 \& \cdots \& 0 \& \lvert  \& 0 \& 0 \& \cdots \& 0 \\
    \vdots \& \vdots \&  \ddots \& 0 \& \lvert \& \vdots \& \vdots \&\ddots\& \vdots \\
    0 \& 0 \& \cdots \& 1 \& \lvert \& 0 \& 0 \& \cdots \& 0 \\
    \textrm{--} \& \textrm{--} \& \textrm{--} \& \textrm{--} \& \textrm{--}\& \textrm{--} \& \textrm{--} \& \textrm{--} \& \textrm{--} \\
    0 \& 0 \& \cdots \& 0 \& \lvert \& 1 \& 0 \& \cdots \& 1 \\
    0 \& 0 \& \cdots \& 0 \& \lvert  \& 0 \& 1 \& \cdots \& 0 \\
    \vdots \& \vdots \&  \ddots \& 0 \& \lvert \& \vdots \& \vdots \&\ddots\& \vdots \\
    1 \& 1 \& \cdots \& 1 \& \lvert \& 0 \& 0 \& \cdots \& 1 \\
};
    \draw[decorate,transform canvas={xshift=-1.4em},thick] (m-4-1.south west) -- node[left=2pt] {$m$} (m-1-1.north west);    
    \draw[decorate,transform canvas={xshift=-1.4em},thick] (m-9-1.south west) -- node[left=2pt] {$n$} (m-6-1.north west);
    \draw[decorate,transform canvas={yshift=0.5em},thick] (m-1-1.north west) -- node[above=2pt] {$m$} (m-1-4.north east);
    \draw[decorate,transform canvas={yshift=0.5em},thick] (m-1-6.north west) -- node[above=2pt] {$n-1$} (m-1-9.north east);
\end{tikzpicture}
\]

\end{document}

2 Answers 2

4

Here are two solutions:

  1. First one completing your code (full tikz matrix)
  2. Second one with both nicematrix and tikz

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,matrix,calc,positioning}
\usepackage{nicematrix}
\tikzset{ 
    table/.style={
        matrix of math nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={rectangle,text width=3em,align=center},
        text depth=1.25ex,
        text height=2.5ex,
        nodes in empty cells,
        left delimiter=[,
        right delimiter={]},
        ampersand replacement=\&
    }
}

\begin{document}
    \[
    A=
    \begin{tikzpicture}[baseline,decoration=brace]
    \matrix (m) [table] {
        1 \& 0 \& \cdots \& 0 \& \& 1 \& 1 \& \cdots \& 1 \\
        0 \& 1 \& \cdots \& 0 \& \& 0 \& 0 \& \cdots \& 0 \\
        \vdots \& \vdots \&  \ddots \& 0 \& \& \vdots \& \vdots \&\ddots\& \vdots \\
        0 \& 0 \& \cdots \& 1 \& \& 0 \& 0 \& \cdots \& 0 \\
        0 \& 0 \& \cdots \& 0 \& \& 1 \& 0 \& \cdots \& 1 \\
        0 \& 0 \& \cdots \& 0 \&  \& 0 \& 1 \& \cdots \& 0 \\
        \vdots \& \vdots \&  \ddots \& 0 \& \& \vdots \& \vdots \&\ddots\& \vdots \\
        1 \& 1 \& \cdots \& 1 \& \& 0 \& 0 \& \cdots \& 1 \\
    };
    \node [above=.5em of m-1-1.north, anchor=south] {$a_{1n}$};
    \node [above=.5em of m-1-2.north, anchor=south] {$a_{2n}$};
    \node [above=.5em of m-1-3.north, anchor=south] {$\ldots$};
    \node [above=.5em of m-1-4.north, anchor=south] {$a_{mn}$};
    \node [above=.5em of m-1-6.north, anchor=south] {$a_{11}$};
    \node [above=.5em of m-1-7.north, anchor=south] {$a_{12}$};
    \node [above=.5em of m-1-8.north, anchor=south] {$\ldots$};
    \node [above=.5em of m-1-9.north, anchor=south] {$a_{1n-1}$};
    \draw[decorate,transform canvas={xshift=1.4em},thick] (m-1-9.north east) -- node[right=2pt] {$m$} (m-4-9.south east); % right
    \draw[decorate,transform canvas={xshift=1.4em},thick] (m-5-9.north east) -- node[right=2pt] {$n$} (m-8-9.south east); % right
    \draw[decorate,transform canvas={yshift=-.5em},thick] (m-8-4.south east) -- node[below=2pt] {$m$} (m-8-1.south west); % below
    \draw[decorate,transform canvas={yshift=-.5em},thick] (m-8-9.south east) -- node[below=2pt] {$n-1$} (m-8-6.south west); % below
    \draw [dotted] (m-1-5.north) -- (m-8-5.south);
    \draw [dotted] (m-4-1.south west) -- (m-4-9.south east);
    \end{tikzpicture}
    \]
    
    $A''=\begin{pNiceMatrix}[first-row,%
    code-before =
    {
        \tikz \draw[decorate, decoration=brace,transform canvas={xshift=1.4em},thick] (row-1-|col-10) -- node[right=2pt] {$m$} (row-5-|col-10); % right
        \tikz \draw[decorate, decoration=brace,transform canvas={xshift=1.4em},thick] (row-5-|col-10) -- node[right=2pt] {$n$} (row-9-|col-10); % right
        \tikz \draw[decorate, decoration=brace,transform canvas={yshift=-.5em},thick] (row-9-|col-5) -- node[below=2pt] {$m$} (row-9-|col-1); % below
        \tikz \draw[decorate, decoration=brace,transform canvas={yshift=-.5em},thick] (row-9-|col-10) -- node[below=2pt] {$n-1$} (row-9-|col-6); % below
    }
    ]
        a_{1n} & a_{2n} & \Cdots & a_{mn} && a_{11} & a_{12} & \Cdots & a_{1n-1}\\
        1 & 0 & \Cdots & 0 & \Vdotsfor{8} & 1 & 1 & \Cdots & 1\\
        0 & 1 & \Cdots & 0 &              & 0 & 0 & \Cdots & 0\\
        \Vdots & \Vdots & \Ddots & \Vdots & & \Vdots & \Vdots & \Ddots & \Vdots\\
        0 & 0 & \Cdots & 1 & & 0 & 0 & \Cdots & 0\\
        \hdottedline
        0 & 0 & \Cdots & 0 & & 1 & 0 & \Cdots & 0\\
        0 & 0 & \Cdots & 0 & & 0 & 1 & \Cdots & 0\\
        \Vdots & \Vdots & \Ddots & \Vdots & & \Vdots & \Vdots & \Ddots & \Vdots\\
        1 & 1 & \Cdots & 1 & & 0 & 0 & \Cdots & 1
    \end{pNiceMatrix}$
    
\end{document}

Result

enter image description here

2
  • Thank you for these great solutions. The second solution is very elegant and with less code. Feb 8, 2021 at 16:01
  • 2
    For the solution with nicematrix, I suggest to use the \CodeAfter instead of the code-before because the code-before requires one more compilation. Feb 14, 2021 at 18:39
2

Here is what you can do with the latest version of nicematrix.

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

\begin{document}

$A=\begin{pNiceArray} [first-row,last-col,xdots/line-style=loosely dotted] {cccc:cccc}
a_{1n} & a_{2n} & \Cdots & a_{mn} & a_{11} & a_{12} & \Cdots & a_{1n-1} \hspace*{-6pt}\\
1      & 0      & \Cdots & 0      & 1      & 1      & \Cdots & 1        & \Block{4-1}{\quad m}\\
0      & 1      & \Cdots & 0      & 0      & 0      & \Cdots & 0 \\
\Vdots & \Vdots & \Ddots & \Vdots & \Vdots & \Vdots & \Ddots & \Vdots \\
0      & 0      & \Cdots & 1      & 0      & 0      & \Cdots & 0 \\
\hdottedline
0      & 0      & \Cdots & 0      & 1      & 0      & \Cdots & 0        & \Block{4-1}{\quad n}\\
0      & 0      & \Cdots & 0      & 0      & 1      & \Cdots & 0 \\
\Vdots & \Vdots & \Ddots & \Vdots & \Vdots & \Vdots & \Ddots & \Vdots \\
1      & 1      & \Cdots & 1      & 0      & 0      & \Cdots & 1
\CodeAfter
  \UnderBrace{last-1}{last-4}{m\vphantom{1}}[shorten,yshift=1.1mm]
  \UnderBrace{last-5}{last-8}{n-1}[shorten,yshift=1.1mm]
  \SubMatrix{.}{1-1}{4-last}{\}}[xshift=2mm]
  \SubMatrix{.}{5-1}{last-last}{\}}[xshift=2mm]
\end{pNiceArray}$

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .