我正在开始编写一个相当大的文档,其中我将不得不在说明图中绘制许多数组/矩阵。我一直在尝试编写一个通用宏来执行此操作,要求如下:
- 能够绘制 1D 或 2D 数组。它们的大小永远不会太大(比如最多 10/15 个元素);
- 能够作为节点访问每个元素的位置,以便轻松地从它们/向它们绘制箭头;
- 如果需要的话,可以指定每个元素的颜色
- 能够在阵列外部设置圆角
到目前为止,我已经rectangle split
按照以下方式使用节点:
\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}
它给出了像这样的漂亮数组:
但不能用于二维数组,而且这个\nodepart{}
东西有点麻烦,虽然可以忍受。我也写过基本的\foreach
东西,效果很好,但我无法用它获得圆角。
有人有什么建议吗?提前谢谢。
编辑1
回答评论:我也尝试过使用matrix
,但圆角也遇到了问题。下面是带有的 MWE matrix
,如果有人知道如何为其添加圆角:)
\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}
编辑2
我想到了一些办法matrix
,并在这里偷来了答案:具有不同圆角的TikZ矩形节点
\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}
结果很好:
我很确定有几个不会起作用,其中之一就是如果你改变其中一个角的颜色(例如\node[mTopLeft,fill=orange] {3};
,填充区域不是圆的:
有人看到这个问题的解决方案/改进方法吗?
答案1
我想出了一个不太优雅,但还算令人满意,但足够了的解决方案:
\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}
然后就可以像平常一样轻松地使用节点,这里(m)
是矩阵,(n1)
这里是左上角的节点,等等。
编辑:抱歉颜色太丑了