节点和森林包的交替颜色边框

节点和森林包的交替颜色边框

我尝试跟随如何用 tikz 绘制双色线并在包中合并一个具有交替颜色的节点forest。在经典tikzpicture和具有node属性的情况下,它可以工作。但是,对于forest包,我使用以下 MWE 得到了奇怪的结果:

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}

\tikzset{%
/forest,
rbcirc/.style={circle,fill=white,draw=black,dash pattern= on 2pt off 2pt,very thick,postaction={draw,red,dash pattern= on 2pt off 2pt,dash phase=2pt,very thick},text width=1.5em}
}


\begin{document}
\begin{forest}
    [A, rbcirc]
  \end{forest}
\end{document}

红色圆圈

如果我注释掉后续操作,则会显示黑色虚线图案边框。

答案1

只需将样式定义为 TiZ 样式,它将正常工作。您希望将这些键传递给 TiZ。

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}

\tikzset{%
  rbcirc/.style={circle,fill=white,draw=black,dash pattern= on 2pt off 2pt,very thick,postaction={draw,red,dash pattern= on 2pt off 2pt,dash phase=2pt,very thick},text width=1.5em}
}


\begin{document}
\begin{forest}
    [A, rbcirc
    ]
\end{forest}
\end{document}

森林树中的红黑边界节点

编辑

我不太明白forest这里发生了什么。要知道,你必须问熟悉forest内部代码的人。但我可以告诉你发生了什么。

考虑以下序列:

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}

\forestset{%
  rbcirc/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern= on 2pt off 2pt,
    very thick,
    postaction={%
      draw,
      red,
      dash pattern= on 2pt off 2pt,
      dash phase=2pt,
      very thick,
    },
    text width=1.5em
  },
  rbcirc 2/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern= on 2pt off 2pt,
    very thick,
    postaction={%
      draw=red,
      dash pattern= on 2pt off 2pt,
      dash phase=2pt,
      very thick,
    },
    text width=1.5em
  },
}


\begin{document}
\begin{forest}
  [A, rbcirc
  ]
\end{forest}
\begin{forest}
  [A, rbcirc 2
  ]
\end{forest}
\end{document}

rbcirc 和 rbcirc 2

在第一种情况下,就好像我们已经传递red到了节点;在第二种情况下,就好像我们根本没有传递它。

现在考虑

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}

\forestset{%
  rbcirc 3/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern= on 2pt off 2pt,
    very thick,
    postaction={%
      dash pattern=on 2pt off 2pt,
      dash phase=2pt,
      very thick,
      draw,
      red,
    },
    text width=1.5em
  },
}


\begin{document}
\begin{forest}
  [A, rbcirc 3
  ]
\end{forest}
\end{document}

失败并出现错误

! Package pgfkeys Error: The key '/tikz/dash pattern' requires a value. I am go
ing to ignore this key.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.729 \end{forest}
                  
? 

forest遇到未知密钥时,它会将其传递给 Ti通过 unknown to定义为等于node options。如果rbcirc|rbcirc 2|rbcirc 3定义为forest样式,rbcirc|rbcirc 2|rbcirc 3则已知forest,因此forest考虑构成样式的键值列表。似乎发生的事情是forest将此键值列表解析为逗号分隔的列表,而不考虑内部分组。我怀疑这不太正确,或者更多的事情会失败,但这似乎是效果。所以,对于rbcirc,TiZ 获取列表

anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=draw, red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em

这本质上

anchor=base,circle=,postaction=draw, red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em

我们得到一个带有红色虚线边框的红色实心圆圈。

对于rbcirc 2,的列表node options

anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=draw=red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em

这基本上

    anchor=base,circle=,fill=white,postaction=draw=red, dash pattern= on 2pt off 2pt, dash phase=2pt, very thick,,text width=1.5em

我们得到一个带有黑色虚线边框的白色节点。

但是rbcirc 3我们得到

anchor=base,circle=,fill=white,draw=black,dash pattern=on 2pt off 2pt,very thick=,postaction=dash pattern=on 2pt off 2pt, dash phase=2pt, very thick, draw, red,,text width=1.5em

因此,第二个dash pattern被解释为传递给的值,postaction而不是作为获取值的键on 2pt off 2pt

相反,如果rbcirc|rbcirc 2|rbcirc 3定义为 TiZ 风格,它们是未知的forest,因此直接作为node optionsTinode optionsZ.在这种情况下,

anchor=base,rbcirc=

anchor=base,rbcirc 2=

anchor=base,rbcirc 3=

索蒂Z 按预期解析样式的定义。

编辑2

我们forest.sty发现

\pgfqkeys{/forest}{%
  .unknown/.code={%
    \expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
    \ifpgfutil@in@
      \expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
    \else
      \edef\forest@marshal{%
        \noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname=\unexpanded{#1}}}%
      }\forest@marshal
    \fi
  },
}

\pgfkeyscurrentname=\unexpanded{#1}有问题,因为它基本上将值传回键而不使用外部括号。如果我们在输入中加倍括号,我们最终会=在末尾得到一个 ie,{<key>=<value>}=这可以工作,但可能不是官方认可的 TiZ 语法。

在序言中添加以下内容可修复此问题但我不知道是否安全不建议使用它

\makeatletter
\pgfqkeys{/forest}{% DO NOT TRY THIS AT HOME!
  .unknown/.code={%
    \expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
    \ifpgfutil@in@
      \expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
    \else
      \edef\forest@marshal{%
        \noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname={\unexpanded{#1}}}}%
      }\forest@marshal
    \fi
  },
}
\makeatother

例如,

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\usepackage{forest}
\makeatletter
\pgfqkeys{/forest}{% DO NOT TRY THIS AT HOME!
  .unknown/.code={%
    \expandafter\pgfutil@in@\expandafter.\expandafter{\pgfkeyscurrentname}%
    \ifpgfutil@in@
      \expandafter\forest@relatednode@option@setter\pgfkeyscurrentname=#1\forest@END
    \else
      \edef\forest@marshal{%
        \noexpand\pgfkeysalso{\forest@unknownto={\pgfkeyscurrentname={\unexpanded{#1}}}}%
      }\forest@marshal
    \fi
  },
}
\makeatother
\forestset{%
  rbcirc/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern= on 2pt off 2pt,
    very thick,
    postaction={%
      draw,
      red,
      dash pattern= on 2pt off 2pt,
      dash phase=2pt,
      very thick,
    },
    text width=1.5em
  },
  rbcirc 2/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern= on 2pt off 2pt,
    very thick,
    postaction={%
      draw=red,
      dash pattern= on 2pt off 2pt,
      dash phase=2pt,
      very thick,
    },
    text width=1.5em
  },
  rbcirc 3/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern=on 2pt off 2pt,
    very thick,
    postaction={%
      dash pattern= on 2pt off 2pt,
      dash phase=2pt,
      very thick,
      draw,
      red,
    },
    text width=1.5em
  },
  rbcirc 4/.style={%
    circle,
    fill=white,
    draw=black,
    dash pattern=on 2pt off 2pt,
    very thick,
    {%
      postaction={%
        dash pattern= on 2pt off 2pt,
        dash phase=2pt,
        very thick,
        draw,
        red,
      }%
    },
    text width=1.5em
  },
}


\begin{document}
\begin{forest}
  [A, rbcirc
  ]
\end{forest}
\begin{forest}
  [A, rbcirc 2
  ]
\end{forest}
\begin{forest}
  [A, rbcirc 3
  ]
\end{forest}
\begin{forest}
  [A, rbcirc 4
  ]
\end{forest}
\end{document}

使用森林风格和危险修改的代码为森林节点交替显示彩色边框

相关内容