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 :
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 :
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 :
Does anyone see a solution/improvement to this ?




matrixlibrary, more later (if needed), now I had to do my work. – Zarko Apr 02 '20 at 09:05matrix, but I also had problems with the rounded corners – Scrimbibete Apr 02 '20 at 09:15