5

I have a serious problem right now understanding what the parameters of the draw command mean.

For a document about neural networks I want to make an example of a system detecting different simple letters and I wanted to draw those letters in a grid, where every pixel is visible.

Therefore, I wrote this:

\documentclass{article}
\usepackage{tikz}
\usepackage[ngerman]{babel}
\begin{document}
\begin{figure}
    \begin{tikzpicture}
            \draw[step=1,black] (0, 0) grid (3, 3);
        \foreach \x in {1,2,3}
                \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
        \foreach \y in {1,2,3}
                \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};
\end{tikzpicture}
\label{figt}
\caption{\glqq T\grqq}

\end{figure} \end{document}

which gives a perfectly fine grid. Exactly what I was looking for.

Now I wanted the fields [1, 0], [1, 1], [0, 2], [1, 2], [2, 2] to be filled out, so that a "T" is visible.

I thought it would be as simple as:

\draw[fill=black, opacity=.5] (1, 0) rectangle (1, 1);

thinking, that (1, 0) is the position (bottom, middle) and (1, 1) is the width of the rectangle.

But this ended up with an empty grid again. So, I thought, maybe tikz starts with 1, not zero. So I wrote:

\draw[fill=black, opacity=.5] (2, 1) rectangle (1, 1);

with the very same result: an empty grid. Then I changed the "1" in (2, 1) to a zero

\draw[fill=black, opacity=.5] (2, 0) rectangle (1, 1);

and it finally worked: I had one "pixel" filled. And I since I already tried (2, 1) and it did nothing, I tried (2, 2), which filled the center pixel. But why? What's with (2, 1)? I don't get it. Does it start with 0 at the y-axis and with 1 at the x-axis?

So, now there is only the "hat" of "T" left. I thought,

\draw[fill=black, opacity=.5] (2, 3) rectangle (1, 1);

might help me. But (with transparency enabled) I see, that this "re-fills" (2, 2). Why?

Trying to fill (0, 3) (the upper left I assume) I wrote:

\draw[fill=black, opacity=.5] (0, 3) rectangle (1, 1);

But this filled both cells, again, (0, 3) and (0, 2), so the "T" looks like this:

what the _t_uck
(source: saved.im)

(with this code:

    \begin{tikzpicture}
            \draw[step=1,black] (0, 0) grid (3, 3);
        \foreach \x in {1,2,3}
                \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
        \foreach \y in {1,2,3}
                \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

        \draw[fill=black, opacity=.5] (2, 0) rectangle (1, 1);
        \draw[fill=black, opacity=.5] (2, 3) rectangle (1, 1);

        \draw[fill=black, opacity=.5] (0, 3) rectangle (1, 1);
\end{tikzpicture}

)

I am very sure that this is not a fault in tikz or whatever, but just my ignorance of the internals of tikz and of how to use it. But right now, after about 1 hour of playing around creating this character (and still haven't succeeded with it), I kind of give up and am clueless. Is my interpretation of those characters so wrong? Or what mistake did I make over and over again?

  • 1
    The arguments of rectangle are the coordinates of diagonally opposite corners. Thus (1,0) rectangle (2,1) would give you the 1-by-1 square you are looking for. If you want the second pair to represent increments, you need the extra syntax of Mark Wibrow's answer. – Dan Aug 13 '14 at 19:55

1 Answers1

8

You need to use +(1,1) or ++(1,1) (there is a difference between the two which is irrelevant in this case) to get the corner of the rectangle 1 unit up and and 1 unit right from the last point:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\tikz[%
  every cell/.style={draw=black},
  every cell 1/.style={fill=gray}
]
   \foreach \row [count=\y] in {%
     {1,1,1,1,1,1,1},%
     {1,0,0,1,0,0,1},%
     {0,0,0,1,0,0,0},%
     {0,0,0,1,0,0,0},%
     {0,0,0,1,0,0,0},%
     {0,0,0,1,0,0,0},%
     {0,0,1,1,1,0,0}}
     \foreach \cell [count=\x] in \row  
        \path [every cell/.try, every cell \cell/.try]
           (\x,-\y) rectangle ++(1,1);
\end{document}

enter image description here

Mark Wibrow
  • 70,437
  • Thanks, but I still do not understand this and after another hour of trying, I'm still too stupid to get it. Could you make the example I need, which would be 1 1 1 0 1 0 0 1 0 so that I may see how it would work?

    Thanks in advance.

    – clueless_me Aug 13 '14 at 21:12
  • Also, could you please explain what the difference is? I'd really love to understand what I am doing here. – clueless_me Aug 13 '14 at 21:19
  • I am sorry, I solved it myself with this clue now. Thanks! – clueless_me Aug 13 '14 at 21:30