我正在使用下面的代码来生成网格,但它并没有连接网格中的所有点。
有人能帮忙吗?谢谢!
\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9}]
\foreach \x in {1, 2, 3}
\foreach \y in {1, 2, 3}
{\pgfmathtruncatemacro {\label}{\x \y}
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};}
\foreach \x in {1,2,3}
\foreach \y [count=\yi] in {1,2}
\draw (\x\y)--(\x\yi) (\y\x) --(\yi\x) ;
\end{tikzpicture}
答案1
毫不奇怪,可以用...绘制网格grid
(并且不需要这样做\pgfmathtruncatemacro {\label}{\x \y}
)。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9,fill=white}]
\draw (1.5,1.5) grid[step=1.5] (4.5,4.5);
\foreach \x in {1, 2, 3}
\foreach \y in {1, 2, 3}
{
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {$U_{\x \y}$};}
\end{tikzpicture}
\end{document}
答案2
问题是你从节点到自身画了一条线,所以它根本没有画任何东西:
\draw (\x\y)--(\x\yi)
实际上,您的循环会生成一个[count=\yi] in {1,2}
从 开始的计数器1
,因此在第一次使用 和 的迭代过程中\x=1
,\y =1
您会生成这个\x=1
\yi=1
,等等。
假设您想要与@DũngVũ相同的结果,这里是另一种方法:
\documentclass[crop,tikz,border=5mm]{standalone}
\begin{document}
\usetikzlibrary{positioning,calc}
\tikzstyle{block} = [draw, rectangle, minimum height=1cm, minimum width=1cm, outer sep=0pt]
\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9}]
\foreach \x in {1, 2, 3}
\foreach \y in {1, 2, 3}
{\pgfmathtruncatemacro {\label}{\x\y}
\node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};
}
\foreach \x in {1,2,3}
\foreach \y [count=\yi from 2] in {1,2}
\path[] (\x\y)edge(\x\yi)(\y\x)edge(\yi\x);
\end{tikzpicture}
\end{document}
答案3
你是这个意思吗?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1, 2, 3}
\foreach \y in {1, 2}
\draw (1.5*\x,1.5*\y) -- (1.5*\x,2.5*\y);
\foreach \x in {1, 2}
\foreach \y in {1, 2, 3}
\draw (1.5*\x,1.5*\y) -- (2.5*\x,1.5*\y);
\foreach \x in {1, 2, 3}
\foreach \y in {1, 2, 3} {
\pgfmathtruncatemacro {\label}{\x\y}
\fill[color=white] (1.5*\x,1.5*\y) circle (0.5cm);
\draw (1.5*\x,1.5*\y) circle (0.5cm);
\node (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};
}
\end{tikzpicture}
\end{document}
希望这会有所帮助!