Q:

Wrong rectangle drawing

I probably don't understand rectangle drawing using TikZ. The code bellow shows clear drawing, but even if those \g and \w are same, output file is wrong.
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\def \a {3}
\def \w {0.5}
\def \g {0.5}
\def \s14 {0.5}
\def \s34 {0.5}

\filldraw [blue] (0,0) rectangle (\a, \a);
\filldraw [white] (\w,\w) rectangle (\a - \w,\a - \w);
\filldraw [yellow] (0,\a/2 + \g/2) rectangle (\w,\g);
\end{tikzpicture}

\end{document}

I made some notes on output drawing. The yellow area is unwanted, position and size of the g x w rectangle is clearly shown. Can you help?

A:

Do you want this? You can use + to tell TikZ that the second coordinate is
relative to the first.
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\newcommand* \mylengthA {3}
\newcommand* \mylengthW {0.5}
\newcommand* \mylengthG {0.5}

\fill [blue] (0,0) rectangle (\mylengthA, \mylengthA);
\fill [white]
  (\mylengthW,\mylengthW) rectangle
  (\mylengthA - \mylengthW,\mylengthA - \mylengthW);
\fill [yellow]
  (0,\mylengthA/2 + \mylengthG/2) rectangle +(\mylengthW,-\mylengthG);
\end{tikzpicture}

\end{document}

rectangle draws a rectangle between the two coordinates given. Without + they are considered to be absolute and not relative to each other. So \draw (1,1) rectangle (2,2); would draw a rectangle between those two points with edges of 1cm length.

