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

Zarko
  • 296,517
  • 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 '19 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]

    – nino_user183677 Apr 11 '19 at 06: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.

    – nino_user183677 Apr 11 '19 at 06:12

2 Answers2

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

  • 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. – nino_user183677 Apr 11 '19 at 06:11
  • @user183677 I added a version that uses your numbers from the \initbao command from your above comment to the scheme. –  Apr 11 '19 at 07:48
  • thanky very much. But I would like to transform the group \begin{tikzpicture}...\end{tikzpicture} in a command with the numbers as parameters. – nino_user183677 Apr 11 '19 at 08:25
  • @user183677 Added. –  Apr 11 '19 at 08: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 – nino_user183677 Apr 11 '19 at 09:22
  • @user183677 I was using pstricks for many years but switched to TikZ roughly two years ago. In the beginning it is a bit unusual but now I cannot switch back. (If the answer solved your problem, could you perhaps consider accepting it by clicking the check mark left of it?) –  Apr 11 '19 at 09:24
  • I realized that your code doesn't display the number in the correct order, i.e. it shows the rows from the last one, while they should be displayed in the exact order indicated in the command \initbao! I've tried to correct the code but I failed to understand \pgfmathparse – nino_user183677 Apr 11 '19 at 17:24
  • @nino_user183677 \pgfmathparse just allows us to extract elements of an array, so \pgfmathparse{<array>[<index>]} makes \pgfmathresult become the element of the array at index position <index>. (You could also use \pgfmathsetmacro{\myelement}{<array>[<index>]} to assign this value to \myelement.) Anyway, I changed the ordering in the command version. –  Apr 11 '19 at 17:30
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

Kpym
  • 23,002
  • Could you perhaps consider stopping picking on me? –  Apr 11 '19 at 07:10
  • Could you be more precise what is your "original" idea that I have stolen from you ? – Kpym Apr 11 '19 at 07:12
  • I did not say you stole anything. –  Apr 11 '19 at 07:14
  • What are you talking about in this case ? – Kpym Apr 11 '19 at 07:15
  • Your behavior to, whenever you see a chance, to pick on me. –  Apr 11 '19 at 07:15
  • Sorry I don't get it (really). Could you be more precise in this particular case what I've done wrong ? – Kpym Apr 11 '19 at 07:17
  • Well, when the question changes, as it did because the OP asked for something additional/different or new in the comments, rather than giving the one who got the comment a chance to address it, immediately write an answer that addresses this. That's called picking. –  Apr 11 '19 at 07:20
  • When I see a question and a way to answer it differently from existing answers, if I have time, I add a new answer. It's as simple as that. I don't care if the question is changed or not. We are not competing here, we are here just to help people. – Kpym Apr 11 '19 at 07:29
  • Although you like \tikzstyle you should note that it is deprecated and should not be used for several reasons… – TeXnician Apr 11 '19 at 08:18
  • @TeXnician I do not agree with you and with lot of other users. I know very well this question but I don't understand how we conclude from this that we should not use it. I have never seen a single problem due to \tikzstyle. – Kpym Apr 11 '19 at 09:06