答案1
没有 foreach,但有网格。绘制中断的棋盘就像说
\draw[step=5mm,line cap=rect] (0,0) grid (3.6,-3.6) (4.4,0) grid (5,-3.6)
(0,-5) grid (3.6,-4.4) (5,-5) grid (4.4,-4.4);
这利用了网格线“在网格上”的特性。虚线/椭圆非常简单,如下所示
\draw[Dotted,thick,step=1.5cm] (3.75,-3.75) -- (4.25,-4.25)
[yshift=-2.5mm] (3.75,0) grid[xstep=0] (4.25,-4.75)
[yshift=2.5mm,xshift=2.5mm] (0,-3.75) grid[ystep=0] (4.75,-4.25);
以下代码的很大一部分是完全可定制的圆形虚线样式的定义并确认其来源。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
dash pattern=on 0.1pt off 2mm,line cap=round,line width = 2pt,
shorten <=2.5mm,shorten >=2.5mm}]
\draw[step=5mm,line cap=rect] (0,0) grid (3.6,-3.6) (4.4,0) grid (5,-3.6)
(0,-5) grid (3.6,-4.4) (5,-5) grid (4.4,-4.4);
\draw[Dotted,thick,step=1.5cm] (3.75,-3.75) -- (4.25,-4.25)
[yshift=-2.5mm] (3.75,0) grid[xstep=0] (4.25,-4.75)
[yshift=2.5mm,xshift=2.5mm] (0,-3.75) grid[ystep=0] (4.75,-4.25);
\end{tikzpicture}
\end{document}
答案2
一种简单的使用方法\foreach
。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,5,7,8}
\draw (\x,0)--(\x,8) (0,8-\x)--(8,8-\x);
\draw[line width=1.7cm, white] (.5,2)--(7.5,2) (6,.5)--(6,7.5);
\foreach \x in {.5,1.5,...,4.5,7.5}
\node at (\x,2) {$\vdots$} node at (6,8-\x) {$\dots$};
\node at (6,2) {$\ddots$};
\end{tikzpicture}
\end{document}
附言:
正如@Schrödinger'scat 所建议的,从外盒中移除零件可以使其看起来更美观。在这种情况下,代码也可以变得更简单:
\begin{tikzpicture}
\draw (0,0) grid (8,8);
\draw[line width=1.7cm, white, line cap=rect] (0,2)--(8,2) (6,0)--(6,8);
\foreach \x in {.5,1.5,...,4.5,7.5}
\node at (\x,2) {$\vdots$} node at (6,8-\x) {$\dots$};
\node at (6,2) {$\ddots$};
\end{tikzpicture}
答案3
使用循环。请注意,我旋转\cdots
节点而不是使用\vdots
或\ddots
。要了解原因,只需在这些位置使用\vdots
和即可。\ddots
\documentclass[tikz,margin=4]{standalone}
\begin{document}
\begin{tikzpicture}
\def\cellsize{7mm}
\foreach \i in {1,...,5} {
\draw (\i*\cellsize,0) -- (\i*\cellsize,-5.2*\cellsize)
(\i*\cellsize,-6.8*\cellsize) -- (\i*\cellsize,-8*\cellsize)
(0,-\i*\cellsize) -- (5.2*\cellsize,-\i*\cellsize)
(6.8*\cellsize,-\i*\cellsize) -- (8*\cellsize,-\i*\cellsize)
(\i*\cellsize-\cellsize/2,-6*\cellsize) node[rotate=90] {$\cdots$}
(6*\cellsize,-\i*\cellsize+\cellsize/2) node {$\cdots$};
}
\draw (0,0) rectangle (8*\cellsize,-8*\cellsize)
(7*\cellsize,-8*\cellsize) -- ++ (0,1.2*\cellsize)
(8*\cellsize,-7*\cellsize) -- ++ (-1.2*\cellsize,0)
(7*\cellsize,0) -- ++ (0,-5.2*\cellsize)
(0,-7*\cellsize) -- ++ (5.2*\cellsize,0)
(6*\cellsize,-6*\cellsize) node[rotate=-45] {$\cdots$}
(6*\cellsize,-7.5*\cellsize) node {$\cdots$}
(7.5*\cellsize,-6*\cellsize) node[rotate=90] {$\cdots$};
\end{tikzpicture}
\end{document}