我正在尝试绘制下图。在此图中,我尝试提供矩形块的尺寸。
我使用以下代码绘制图形-
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\tikzset{%
dimen/.style={<->,>=latex,thin,every rectangle node/.style={fill=white,midway,font=\sffamily}},
symmetry/.style={dashed,thin},
}
\tikzstyle{lightGrayBoxStyle}=[fill=gray!20,draw=black,minimum height=0.5cm,minimum width=0.5cm]
\begin{scope}[local bounding box=scope1]
\draw [draw=black, fill=gray,step=0.5] (0,0) grid (3,1.5) rectangle (0,0);
\node[lightGrayBoxStyle] at (2.25,0.75) (b4) {};
\node[lightGrayBoxStyle] at (1.75,0.75) (b3) {};
\node[lightGrayBoxStyle] at (1.25,0.75) (b2) {};
\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};
\draw [dimen,-] ([xshift=-1.4cm]b1.south) -- ([xshift=-1.4cm]b1.north) node {$s_1 = 1$};
\draw [dimen,<-,rotate=-90] ([yshift=-1.2cm]b1.north) -- ++(-7pt,0);
\draw [dimen,<-,rotate=90] ([yshift=1.2cm]b1.south) -- ++(-7pt,0);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-2.0cm]b1.south);
\draw[dashed] ([xshift=-0.75cm]b1.north) --([xshift=-2.0cm]b1.north);
\draw [dimen,<->] ([yshift=1.1cm]b1.west) -- ([yshift=1.1cm]b4.east) node {$s_2 = 4$};
\draw[dashed] ([yshift=0.75cm]b1.west) --([yshift=1.25cm]b1.west);
\draw[dashed] ([yshift=0.75cm]b4.east) --([yshift=1.25cm]b4.east);
\node[draw=none] at (1.5,-0.75) {$(s_1,s_2) = (1,4)$};
\end{scope}
\begin{scope}[local bounding box=scope2,shift={($(scope1.east)+(1.5cm,-0.5cm)$)}]
\draw [draw=black, fill=gray,step=0.5] (0,0) grid (2,2) rectangle (0,0);
\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};
\node[lightGrayBoxStyle] at (1.25,0.75) (b2) {};
\node[lightGrayBoxStyle] at (0.75,1.25) (b3) {};
\node[lightGrayBoxStyle] at (1.25,1.25) (b4) {};
\draw [dimen,-] ([yshift=1.6cm]b1.west) -- ([yshift=1.6cm]b2.east) node[above=2pt]{$s_2 = 2$};
\node[draw=none] at (1.00,-0.75) {$(s_1,s_2) = (2,2)$};
\draw [dimen,<-] ([yshift=1.6cm]b1.west) -- ++(-7pt,0);
\draw [dimen,<-] ([yshift=1.6cm]b2.east) -- ++(+7pt,0);
\draw[dashed] ([yshift=1.25cm]b1.west) --([yshift=1.8cm]b1.west);
\draw[dashed] ([yshift=1.25cm]b2.east) --([yshift=1.8cm]b2.east);
\draw[dashed] ([xshift=-0.75cm]b3.north) --([xshift=-1.5cm]b3.north);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-1.5cm]b1.south);
\draw [dimen,-] ([xshift=-1.2cm]b1.south) -- ([xshift=-1.2cm]b3.north) node[rotate=90,yshift=0.40cm] {$s_1 = 2$};
\draw [dimen,<-,rotate=-90] ([yshift=-1.2cm]b3.north) -- ++(-7pt,0);
\draw [dimen,<-,rotate=90] ([yshift=1.2cm]b1.south) -- ++(-7pt,0);
\end{scope}
\begin{scope}[local bounding box=scope3,shift={($(scope2.east)+(1.0cm,-0.75cm)$)}]
\draw [draw=black, fill=gray,step=0.5] (0,0) grid (1.5,3) rectangle (0,0);
\node[lightGrayBoxStyle] at (0.75,0.75) (b1) {};
\node[lightGrayBoxStyle] at (0.75,1.25) (b2) {};
\node[lightGrayBoxStyle] at (0.75,1.75) (b3){};
\node[lightGrayBoxStyle] at (0.75,2.25) (b4) {};
\node[yshift=1.15cm,rotate=90] at (b4.north) {$s_2=1$};
\draw[dashed] ([yshift=0.75cm]b4.west) --([yshift=1.85cm]b4.west);
\draw[dashed] ([yshift=0.75cm]b4.east) --([yshift=1.85cm]b4.east);
\draw [dimen,<->] ([xshift=-1.2cm]b1.south) -- ([xshift=-1.2cm]b4.north) node[rotate=90] {$s_1 = 4$};
\draw[dashed] ([xshift=-0.75cm]b4.north) --([xshift=-1.3cm]b4.north);
\draw[dashed] ([xshift=-0.75cm]b1.south) --([xshift=-1.3cm]b1.south);
\draw [dimen,<-] ([yshift=1.2cm]b4.west) -- ++(-7pt,0);
\draw [dimen,<-] ([yshift=1.2cm]b4.east) -- ++(+7pt,0);
\node[draw=none] at (0.7,-0.75) {$(s_1,s_2) = (4,1)$};
\end{scope}
\end{tikzpicture}
\end{document}
现在的问题是,我用红色圈出的网格线和尺寸线的交界处似乎没有对齐。我该如何确保这些线对齐?
如果有更好的方法来绘制这个图形,我会很高兴知道。我还想补充一个问题。是否可以将每个矩形底部的所有文本对齐?
答案1
线条对齐的问题是由于线条宽度造成的。我建议采用不同的方法来对图形进行编码,在我看来,这种方法可以大大简化“网格”的输入,也使添加所需尺寸变得更加简单。
我的解决方案是使用matrix of nodes
来绘制网格。首先,我们为每种方块定义一些样式:
\tikzset{
X/.style = {minimum width=.5cm, minimum height=.5cm, draw, fill=black!60},
!/.style = {X, fill=black!20},
}
接下来,“网格”由以下矩阵指定(\usetikzlibrary{matrix}
是必需的):
\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};
\end{tikzpicture}
得出的结果为:
此外,它还定义(M)
为矩阵的名称,这会导致该矩阵中的每个单元自动以该方案命名(M-row-column)
。
要绘制第一条垂直虚线,首先可以考虑使用:
\draw[dashed] (M-1-1.north east) -- +(0,0.5);
(注意使用带+
符号的相对坐标)。但是,这会导致与原始图形相同的问题,即:线宽会导致一点偏移:
为了与线宽无关,我们应该使用位于第一个和第二个单元格中间的点作为起点。您可以使用插值坐标系来指定它(需要\usetikzlibrary{calc}
),如下所示:
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5);
这并没有不想要的偏移:
另外,我们可以在该虚线的末端附近命名一个辅助坐标,以便于后面绘制尺寸线:
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
(a)
这将绘制一条长度为 0.5 倍的垂直线,并在线起点 0.4 倍长度处定义一个坐标。这将是网格和尺寸线之间的垂直距离。
利用这些思想,第一个图的完整代码如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\tikzset{
X/.style = {minimum width=.5cm, minimum height=.5cm, draw, fill=black!60},
!/.style = {X, fill=black!20},
dimen/.style={
<->,>=latex,thin,
every rectangle node/.style={fill=white,midway,font=\sffamily}
},
}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-5.north)!.5!(M-1-6.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen] (a) -- (b) node {$s_2=4$};
\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(a);
\draw[dashed] ($(M-2-1.west)!.5!(M-3-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[left] at (M-2-1.west) {$s_1=1$};
\end{tikzpicture}
\end{document}
其结果为:
更新
请注意,使用 时\matrix
,您可以指定矩阵出现的坐标。这样您就可以再次将矩阵用于剩余的网格,而无需示波器或 x 移位。为确保底部对齐正确,请为每个矩阵指定anchor=south
。
在第一个矩阵完成后,您甚至可以重复使用第二个矩阵的名称(M)
。这简化了从第一个矩阵复制和粘贴代码的过程。例如:
\begin{tikzpicture}
%%%%% FIRST MATRIX
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] (M) {
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(1,4)$};
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-5.north)!.5!(M-1-6.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen] (a) -- (b) node {$s_2=4$};
\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(a);
\draw[dashed] ($(M-2-1.west)!.5!(M-3-1.west)$) -- +(-1.2,0) +(-.3,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[left] at (M-2-1.west) {$s_1=1$};
%%%%%%% SECOND MATRIX
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] at (4,0) (M) {
|[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(2,2)$};
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-3.north)!.5!(M-1-4.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\end{tikzpicture}
还请注意如何(M.south)
使用来定位标题。
编辑
根据评论和聊天中的对话,OP 在第二个矩阵的维度放置方面存在问题,因此我为此提供了一个完整的示例:
%%%%%%% SECOND MATRIX
\matrix[matrix of nodes, nodes in empty cells,
row sep=-0.5\pgflinewidth,
column sep=-0.5\pgflinewidth, anchor=south] at (4,0) (M) {
|[X]| & |[X]| & |[X]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[!]| & |[!]| & |[X]| \\
|[X]| & |[X]| & |[X]| & |[X]| \\
};
\node[below] at (M.south) {$(s_1,s_2)=(2,2)$};
\draw[dashed] ($(M-1-1.north)!.5!(M-1-2.north)$) -- +(0,0.5) +(0,0.4) coordinate(a);
\draw[dashed] ($(M-1-3.north)!.5!(M-1-4.north)$) -- +(0,0.5) +(0,0.4) coordinate(b);
\draw[dimen, <-] (a) -- +(-.4,0);
\draw[dimen, <-] (b) -- +(.4,0);
\node[above] at ($(a)!.5!(b)$) {$s_2=2$};
\draw[dashed] ($(M-1-1.west)!.5!(M-2-1.west)$) -- +(-.5,0) +(-.4,0) coordinate(a);
\draw[dashed] ($(M-3-1.west)!.5!(M-4-1.west)$) -- +(-.5,0) +(-.4,0) coordinate(b);
\draw[dimen, <-] (a) -- +(0,.4);
\draw[dimen, <-] (b) -- +(0,-.4);
\node[above,rotate=90] at ($(a)!.5!(b)$) {$s_1=2$};