4

I am trying to draw gridlines inside a square. Here is my code:

\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}

This code, however, will not compile. My goal is to make a square with a 24 x 24 grid.

  • No grid comes up when I use this. It only produces the square – Aiden Kenny Dec 16 '18 at 9:07
6

Works fine if you use \pgfplotsinvokeforeach instead of \foreach:

enter image description here

Notes:

  • I would suggest you try the grid options built into pgfplots and tikz.
  • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses \foreach.

References:

Code: 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}

Code: no 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}
  • Just curious, for using \pgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of \test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening? – Aiden Kenny Dec 23 '18 at 17:09
  • @AidenKenny: I am guessng that you are using \pgfplotsinvokeforeach within a macro where #1 refers to the first parameter of the macro. Thus, if you are using \pgfplotsinvokeforeach within a macro definition, you need to use ##1 to refer to the parameters of the \pgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where #1 would refer to the first parameter of the outer macro and ##1 would refe to the first parameter of the inner macro. – Peter Grill Dec 23 '18 at 17:58
  • @AidenKenny: If that does not solve your issue, please ask a new question with a MWE including \documentclass that reproduces the problem. – Peter Grill Dec 23 '18 at 18:00
6

Two other options without pgfplots, one without foreach and one with 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}

enter image description here

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.