2

I use the following code from the answer to this post to draw the progress of Coloraddo game, which uses 3x3 cards to be arranged in a board; displaying only a part of the board, as illustrated below.

Other cards are used in different arrangements in other locations of the document.

Is it possible to put each card in a \pic and then insert each \pic in its respective location; instead of repeating drawing the whole board!

\documentclass[12pt,a4paper]{article}
\usepackage{geometry}
\geometry{a4paper, left=10mm, right=10mm, top=8mm, bottom=15mm}
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\begin{document}
%begin defining shapes ==========================
\pgfmathsetmacro{\lwdth}{.3mm}% for line width

\tikzset{% trg/.style={% regular polygon, regular polygon sides=3, draw=black, line width=\lwdth, fill=blue, minimum size=1.4cm, rounded corners=0.1cm}, crc/.style={% circle, draw=black, line width=\lwdth, fill=green!70!black, minimum size=1.cm}, sqr/.style={% regular polygon, regular polygon sides=4, draw=black, line width=\lwdth, fill=yellow, minimum size=1.2cm}, str/.style={% star, star points=6, draw=black, line width=\lwdth, fill=red, minimum size=1.1cm}, filrd/.style={% fill=red}, filgr/.style={% fill=green}, filyw/.style={% fill=yellow}, filbl/.style={% fill=blue}, } %end defining shapes ========================== %begin defining grid ========================== \newcommand\sqw{1} \tikzset{ pics/square/.default={\sqw}, pics/square/.style = { code = { \draw[pic actions, draw=none] (0,0) rectangle (#1,#1); }}}

\renewcommand\sqw{1.3} %end defining grid ========================== An example of the placement of the first 4 cards of a game

starting board before play begins \begin{tikzpicture}[scale=0.3, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (10\sqw, 7\sqw); % \foreach \i in {3,2,1}{ \foreach \j in {1,2,3}{ \pic[filrd] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {4,5,6}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {6,5,4}{ \foreach \j in {7,8,9}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {7,8,9}{ \pic[filyw] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {6,5,4}{ \foreach \j in {4,5,6}{ \pic[filyw] at (\j\sqw,\i\sqw) {square};};}; % % \foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/4.5,2.5/6.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/6.5,3.5/5.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/4.5,3.5/6.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \vskip .6cm after player 1 places card 1 \kern 4.9mm \begin{tikzpicture}[scale=0.3, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (10\sqw, 7\sqw); % \foreach \i in {3,2,1}{ \foreach \j in {1,2,3}{ \pic[filrd] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {4,5,6}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {6,5,4}{ \foreach \j in {7,8,9}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {7,8,9}{ \pic[filyw] at (\j\sqw,\i\sqw) {square};};}; % % \foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/4.5,2.5/6.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \vskip .4cm after player 2 places card 2 \kern 4.9mm \begin{tikzpicture}[scale=0.3, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (10\sqw, 7\sqw); % \foreach \i in {3,2,1}{ \foreach \j in {1,2,3}{ \pic[filrd] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {6,5,4}{ \foreach \j in {7,8,9}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {7,8,9}{ \pic[filyw] at (\j\sqw,\i\sqw) {square};};}; % % \foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/4.5,2.5/6.5,4.5/3.5,4.5/1.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5,4.5/2.5,5.5/3.5,5.5/2.5,5.5/1.5,6.5/2.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5,6.5/3.5,6.5/1.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \vskip .4cm after player 3 places card 3 \kern 4.9mm \begin{tikzpicture}[scale=0.3, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (10\sqw, 7\sqw); % \foreach \i in {6,5,4}{ \foreach \j in {7,8,9}{ \pic[filbl] at (\j\sqw,\i\sqw) {square};};}; % \foreach \i in {3,2,1}{ \foreach \j in {7,8,9}{ \pic[filyw] at (\j\sqw,\i\sqw) {square};};}; % % \foreach\x/\y in {1.5/5.4,2.5/5.4,3.5/4.4,4.5/4.4,3.5/3.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/4.5,2.5/6.5,4.5/3.5,4.5/1.5,1.5/3.5,1.5/2.5,1.5/1.5,2.5/1.5,3.5/1.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/6.5,3.5/5.5,4.5/5.5,5.5/5.5,5.5/4.5,4.5/2.5,5.5/3.5,5.5/2.5,5.5/1.5,6.5/2.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/4.5,3.5/6.5,4.5/6.5,5.5/6.5,6.5/6.5,6.5/5.5,6.5/4.5,6.5/3.5,6.5/1.5,2.5/3.5,2.5/2.5,3.5/2.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \vskip 1.cm central square \begin{tikzpicture}[scale=0.5, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (4\sqw, 4\sqw); % \foreach\x/\y in {1.5/2.4,2.5/2.4,3.5/1.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/3.5,3.5/2.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/1.5,2.5/3.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/1.5,3.5/3.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \hskip .6cm card1 \begin{tikzpicture}[scale=0.5, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (4\sqw, 4\sqw); % \foreach\x/\y in {1.5/1.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/2.5,2.5/2.5,3.5/2.5,2.5/1.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/3.5,2.5/3.5,3.5/3.5,3.5/2.5,3.5/1.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \hskip .6cm card2 \begin{tikzpicture}[scale=0.5, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (4\sqw, 4\sqw); % \foreach\x/\y in {1.5/2.5,2.5/1.5,2.5/2.5,2.5/3.5,3.5/2.5} \node[str] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/1.5,1.5/3.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {3.5/1.5,3.5/3.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \hskip .6cm card3 \begin{tikzpicture}[scale=0.5, transform shape] \draw[step=\sqw] (\sqw,\sqw) grid (4\sqw, 4\sqw); % \foreach\x/\y in {3.5/3.4} \node[trg] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {1.5/1.5,1.5/2.5,1.5/3.5,2.5/1.5,3.5/1.5} \node[sqr] at (\x\sqw,\y\sqw) {}; % \foreach\x/\y in {2.5/2.5,2.5/3.5,3.5/2.5} \node[crc] at (\x\sqw,\y\sqw) {}; \end{tikzpicture} \end{document}

enter image description here

enter image description here

Hany
  • 4,709
  • "Is it possible to put each card in a \pic and then insert each \pic in its respective location; instead of repeating drawing the whole board!" >>> Yes! that is exactly the purpose when introducing the concept of pic, IMO. – Black Mild Sep 24 '22 at 22:58
  • 1
    @– Black Mild Would you please tell me how to do that for one card as an example. I have a limited programming capacity; and I do not know how to do that. Thank you. – Hany Sep 25 '22 at 04:44

1 Answers1

3

For free drawing, intentionally I don't use neither the library shapes.geometric, nor the library matrix.

Here every card is just a pic.

We can scale whole figure with [scale=.5,transform shape]. The option rounded corner should not be used (it doesn't work with scale that is often used in this situation I suppose, check pgfmanual.pdf).

enter image description here

enter image description here

enter image description here

enter image description here

\documentclass[12pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tikz}
\begin{document}
\tikzset{%
triangle/.pic={\draw[fill=blue,shift={(0,-.1)},scale=.45] (90:1)--(210:1)--(-30:1)--cycle;},
circle/.pic={\draw[fill=green!70,scale=.4] (0,0) circle(1);},
star/.pic={\draw[fill=magenta,scale=.4] (90:1)--(120:.6)--(150:1)--(180:.6)--
(210:1)--(240:.6)--(270:1)--(300:.6)--
(330:1)--(0:.6)--(30:1)--(60:.6)--cycle;},
square/.pic={\draw[fill=yellow,scale=.35] (-1,-1) rectangle (1,1);}
}
\tikzset{first card/.pic={  
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{circle}   ++(0:1) pic{circle} +(0:1) pic{circle}
(.5,1.5) pic{star}     ++(0:1) pic{star}   +(0:1) pic{circle}
(.5,.5)  pic{triangle} ++(0:1) pic{star}   +(0:1) pic{circle}
;}}
\tikzset{second card/.pic={ 
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{square} ++(0:1) pic{star} +(0:1) pic{circle}
(.5,1.5) pic{star}   ++(0:1) pic{star} +(0:1) pic{star}
(.5,.5)  pic{square} ++(0:1) pic{star} +(0:1) pic{circle}
;}}
\tikzset{third card/.pic={
\fill[white] (0,0) rectangle (3,3);         
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{square} ++(0:1) pic{circle} +(0:1) pic{triangle}
(.5,1.5) pic{square} ++(0:1) pic{circle} +(0:1) pic{circle}
(.5,.5)  pic{square} ++(0:1) pic{square} +(0:1) pic{square}
;}}
\tikzset{central card/.pic={    
\fill[white] (0,0) rectangle (3,3);
\draw (0,0) grid (3,3);
\path 
(.5,2.5) pic{star}     ++(0:1) pic{square}   +(0:1) pic{circle}
(.5,1.5) pic{triangle} ++(0:1) pic{triangle} +(0:1) pic{star}
(.5,.5)  pic{square}   ++(0:1) pic{circle}   +(0:1) pic{triangle}
;}}
\begin{tikzpicture}
\path 
(0,0) pic{first card}    +(1.5,3.5) node{first card}
(4,0) pic{second card}   +(1.5,3.5) node{second card}
(8,0) pic{third card}    +(1.5,3.5) node{third card}
(12,0) pic{central card} +(1.5,3.5) node{central card}
;
\end{tikzpicture}
\vspace*{5mm}

Before putting cards

\begin{tikzpicture} \fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);
\fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3); \fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3); \draw[very thick] (0,0) grid[step=3cm] (9,6); \end{tikzpicture} \vspace*{5mm}

After putting cards

\begin{tikzpicture} \fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);
\fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3); \fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3); \draw[very thick] (0,0) grid[step=3cm] (9,6); \path (0,0) pic{first card} (0,3) pic{second card} (6,0) pic{third card} (6,3) pic{central card} ; \end{tikzpicture}

\vspace*{5mm}

We can scale whole figure with \verb|[scale=.5,transform shape]|

\begin{tikzpicture}[scale=.5,transform shape] \fill[magenta] (0,0) rectangle ++(3,3) rectangle +(-3,3);
\fill[blue] (3,0) rectangle ++(3,3) rectangle +(3,3); \fill[yellow] (3,6) rectangle ++(3,-3) rectangle +(3,-3); \draw[very thick] (0,0) grid[step=3cm] (9,6); \path (0,0) pic{first card} (0,3) pic{second card} (6,0) pic{third card} (6,3) pic{central card} ; \end{tikzpicture} \end{document}

Appendix I update a code using the library shapes.geometric, as OP requested: just change the definition of pics triangle, circle, star, and square as follows.

\usetikzlibrary{shapes.geometric}
\tikzset{triangle/.pic={
\node[regular polygon,regular polygon sides=3,minimum size=8mm, draw,fill=blue,shift={(0,-1mm)}]{};},
circle/.pic={\node[circle,draw,fill=green!70,minimum size=8mm]{};},
star/.pic={\node[star,star points=6,draw,fill=red,minimum size=8mm]{};},
square/.pic={\node[draw,fill=yellow,minimum size=8mm]{};}
}
Black Mild
  • 17,569
  • 1
    @– Black Mild Thank you for your answer. I appreciate your solution. But how can I apply my code, instead of yours, to the pic definition. I have my reasons for doing so. For example, using the following code to define the triangle pic: regular polygon, regular polygon sides=3, draw=black, line width=\lwdth, fill=blue, minimum size=1.4cm – Hany Sep 26 '22 at 06:34
  • @Hany It's my coding style. I always code in the simplest way I can. Honestly, your code is not so clean. Anyway, I add an update. Hope that helps! – Black Mild Sep 26 '22 at 11:35
  • @– Black Mild Thank you very much for your time. I understand your opinion and coding style. My question represents only one situation used in my documents. I use many different shapes in different locations. So, in my documents, using polygon definitions is more convenient and requires less effort than defining the different shapes, as in your code. Again, thank you very much. I appreciate your answer and concern very much. – Hany Sep 27 '22 at 05:20