2

I want this:

enter image description here

which is done with

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

\usetikzlibrary{matrix}

\begin{tikzpicture}[
    mygridmatrix/.style={matrix of nodes, 
       row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
       nodes={draw, minimum size=1cm}, nodes in empty cells},
]

\matrix (m) [mygridmatrix]
{
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
};

\foreach \i [count=\xi from 0] in  {1,...,6}{
    \node also [label=above:\xi] (m-1-\i) {}; 
    \node also [label=left:\xi] (m-\i-1) {}; 
}
\end{tikzpicture}
\end{document}

As you can see, \matrix draws the grid and later on \foreach adds labels to first row and column nodes.

I would like to include this second part into mygridmatrix definition. I know I could do it with

\matrix (m) [mygridmatrix]
{
|[label=left:0,label=above:0]|&|[label=above:1]| ....\\
|[label=left:1]|& ...\\
...

but I would prefer to declare some row 1/.style and column 1/.style which automatically declare add labels related with \pgfmatrixcurrentrow and \pgfmatrixcurrentcolumn (which are only known when TiKZ is building each node).

May be using some late code or execute at end cell but I couldn't find it.

Is it possible?

  • not tried ... there's a \hypertarget command provided by hyperref that allows a \label to be put in a location otherwise unidentified in the "usual" manner. i don't know whether it would "survive" in such a localized environment, but it's what i would try if i needed it. – barbara beeton Feb 6 '15 at 16:38
  • @barbarabeeton Thank you, but I need a node's label (in TikZ sense) and not a \label (in hyperref sense). I've changed title to make it clearer. – Ignasi Feb 6 '15 at 16:49
  • thanks for the clarification. (sometimes we are misled by a too-limited vocabulary, where the same word means more than one thing.) – barbara beeton Feb 6 '15 at 17:02
  • Is it about the drawing the grid in particular or you have other things in mind? – percusse Feb 6 '15 at 17:23
  • @percusse It's about drawing this kind of grids in particular. I was trying to simplify my answer to tikz-matrix-of-objects. If it's possible to add some contents to a node, why not a label? But column and row values are not remembered once used. – Ignasi Feb 6 '15 at 18:10
2

With some help from Zarko and some from percusse I've managed to define several .styles which can be used to add labels to nodes on top row and left column on a matrix more or less automatically.

The style proposed by Zarko can be easily applied to a row or column, but it doesn't work for corner cells where a left and top label have to be applied. As a workaround solution I've defined some .list styles which are applied to all except first elements of top row or left column in a matrix.

Another consideration has been necessary to distinguish between empty and non empty cells.

I'm still not very satisfied with the code, so if you can improve, just let me know or add an answer with better solutions.

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

\begin{document}
\begin{tikzpicture} [
    toplabel/.style={
        row 1 column #1/.style={%
            execute at begin cell={%
                |[label=\the\pgfmatrixcurrentcolumn]|},
            execute at empty cell={%
                \node[label=\the\pgfmatrixcurrentcolumn]{};}
        }
    },
    leftlabel/.style={
        row #1 column 1/.style={%
            execute at begin cell={%
                |[label=left:\the\pgfmatrixcurrentrow]|},
            execute at empty cell={%
                \node[label=left:\the\pgfmatrixcurrentrow]{};}
        }
    },
    topleft/.style={
        row 1 column 1/.style={%
            execute at begin cell={%
                |[label=\the\pgfmatrixcurrentcolumn,
                    label=left:\the\pgfmatrixcurrentrow]|},
            execute at empty cell={%
                \node[label=left:\the\pgfmatrixcurrentrow,
                    label=\the\pgfmatrixcurrentcolumn]{};}
        }
    }
]
\matrix (A) [
    matrix of nodes, 
    nodes in empty cells,
    nodes={draw, minimum size=1cm, anchor=center},
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    topleft,
    toplabel/.list={2,...,6},
    leftlabel/.list={2,...,6}
]
{A&A& &A&A& \\  
  &A&A&A&A&A\\
 A&A& & &A&A\\
  &A&A&A& &A\\
 A&A&A&A&A&A\\
 A& & & & & \\};

\end{tikzpicture}
\end{document}

enter image description here

  • (+1). good trick with lists, but i need more time to figured out their use. – Zarko Aug 17 '17 at 14:31
1

If you accept to not use the first row and column, you can use the styles row 1, column 1 and row 1 column 1 to do the following :

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

\begin{document}
\begin{tikzpicture}[
  mygridmatrix/.style={matrix of nodes,
    row sep=-\pgflinewidth, column sep=-\pgflinewidth,
    nodes={draw, minimum size=1cm,anchor=center},
    nodes in empty cells},
  row 1/.append style={nodes={draw=none,node contents=\pgfmathparse{int(\pgfmatrixcurrentcolumn-2)}\pgfmathresult}},
  column 1/.append style={nodes={draw=none,node contents=\pgfmathparse{int(\pgfmatrixcurrentrow-2)}\pgfmathresult}},
  row 1 column 1/.append style={nodes={node contents=}},
]

\matrix (m) [mygridmatrix]
{
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
&&&&&\\
};

\end{tikzpicture}
\end{document}

enter image description here

  • Nice trick. +1. – Ignasi Feb 28 '15 at 7:44
1

I understand that the original question was about a particular issue with styles in TikZ and on how to deal with a matrix grid and numbers outside of it. But as soon as I view your main issue with the matrix grid and numbers of rows and columns, it brings to mind a package that is used, however, for more playful purposes: LogicPuzzle package.

It is easy to use the skyline puzzle for making rectangular grids with empty cells and/or with numbers/letters in them, and the rows and columns can be numbered in each side of the rectangle, with options:

\skylineL{...} % For left
\skylineT{...} % For top
\skylineR{...} % For right
\skylineB{...} % For bottom

Also, there are another features like: coloring of cells, highlighting the contour of some cells, among others. (See my answer here as an example.) So if you consider this answer does not meet your spectations or helps you in some way, take it as a just for fun answer.

Output

enter image description here

The code

\documentclass[border=4mm]{standalone}    
\usepackage{logicpuzzle}

\begin{document}

\begin{skyline}[rows=6, columns=6]
    \skylineL{6,5,4,3,2,1}
    \skylineT{1,2,3,4,5,6}
    \setrow{6}{A,A,{},A,A,{}}
    \setrow{5}{{},A,A,A,A,A}
    \setrow{4}{A,A,{},{},A,A}
    \setrow{3}{{},A,A,A,{},A}
    \setrow{2}{A,A,A,A,A,A}
    \setrow{1}{A,{},{},{},{},{}}
\end{skyline}

\end{document}

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.