[Tex/LaTex] \draw rectangle TikZ

drawtikz-pgf

I'm trying to lean TikZ.

I've written the following small document

\documentclass[tikz,border=12pt]{standalone}
\usepackage{ifthen}
\usepackage{pgf, tikz}
\usetikzlibrary{graphs,calc}

\makeatletter
\newcommand*\circled[1]{\smash{\tikz[baseline=(char.base)]{
            \node[anchor=text, shape=circle,fill,draw,inner sep=0pt,text=white,minimum size=1.4em] (char) {#1\strut};}}}
\makeatother

\begin{document}

    \begin{tikzpicture}
%    \draw(4,-1) rectangle(8,4);
    \foreach \y in {1, ..., 4}
    \foreach \x in {1, ..., 8} {
            \ifthenelse{\equal{\x}{5} \AND \equal{\y}{3}}{\draw(\x,\y)rectangle(0.4cm,0.4cm)}{
            \ifthenelse{\equal{\x}{4} \AND \equal{\y}{2}}{
                \draw[thick] (\x,\y) rectangle (0.4cm,0.4cm);
                \draw(\x,\y)rectangle(0.4cm,0.4cm)}{\draw(\x,\y)circle(0.4cm)}};
    }

    \end{tikzpicture}

\end{document}

They output should be a set of 4×8 circles, but the 4th circle of the second row and then 5th circle of the third row should be replaced by rettangles.

I don't understand why they are as in the picture.

Finally, how can define a command which writes numbers in the circles according to the parameters passed by another document. I taught that I could use the newcommand \circled, but I don't understand how.

Please help me!

enter image description here

Best Answer

I recommend using pics for that and not to use ifthenelse (for that).

\documentclass[tikz,border=12pt]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[pics/.cd,
circ/.style={code={\draw (0,0) circle[radius=#1];}},
circ/.default=4mm,
rect/.style={code={\draw (-#1,-#1) rectangle (#1,#1);}},
rect/.default=4mm]
%    \draw(4,-1) rectangle(8,4);
\path    foreach \Y in {1,...,4}
    {foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
     (\X,\Y)
     \ifnum\Z=35    
       pic{rect}
    \else
      \ifnum\Z=24
         pic{rect}
      \else
         pic{circ}
      \fi
    \fi }};
\end{tikzpicture}
\end{document}

enter image description here

This is what you could do if you want to add the numbers from your list \initbao to the scheme.

\documentclass[tikz,border=12pt]{standalone}

\begin{document}


\begin{tikzpicture}[circ/.style={circle,draw,minimum size=8mm},
rect/.style={rectangle,draw,minimum size=8mm}]
\def\myarray{{22,0,0,0,0,0,0,0,0, 0,2,2,6,0,0,0,0,
   0,0,0,0,6,2,2,0, 0,0,0,0,0,0,0,0,22}}
\path    foreach \Y in {1,...,4}
    {foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
     (\X,\Y)
     \ifnum\Z=35    
       node[rect] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
    \else
      \ifnum\Z=24
          node[rect] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
      \else
          node[circ] {\pgfmathparse{\myarray[(\Y-1)*8+\X-1]}\pgfmathresult}
      \fi
    \fi 
    }};
\end{tikzpicture}
\end{document}

enter image description here

Or a command version thereof (with the ordering changed).

\documentclass[tikz,border=12pt]{standalone}

\newcommand{\initbao}[1]{\begin{tikzpicture}[circ/.style={circle,draw,minimum size=8mm},
rect/.style={rectangle,draw,minimum size=8mm}]
\path    foreach \Y in {1,...,4}
    {foreach [evaluate=\X as \Z using {int(10*\Y+\X)}] \X in {1,...,8} {
     (\X,\Y)
     \ifnum\Z=35    
       node[rect] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
    \else
      \ifnum\Z=24
          node[rect] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
      \else
          node[circ] {\pgfmathparse{{#1}[(4-\Y)*8+\X-1]}\pgfmathresult}
      \fi
    \fi 
    }};
\end{tikzpicture}}
\begin{document}

\initbao{22,0,0,0,0,0,0,0,0, 0,2,2,6,0,0,0,0,
   0,0,0,0,6,2,2,0, 0,0,0,0,0,0,0,0,22}
\end{document}

enter image description here