Optimize a code with TikZ

A bit more code golfing

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[addlabel/.style={label={[anchor=#1]#1:\textbf{\xe}}}]
\fill[red,even odd rule] (0,0) circle (0.5) circle (1) circle (1.5);
\foreach\x[evaluate={\xe=int(10-\x)}] in {1,...,5}{
  \ifnum\x=2\relax\tikzset{text=white}\fi
  \node[minimum size=(1+\x)*1 cm,draw,circle,addlabel/.list={0,90,180,270}] {};
}
\node[circle,draw,text=white,minimum size=1cm] at (0,0) {\textbf{10}};
\end{tikzpicture}
\end{document}

enter image description here


Whether you like it more or not, I can't say, but it's enough with a single outer loop, and a couple of \ifnums to change the formatting. By the way,\bf has been deprecated for about 25 years.

output of code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [count=\score from 5] \radius in {3,2.5,...,0.5}
{
\ifnum \score = 8
  \filldraw [thick,draw=black,fill=red] circle[radius=\radius cm];
  \foreach \ang in {0,90,180,270}
     \node [text=white,font=\bfseries] at (\ang:\radius cm-0.25cm) {\score};
\else
  \ifnum \score = 10
    \filldraw [thick,draw=black,fill=red] circle[radius=\radius cm];
    \node [text=white,font=\bfseries] at (0,0) {\score};     
  \else
    \filldraw [thick,draw=black,fill=white] circle[radius=\radius cm];
    \foreach \ang in {0,90,180,270}
       \node [font=\bfseries] at (\ang:\radius cm-0.25cm) {\score};
   \fi
\fi
}   
\end{tikzpicture}
\end{document}

Some suggestions:

  • never use \bf (it is deprecated for 25 years) and do not use it in every node
  • use one foreach loop
  • you can put the node (for the 10) at the end of the draw command

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.text,calc,arrows.meta}
\begin{document}
\begin{tikzpicture}[font=\bfseries]
\coordinate (O) at (0,0);
\foreach \r in {0,...,5} \draw[thick] (O) circle (0.5cm + .5cm*\r);
\draw[fill=red,draw=red] (O) circle (0.5) node[white] {10};
\draw[fill=red,draw=red,even odd rule] (O) circle (1.5cm) circle (1cm);
\foreach \i in {0,90,180,270}{
    \node at (\i:0.75cm) {9};
    \node[white] at (\i:1.25cm) {8};
    \node at (\i:1.75cm) {7};
    \node at (\i:2.25cm) {6};
    \node at (\i:2.75cm) {5};
}
\end{tikzpicture}
\end{document}

Tags:

Tikz Pgf