我正在尝试在正方形内绘制网格线。这是我的代码:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[scale=1.25,line width=1pt]
\begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]
\foreach \x in {-24,..., 24}
{\draw[thin, gray] (\x,-24) -- (\x,24);}
\draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);
\node[black, below] at (24,-24) {$(1,0)$};
\node[black, below] at (-24,-24) {$(0,0)$};
\node[black, above] at (-24,24) {$(0,1)$};
\node[black, above] at (24,24) {$(1,1)$};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
但是,此代码无法编译。我的目标是制作一个 24 x 24 网格的正方形。
答案1
\pgfplotsinvokeforeach
如果您使用而不是,则可以正常工作\foreach
:
笔记:
- 我建议您尝试和
grid
内置的选项。pgfplots
tikz
- 您显示的所有代码都不需要,
pgfplots
因此您可以消除axis
环境。因此,除非有其他功能未在 MWE 中显示,否则您应该考虑axis
使用 的非环境版本\foreach
。
参考:
代码:pgfplots
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
%
%\begin{figure}[ht]
%
%\centering
%
\begin{tikzpicture}%[scale=1.25,line width=1pt]
\begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]
\pgfplotsinvokeforeach {-24,...,24} {%
\draw[thin, gray] (#1,-24) -- (#1,24);
}
\draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);
\node[black, below] at (24,-24) {$(1,0)$};
\node[black, below] at (-24,-24) {$(0,0)$};
\node[black, above] at (-24,24) {$(0,1)$};
\node[black, above] at (24,24) {$(1,1)$};
\end{axis}
\end{tikzpicture}%
%
%\end{figure}
%
\end{document}
代碼: 沒有pgfplots
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[scale=0.10]
\foreach \x in {-24,...,24} {%
\draw[thin, gray] (\x,-24) -- (\x,24);
}
\draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);
\node[black, below] at (24,-24) {$(1,0)$};
\node[black, below] at (-24,-24) {$(0,0)$};
\node[black, above] at (-24,24) {$(0,1)$};
\node[black, above] at (24,24) {$(1,1)$};
\end{tikzpicture}%
\end{document}
答案2
另外两个选项没有pgfplots
,一个没有foreach
,一个有foreach
:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\draw (0,0) grid (24,24);
\node[above right] at (24,24) {$(1,1)$};
\node[above left] at (0,24) {$(0,1)$};
\node[below left] at (0,0) {$(0,0)$};
\node[below right] at (24,0) {$(1,0)$};
\end{tikzpicture}
\begin{tikzpicture}
\foreach \i in {0,...,24}{
\draw (\i,0) -- ++(90:24);
\draw (0,\i) -- ++(0:24);
}
\node[above right] at (24,24) {$(1,1)$};
\node[above left] at (0,24) {$(0,1)$};
\node[below left] at (0,0) {$(0,0)$};
\node[below right] at (24,0) {$(1,0)$};
\end{tikzpicture}
\end{document}