3

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 4x8 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

3
  • 2
    Welcome to TeX.SX! You should use rectangle ++(0.4cm, 0.4cm) (or even better, shift it 0.2cm to the left and bottom beforehand). Which kind of number parameter do you want to use?
    – TeXnician
    Apr 10, 2019 at 20:05
  • Thanks. I would like to have a command like this \newcommand{\initbao}[1][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] Apr 11, 2019 at 6:01
  • @TeXnician Thanks. I would like to have a command like this \newcommand{\initbao}[1][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] The command should put the passed number in the circles. Apr 11, 2019 at 6:12

2 Answers 2

7

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

8
  • thank you very much. Can you help me to create a new command to put numbers into the circles/rettangles. The numbers will be different depending on the document which uses the new command. Apr 11, 2019 at 6:11
  • @user183677 I added a version that uses your numbers from the \initbao command from your above comment to the scheme.
    – user121799
    Apr 11, 2019 at 7:48
  • thanky very much. But I would like to transform the group \begin{tikzpicture}...\end{tikzpicture} in a command with the numbers as parameters. Apr 11, 2019 at 8:25
  • @user183677 Added.
    – user121799
    Apr 11, 2019 at 8:32
  • Thank you very much. I wrote a sty file which uses PSTrick but since 2014 it doesn't work. Now thanks to you I can write another sty file using TikZ, which I have to understand well! Nino Apr 11, 2019 at 9:22
2

In place to use a loop and conditionals you can use matrix of nodes where all the nodes are circles and style some of them as rectangles.

\documentclass[tikz,border=7mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}
    \tikzstyle{r}=[rectangle] % <-- because I like tikzstyle ;)
    \matrix[matrix of nodes, nodes in empty cells,
      column sep={1cm,between origins},row sep={1cm,between origins},
      nodes={circle,anchor=center,draw,minimum size=8mm}](M){
      & & &             &       & & &   \\
      & & &             & |[r]| & & &   \\
      & & & |[r,red]| ? &       & & &   \\
      & & &             &       & & & ! \\
    };
  \end{tikzpicture}
\end{document}

enter image description here

10
  • Could you perhaps consider stopping picking on me?
    – user121799
    Apr 11, 2019 at 7:10
  • Could you be more precise what is your "original" idea that I have stolen from you ?
    – Kpym
    Apr 11, 2019 at 7:12
  • I did not say you stole anything.
    – user121799
    Apr 11, 2019 at 7:14
  • What are you talking about in this case ?
    – Kpym
    Apr 11, 2019 at 7:15
  • Your behavior to, whenever you see a chance, to pick on me.
    – user121799
    Apr 11, 2019 at 7:15

You must log in to answer this question.

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