使用 TikZ 优化代码

使用 TikZ 优化代码

可以优化下面的代码吗?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.text,calc,arrows.meta}
\begin{document}
\begin{tikzpicture}
\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);
\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) {\bf 9};
\foreach \i in {0,90,180,270} \node[white] at (\i:1.25cm) {\bf 8};
\foreach \i in {0,90,180,270} \node at (\i:1.75cm) {\bf 7};
\foreach \i in {0,90,180,270} \node at (\i:2.25cm) {\bf 6};
\foreach \i in {0,90,180,270} \node at (\i:2.75cm) {\bf 5};
\node[white] at (O) {\bf 10};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

更多代码高尔夫

\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}

在此处输入图片描述

答案2

我不知道你是否更喜欢它,但只需一个外循环和几个\ifnums 就可以更改格式。顺便说一句,\bf它已经被弃用了大约 25 年。

代码输出

\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}

答案3

一些建议:

  • 永远不要使用\bf(它已被弃用 25 年了),也不要在每个节点上使用它
  • 使用一个 foreach 循环
  • 你可以将节点(10)放在绘制命令的末尾

代码:

\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}

答案4

忍不住要用 MetaPost 画这幅图。以下代码(包含在 LuaLaTeX 程序中)可能也需要一些优化。它使用一个外循环和一个内循环,以及其中的条件指令。

\documentclass[border=3mm]{standalone}
\usepackage{luatex85,luamplib}
\begin{document}
\begin{mplibcode}
u=.5cm;
beginfig(1);
  for i = 6 downto 1:
    path circle; circle = fullcircle scaled (u*2i);
    if (i=3) or (i=1): fill circle withcolor red; fi
    if i=2: unfill circle; fi
    draw circle withpen pencircle scaled .8bp;
    if i>1:
        m := u*.5[i,i-1]; k := 11-i;
        for j = 0 upto 3:
            draw thelabel(TEX("\textbf{" & decimal k & "}"), m*dir(j*90))
                if i=3: withcolor white fi;
        endfor;
    else: draw thelabel(TEX("\textbf{10}"), origin) withcolor white; fi
  endfor;
endfig;
\end{mplibcode}
\end{document}

在此处输入图片描述

相关内容