tikz,形状参数的条件设置

tikz,形状参数的条件设置

在我的插图中,我使用了秒表(计时器)的简化草图,它们以三种不同的“模式”出现:(1)间隔开始(绿色),(2)进行中(黄色)和时间已到(红色)。

在此处输入图片描述

对于每一种情况,我都确定了不同的风格,只有两行有所不同,参见 MWE:

\documentclass[12pt,tikz,border=3mm]{standalone}
    \usetikzlibrary{chains,shapes}
\begin{document}
    \begin{tikzpicture}
\tikzset{
     node distance = 5mm,
       start chain = going right,
boxG/.style 2 args = {name=n#1,
                      shape=rectangle, draw=#2, thick, inner sep=0mm, on chain,
                      node contents={\tikz{
    \draw[radius=3.5mm,fill=#2!30] (0,0) circle;
    \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) arc[start angle=90,end angle=135] -- cycle;
    \foreach \i in {0,30,...,330}   \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
    \draw                       ( 0mm,-1mm) -- ++ (0mm,4.0mm);
    \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             ( 0mm,-1mm) -- ++ ( 90:4.0mm);
                                            }}
                      },
boxY/.style 2 args = {name=n#1,
                      shape=rectangle, draw=#2, thick, inner sep=0mm, on chain,
                      node contents={\tikz{
    \draw[radius=3.5mm,fill=#2!30] (0,0) circle;
    \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) arc[start angle=90,end angle=135] -- cycle;
    \foreach \i in {0,30,...,330}   \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
    \draw                       ( 0mm,-1mm) -- ++ (0mm,4.0mm);
    \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             (+1mm,+1mm) -- ++ (225:4.0mm);
                                            }}
                      },
boxR/.style 2 args = {name=n#1,
                      shape=circle, draw=#2, thick, inner sep=0mm, on chain,
                      node contents={\tikz{
    \draw[radius=3.5mm,fill=#2!30] (0,0) circle;
    \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) arc[start angle=90,end angle=135] -- cycle;
    \foreach \i in {0,30,...,330}   \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
    \draw                       ( 0mm,-1mm) -- ++ (0mm,4.0mm);
    \draw[yellow,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             (+1mm,-1mm) -- ++ (135:4.0mm);
                                            }}
                      },
}
\node[boxG={1}{green}];
\node[boxY={2}{yellow}];
\node[boxR={3}{red}];
    \end{tikzpicture}
\end{document}

我想知道是否可以只确定一种风格,其中选择最后两行关于使用的颜色,如下所示:

timer/.style 2 args = {name=n#1,
                      shape=circle, draw=#2, thick, inner sep=0mm, on chain,
                      node contents={\tikz{
    \draw[radius=3.5mm,fill=#2!30] (0,0) circle;
    \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) arc[start angle=90,end angle=135] -- cycle;
    \foreach \i in {0,30,...,330}   \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
    \draw                       ( 0mm,-1mm) -- ++ (0mm,4.0mm);
%%%% pseudocode for conditional selecting of appropriate last two lines
if #2 = green
    \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             ( 0mm,-1mm) -- ++ ( 90:4.0mm);
else if #2 = yellow
    \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             (+1mm,+1mm) -- ++ (225:4.0mm);
else if #2 = red
    \draw[yellow,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
    \draw[thick,->]             (+1mm,-1mm) -- ++ (135:4.0mm);
fi fi fi
%%%%
                        }}}

附加问题:在第三个计时器中,为什么内部图片和外部节点之间的距离不像前两个那样等于零(最后我想用一个圆圈作为外部节点)?

答案1

这是另一个建议,使用 内部的节点。您可以在代码外部pic引用此节点及其锚点。<picname>-cpic

\documentclass[12pt,tikz,border=3mm]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{positioning}
\newcommand*\clcol{}

\tikzset{
    pics/clock/.style={code={
    \edef\clcol{#1}
    \node(-c)[draw=\clcol,thick,minimum size=7mm+\pgflinewidth]{};
    \coordinate(q)at(-c.center);
    \draw[radius=3.5mm,fill=\clcol!30](q)circle;
    \fill[radius=3.5mm-.5*\pgflinewidth,white] (q) -- + (90:{3.5mm-.5*\pgflinewidth}) arc[start angle=90,end angle=135] -- cycle;
    \foreach \i in {0,30,...,330}   \draw (q) + (\i:2.5mm) -- +(\i:3mm);
    \ifdefstring{\clcol}{green}{%
      \draw[orange,ultra thick]   (q)++(+1mm,-1mm) -- +(135:4.5mm);
      \draw[thick,->]             (q)++( 0mm,-1mm) -- +( 90:4.0mm);}{}
    \ifdefstring{\clcol}{yellow}{%
      \draw[orange,ultra thick]   (q)++(+1mm,-1mm) -- +(135:4.5mm);
      \draw[thick,->]             (q)++(+1mm,+1mm) -- +(225:4.0mm);}{}
    \ifdefstring{\clcol}{red}{%
      \draw[yellow,ultra thick]   (q)++(+1mm,-1mm) -- +(135:4.5mm);
      \draw[thick,->]             (q)++(+1mm,-1mm) -- +(135:4.0mm);}{}
    }}}

\begin{document}
\begin{tikzpicture}
\draw[help lines](-1,-1)grid(2,2);
\pic(n1)[circle] at (0,0){clock=red};
\pic(n2)[right=1cm,anchor=north west] at (n1-c){clock=yellow};
\pic(n3)[above right=1cm and 1cm,anchor=south] at (n1-c){clock=green};
\draw[->](n1-c)edge(n2-c)
         (n2-c)edge(n3-c)
         (n3-c)edge(n1-c);
\end{tikzpicture}
\end{document}

在此处输入图片描述


解释一下题目中的MWE中黑色画圆和红色圆圈节点之间的距离:设置node contents在一个矩形框中,圆圈节点适合这个矩形框。

在此处输入图片描述

\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\tikz{%
  \node[circle,inner sep=0pt,draw=red,thick]{\tikz{\draw[radius=3.5mm]circle;}};
  \node[draw=blue,thin, dashed,minimum size=7mm]{};
  }
\end{document}

第二版(无pic

为了解决圆节点中的距离问题,我使用选项minimum size而不是inner sep=0pt。使用overlay隐藏node contents其尺寸。

\documentclass[12pt,tikz,border=3mm]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{chains,shapes}
\begin{document}
\begin{tikzpicture}
  \tikzset{
    node distance = 5mm,
    start chain = going right,%
    box/.style 2 args = {
      name=n#1,
      draw=#2,
      thick,
      minimum size=7mm+\pgflinewidth,
      on chain,
      node contents={\edef\clcol{#2}\tikz[overlay]{
        \draw[radius=3.5mm,fill=\clcol!30] (0,0) circle;
        \fill[radius=3.5mm-0.5*\pgflinewidth,white] (0,0) -- + (90:{3.5mm-0.5*\pgflinewidth}) arc[start angle=90,end angle=135] -- cycle;
        \foreach \i in {0,30,...,330}   \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
        \ifdefstring{\clcol}{green}{%
          \draw[orange,ultra thick]   (+1mm,-1mm) -- +(135:4.5mm);
          \draw[thick,->]             ( 0mm,-1mm) -- +( 90:4.0mm);}{}
        \ifdefstring{\clcol}{yellow}{%
          \draw[orange,ultra thick]   (+1mm,-1mm) -- +(135:4.5mm);
          \draw[thick,->]             (+1mm,+1mm) -- +(225:4.0mm);}{}
        \ifdefstring{\clcol}{red}{%
          \draw[yellow,ultra thick]   (+1mm,-1mm) -- +(135:4.5mm);
          \draw[thick,->]             (+1mm,-1mm) -- +(135:4.0mm);}{}
        }}
      },
  }
  \node[box={1}{green}];
  \node[box={2}{yellow}];
  \node[box={3}{red},circle];
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这不是一个明确的答案,因为我不知道你想如何使用这些符号,而且代码不使用条件样式。我所做的是使用pics(你需要 TikZ 3.0)。

第一张图片clock绘制符号的公共部分,其他三张图片使用它(固定其填充颜色)并绘制特定特征。目前,每个特定符号boxGboxYboxR固定其颜色,因此不需要像您的示例中那样使用第二个参数。

关于您示例中的第一个参数(节点名称),我不确定如何处理它,也避免使用它。如果您解释为什么需要它们,代码可能会改变。

\documentclass[12pt,tikz,border=3mm]{standalone}

\tikzset{
    pics/clock/.style = {
        code = {
            \draw[radius=3.5mm,fill=#1!30] (0,0) circle;
            \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) 
                arc[start angle=90, end angle=135] -- cycle;
            \foreach \i in {0,30,...,330}   
                \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
            \draw ( 0mm,-1mm) -- ++ (0mm,4.0mm);
        }
    },
    pics/boxG/.style = {
        code = {
            \draw[thick,green] (-3.6mm,-3.6mm) rectangle (3.6mm,3.6mm);
            \draw (0,0) pic {clock = {green}};
            \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] ( 0mm,-1mm) -- ++ ( 90:4.0mm);                              
        }
    },
    pics/boxY/.style = {
        code = {
            \draw[thick,yellow] (-3.6mm,-3.6mm) rectangle (3.6mm,3.6mm);
            \draw (0,0) pic {clock = {yellow}};
            \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] (+1mm,+1mm) -- ++ (225:4.0mm);                              
        }
    },
    pics/boxR/.style= {
        code = {
            \draw[thick,red] (0,0) circle[radius=3.6mm];
            \draw (0,0) pic {clock = {red}};
            \draw[yellow,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] ( +1mm,-1mm) -- ++ (135:4.0mm);                              
        }
    },
}

\begin{document}
    \begin{tikzpicture}
    \draw (0,0) pic {boxG};
    \draw (1,0) pic {boxY};
    \draw (2,0) pic {boxR};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

第二版本

不确定这是否是一种改进,但现在只clock定义了一个。添加了两个新键,border和,time可能值为RGY。它们选择边框类型和箭头位置。

我必须承认我不太了解 pgfkeys 的工作原理。这是我的第一次,所以我希望其他人能纠正或提出更好的答案。

尽管语法发生了变化,但结果与第一个版本完全相同。

\documentclass[12pt,tikz,border=3mm]{standalone}

\tikzset{
    /border/.is choice,
    /border/G/.code = {
        \draw[thick,green] (-3.6mm,-3.6mm) rectangle (3.6mm,3.6mm);
    },
    /border/Y/.code = { 
        \draw[thick,yellow] (-3.6mm,-3.6mm) rectangle (3.6mm,3.6mm);
    },
    /border/R/.code = { 
        \draw[thick,red] (0,0) circle[radius=3.6mm];
    },
    /time/.is choice,
    /time/G/.code = {
            \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] ( 0mm,-1mm) -- ++ ( 90:4.0mm);                              
    },
    /time/Y/.code = {
            \draw[orange,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] (+1mm,+1mm) -- ++ (225:4.0mm);                              
    },
    /time/R/.code = {
            \draw[yellow,ultra thick]   (+1mm,-1mm) -- ++ (135:4.5mm);
            \draw[thick,->] ( +1mm,-1mm) -- ++ (135:4.0mm);                              
    },
    pics/clock/.style 2 args= {
        code = {
            \pgfkeys{border=#1},    
                \draw[radius=3.5mm,fill=#2!30] (0,0) circle;
                \fill[radius=3.4mm,white] (0,0) -- + (90:3.4mm) 
                arc[start angle=90, end angle=135] -- cycle;
                \foreach \i in {0,30,...,330}   
                \draw (0,0) ++ (\i:2.5mm) -- (\i:3mm);
                \draw ( 0mm,-1mm) -- ++ (0mm,4.0mm);
            \pgfkeys{time=#1}
        }
    },
}

\begin{document}
    \begin{tikzpicture}
    \draw (0,0) pic {clock={G}{green}};
    \draw (1,0) pic {clock={Y}{yellow}};
    \draw (2,0) pic {clock={R}{red}};
    \end{tikzpicture}
\end{document}

相关内容