8

I want to make a matrix whose entries are matrices of different size. If I use \left[ and \right] for the entries, the line-widths of the brackets are unequal, when the inner matrices are of uneven size. How do I make the brackets look the same?

I attach a minimal example and an image of the produced output where the problem is obvious.

Brackets with different width

\documentclass{article}
\begin{document}
\[
\left[
\begin{array}{c}
\left[\begin{array}{ccc}1&0&0\\2&0&0\\1&0&0\end{array}\right]\\
\left[\begin{array}{ccc}1&1&0\\1&1&0\end{array}\right]\\
\left[\begin{array}{ccc}1&2&1\end{array}\right]
\end{array}
\right]
\]
\end{document}
8

Maybe a TikZ matrix can help in this case, but you can't use right and left delimeter options, otherwise you got the same problem, draw an appropriate line instead. With line width=... you can choose the thickness of the line.

If you want to create a new command to shorten the typing, you have to use ampersand replacement=\& and use \& as cell separator like \mym here:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mymatrix/.style={
        matrix of math nodes,
        inner ysep= 0pt,
        nodes ={inner xsep=4pt,
            inner ysep=3pt},
        ampersand replacement=\&, 
        }
    }

\newcommand{\thicklen}{2.5pt}
\newcommand{\mym}[1]{%
    \begin{tikzpicture}[line width=.7pt]
    \matrix[mymatrix](a){#1\\};
    \draw ([xshift=\thicklen]a.north west)  -- ++(-\thicklen,0) -- (a.south west) -- ++(\thicklen,0);
    \draw ([xshift=-\thicklen]a.north east)  -- ++(\thicklen,0) -- (a.south east) -- ++(-\thicklen,0);
    \end{tikzpicture}}

\begin{document}
\[
\left[
\begin{array}{c}
\mym{1\&0\&0\\2\&0\&0\\1\&0\&0}\\
\mym{1\&1\&0\\1\&1\&0}\\
\mym{1\&2\&1}\\
\end{array}
\right]
\]
\end{document}

enter image description here

Edit: to align the nodes you can use align=center but set also a text width otherwise the three matrix may be not consistent.

To have also the external bracket aligned you can draw also it with TikZ, using overlay and remember picture. Even if at this point to have three different matrices is uselessly complicated. You can reach the same result with only one matrix, see the solution of the second edit.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mymatrix/.style={
        matrix of math nodes,
        inner ysep= 0pt,
        nodes ={
            text width=1.5em,
            align=center,
            inner xsep=4pt,
            inner ysep=3pt},
        ampersand replacement=\&, 
        }
    }

\newcommand{\thicklen}{2.5pt}
\newcommand{\mym}[2]{%
    \begin{tikzpicture}[remember picture,line width=.7pt]
    \matrix[mymatrix](#2){#1\\};
    \draw ([xshift=\thicklen]#2.north west)  -- ++(-\thicklen,0) -- (#2.south west) -- ++(\thicklen,0);
    \draw ([xshift=-\thicklen]#2.north east)  -- ++(\thicklen,0) -- (#2.south east) -- ++(-\thicklen,0);
    \end{tikzpicture}}

\begin{document}
\[
\begin{array}{c}
\mym{123\&0\&0\\2\&0\&0\\1\&0\&0}{a}\\
\mym{1\&123\&0\\1\&1\&0}{b}\\
\mym{1\&2\&123}{c}
\end{array}
\]
\begin{tikzpicture}[overlay, remember picture, line width=1pt]
    \draw ([xshift=-2*\thicklen]a.north west)  -- ++(-\thicklen,0) -- ([xshift=-3*\thicklen]c.south west) -- ++(\thicklen,0);
    \draw ([xshift=2*\thicklen]a.north east)  -- ++(\thicklen,0) -- ([xshift=3*\thicklen]c.south east) -- ++(-\thicklen,0);
\end{tikzpicture}
\end{document}

enter image description here

Second edit: Having only one matrix, totally drawn in TikZ, you can avoid using ampersand replacement=\& and all is simpler.

Of course, you can choose the line width you prefer, here is only an example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mymatrix/.style={
        matrix of math nodes,
        inner ysep= 0pt,
        nodes ={
            text width=1.5em,
            align=center,
            inner xsep=2pt,
            inner ysep=3pt} 
        }
    }

\newcommand{\thicklen}{2.5pt}

\begin{document}
\begin{tikzpicture}
    \matrix[mymatrix](a){
        123&0&0\\
        2&0&0\\
        1&0&0\\[4pt]
        1&123&0\\
        1&1&0\\[4pt]
        1&2&123\\};
    \foreach \start/\end in {1/3, 4/5, 6/6}{%
        \draw[line width=.7pt] ([xshift=-\thicklen]a-\start-1.north west)  -- ++(-\thicklen,0) -- ([xshift=-2*\thicklen]a-\end-1.south west) -- ++(\thicklen,0);    
        \draw[line width=.7pt] ([xshift=\thicklen]a-\start-3.north east)  -- ++(\thicklen,0) -- ([xshift=2*\thicklen]a-\end-3.south east) -- ++(-\thicklen,0);}
    \draw[line width=1pt] ([xshift=-2*\thicklen]a.north west)  -- ++(-\thicklen,0) -- ([xshift=-3*\thicklen]a.south west) -- ++(\thicklen,0);
    \draw[line width=1pt] ([xshift=2*\thicklen]a.north east)  -- ++(\thicklen,0) -- ([xshift=3*\thicklen]a.south east) -- ++(-\thicklen,0);
\end{tikzpicture}
\end{document}

enter image description here

  • Great. Two minor things still. 1) Is it possible that the outer brackets \left[ and \right] are symmetrically around the inner ones (the outer bracket does not go above the inner ones). 2) How to add an alignment option to the matrix mym (I suppose this is standard tikz stuff, but i never used tikz before). – tommsch Dec 31 '18 at 11:08
  • @tommsch I think the better solution is the one of my second edit. – CarLaTeX Dec 31 '18 at 12:37
  • @CarLaTeX It's really nice to know English well. In so many questions I don't understand what they ask. My compliments. – Sebastiano Dec 31 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.