1

I am starting a pretty big documentation, in which I will have to draw many arrays/matrices in explanatory drawings. I have been trying to write a generic macro to do this for quite some time, with the following requirements :

  • Be able to draw either 1D or 2D arrays. Their size will never be large (let's say max 10/15 elements);
  • Be able to access the position of each element as a node, to easily draw arrows from/to them;
  • Be able to specify the color of each element if I need to
  • Be able to have rounded corners on the outside of the array

Up to now, I have used rectangle split nodes, in the following fashion:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}

\begin{tikzpicture}[
                label/.style={text depth=.3\baselineskip,text height=.5\baselineskip},
                arrow/.style={thick,mygray2,stealth-}]

    % List
    \node[  rectangle split,
            rectangle split parts=5,
            rectangle split horizontal,
            draw=red,
            thick,
            rounded corners,
            rectangle split part fill=blue,
            rectangle split draw splits=true] (simplex) at (0,0) {  \nodepart{one}1
                                                     \nodepart{two}3
                                                    \nodepart{three}1
                                                    \nodepart{four}0
                                                    \nodepart{five}0};

\end{tikzpicture}

\end{document}

which gives nice arrays like that :

enter image description here

but cannot be used for 2D arrays, and the \nodepart{} thing is a bit cumbersome, although tolerable. I have also written basic \foreach thing, which works well, but I could not obtain the rounded corners with it.

Does anyone have a suggestion ? Thanks in advance.

EDIT 1

Answering to a comment : I have also tried using matrix, but had trouble with the rounded corners as well. Below is a MWE with matrix, if someone knows how to add the rounded corners to it :)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}

\begin{tikzpicture}[mnode/.style={  draw=red, thick,
                            fill=blue,
                            minimum width=0.5cm, 
                            minimum height=0.5cm}]

    \matrix [   nodes=mnode,
            row sep=-2*\pgflinewidth,
            column sep=-2*\pgflinewidth]
{
\node {1}; & \node{3}; & \node {1}; & \node{0};& \node{0}; \\
};

\end{document}

EDIT 2

I came up with something using matrix and an answer stolen here : TikZ rectangular node with different rounded corners

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}

 \tikzset{
    mStyle/.style={row sep=-2*\pgflinewidth, column sep=-2*\pgflinewidth},
    mWH/.style n args={2}{minimum width=#1cm, minimum height=#2cm},
    mNode/.style={draw=red, thick, fill=blue, mWH},
    mRounded/.style n args={4}{append after command={\pgfextra
                \draw[mNode, sharp corners]% 
            (\tikzlastnode.west)% 
            [rounded corners=#1pt] |- (\tikzlastnode.north)% 
            [rounded corners=#2pt] -| (\tikzlastnode.east)% 
            [rounded corners=#3pt] |- (\tikzlastnode.south)% 
            [rounded corners=#4pt] -| (\tikzlastnode.west);%
        \endpgfextra}},
    mTopLeft/.style={mRounded={3}{0}{0}{0}},
    mTopRight/.style={mRounded={0}{3}{0}{0}},
    mBotLeft/.style={mRounded={0}{0}{0}{3}},
    mBotRight/.style={mRounded={0}{0}{3}{0}},
    mLeft/.style={mRounded={3}{0}{0}{3}},
    mRight/.style={mRounded={0}{3}{3}{0}},
    mReg/.style={mRounded={0}{0}{0}{0}},
}

\begin{tikzpicture}

    \matrix[mStyle, nodes={mWH={0.5}{0.5}}]{
        \node[mTopLeft] {3};    & \node[mReg]{2};   & \node[mTopRight]{1};  \\
        \node[mReg] {2};    & \node[mReg]{3};   & \node[mReg]{1};       \\
        \node[mBotLeft] {1};    & \node[mReg]{1};   & \node[mBotRight]{1};  \\
};

\end{tikzpicture}

\end{document}

which yields a nice result :

enter image description here

I am pretty sure several won't work though, one of them being that if you change the color of one of the corners (for example \node[mTopLeft,fill=orange] {3};, the filling region is not rounded :

enter image description here

Does anyone see a solution/improvement to this ?

5
  • As always, pleas help us to help you ... extend your code snippet to MWE (Minimal Working Example, a small but complete, compilable document).
    – Zarko
    Commented Apr 2, 2020 at 8:51
  • Where are defined used colors?
    – Zarko
    Commented Apr 2, 2020 at 9:00
  • @Zarko Sorry, I edited the question. As it is an open question (I am just giving an example of what I tried before), I am not asking for help to fix this specific code, so I did not think it made sense. But you are right. Commented Apr 2, 2020 at 9:01
  • o.k., Ttray to use matrix library, more later (if needed), now I had to do my work.
    – Zarko
    Commented Apr 2, 2020 at 9:05
  • @Zarko I spent some time already trying to achieve what I want with matrix, but I also had problems with the rounded corners Commented Apr 2, 2020 at 9:15

1 Answer 1

0

I came up with an inelegant, moderately satisfying, yet sufficient solution :

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}

\tikzset{
    mStyle/.style={row sep=-2*\pgflinewidth, column sep=-2*\pgflinewidth},
    mWH/.style n args={2}{minimum width=#1cm, minimum height=#2cm},
    mNode/.style n args={1}{draw=red, thick, fill=#1, mWH},
    mRounded/.style n args={5}{append after command={\pgfextra
                \draw[mNode={#5}, sharp corners]% 
            (\tikzlastnode.west)% 
            [rounded corners=#1pt] |- (\tikzlastnode.north)% 
            [rounded corners=#2pt] -| (\tikzlastnode.east)% 
            [rounded corners=#3pt] |- (\tikzlastnode.south)% 
            [rounded corners=#4pt] -| (\tikzlastnode.west);%
        \endpgfextra}},
    mTL/.style n args={1}{mRounded={3}{0}{0}{0}{#1}},
    mTR/.style n args={1}{mRounded={0}{3}{0}{0}{#1}},
    mBL/.style n args={1}{mRounded={0}{0}{0}{3}{#1}},
    mBR/.style n args={1}{mRounded={0}{0}{3}{0}{#1}},
    mL/.style n args={1}{mRounded={3}{0}{0}{3}{#1}},
    mR/.style n args={1}{mRounded={0}{3}{3}{0}{#1}},
    mN/.style n args={1}{mRounded={0}{0}{0}{0}{#1}},
}

\begin{tikzpicture}
    \def\b{blue}
    \def\o{orange}

    \matrix[mStyle, nodes={mWH={0.5}{0.5}}] (m) {
        \node[mTL={\o}] (n1) {3}; & \node[mN={\b}]  {2}; & \node[mTR={\b}]  {1};    \\
        \node[mN={\b}]      {2}; & \node[mN={\o}]   {3}; & \node[mN={\b}]   {1};    \\
        \node[mBL={\b}]     {1}; & \node[mN={\b}]   {1}; & \node[mBR={\b}]  {1};    \\
};

\end{tikzpicture}

\end{document}

enter image description here

It is then easy to work with the nodes as usual, here (m) for the matrix, (n1) for the top left one, and so on.

EDIT : sorry for the ugly colors

You must log in to answer this question.

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