1

I made a board with coordinates. But I want it at the beginning (0,0) instead of (3,0) with this code below:

\begin{tikzpicture}[scale=1.25]
\foreach \y in {0,1,...,3}
\foreach \x in {0,1,...,3}
{
%\draw[draw = gray] (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw[draw = gray] (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\small (\y,\x)};
}
\foreach \x in {0,1,...,3}{
\node[draw=red!70] at (\x,-1) {\x};
\node[draw=blue!80] at (-1,\x) {\x};
}
%\draw [thick,red!50!black, -Stealth] (-3,0) --  node[above=1mm] {row} (-1.5,0);
%\draw [thick,blue!50!black, -Stealth] (0,-3) -- node[below=10mm] {column} (0,-1.5);
\end{tikzpicture}

i get: [3,0] [3,1] [3,2] [3,3] but i want: [0,0] [0,1] [0,2] [0,3] and the rows and columns should also be below the grid. How can I solve this?

3 Answers 3

2

An alternative approach could be to use nodes instead of drawing rectangles and placing nodes. Since you use the scale option, you need to grab the scaled x/y units in order to be able to use them inside the node options though. In any case, you only need two \foreach loops:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1.25]
\path (1,1);
\pgfgetlastxy{\unitx}{\unity}

\foreach \x in {0,...,3}{
    \node[draw=red!70] at (\x,1) {\x};
    \foreach \y in {0,...,3}{
        \node[draw=blue!80] at (-1,-\y) {\y};
        \node[draw = gray, minimum width=\unitx, minimum height=\unity] at (\x,-\y) {\small (\x,\y)};
    }
}

\end{tikzpicture}

\end{document}

enter image description here

2

A matrix is also a valid solution:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
    nodes={anchor=center, draw, minimum size=1cm},
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    row 1/.style={nodes={draw=red, minimum size=3mm}},
    column 1/.style={nodes={draw=blue, minimum size=3mm}}]{
    &[3mm] 0 & 1 & 2 & 3\\[3mm]
    0 & (0,0) & (0,1) & (0,2) & (0,3)\\
    1 & (1,0) & (1,1) & (1,2) & (1,3)\\
    2 & (2,0) & (2,1) & (2,2) & (2,3)\\
    3 & (3,0) & (3,1) & (3,2) & (3,3)\\};
\end{tikzpicture}
\end{document}

enter image description here

0

I found the solution here you go:

\begin{tikzpicture}[scale=1.25]
\foreach \x in {0,1,...,3}
    \foreach \y in {0,1,...,3}
    {
    \draw[draw = gray] (\y,-\x) +(-.5,-.5) rectangle ++(.5,.5);
    \draw (\y,-\x) node{\small (\x,\y)};
    }

\foreach \x in {0,1,2,...,3}{
\node[draw=red!70] at (\x,1) {\x};
\node[draw=blue!80] at (-1,-\x) {\x};
}
%\draw [thick,red!50!black, -Stealth] (-3,0) --  node[above=1mm] {row} (-1.5,0);
%\draw [thick,blue!50!black, -Stealth] (0,-3) -- node[below=10mm] {column} (0,-1.5);
\end{tikzpicture}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .