如何在边界框中使用两种颜色?

如何在边界框中使用两种颜色?

我有一个自认为很简单的任务,即创建两个不同颜色的背景矩形。目的是区分一组概念和另一组概念。

我还没有找到适合我的情况的答案。这个问题是关于着色盒的我认为这很相关,但我无法让解决方案发挥作用。这道题的解法使用了 calc但我不知道如何指定第二种颜色,因为我不想要白色。

我最初尝试使用两个单独的矩形,但它们没有垂直对齐。该尝试的代码是:

\begin{tikzpicture}[every node/.style=draw, arrow/.style={thick, -stealth}]
\node[ellipse, text width=3.2cm] (PS) {Finding out about a problem};
\node[ellipse, below=3.5cm of PS] (RD) {Root definitions};
\node[ellipse, right=1cm of RD] (CM) {Conceptual models};
\node[ellipse, above=1.5cm of CM] (RWE) {Real world events};
\node[ellipse, above=1.55cm of RWE] (TA) {Taking action};
\node[draw=none,text width=3cm, above right=of RWE, xshift=.4cm, yshift=-.8cm] (box1) {Real-world events occurring over time};
\node[draw=none, text width= 3cm, above right = of CM, xshift=.4cm, yshift=-1.8cm] (box2) {Systems thinking on the real world};

\draw[arrow] (PS.south) -- (RD.north);
\draw[arrow] (RD.east) -- (CM.west);
\draw[arrow] (CM.north) -- (RWE.south);
\draw[arrow] (RWE.north) -- (TA.south);
\draw[arrow] (TA.west) -- (PS.east);

\begin{scope}[on background layer]
\node[fill=yellow!20, inner sep=10pt, rectangle, fit=(PS) (TA) (RWE) (box1)]{};
\node[fill=green!20, inner sep=10pt, rectangle, fit= (RD) (CM) (box2)]{};
\end{scope}
\end{tikzpicture}

矩形是错误的

然后我尝试了背景方法。这仍然无法正确对齐彩色矩形。更新尝试的代码是:

\begin{tikzpicture}[every node/.style=draw, arrow/.style={thick, -stealth}]
\node[ellipse, text width=3.2cm] (PS) {Finding out about a problem};
\node[ellipse, below=3.5cm of PS] (RD) {Root definitions};
\node[ellipse, right=1cm of RD] (CM) {Conceptual models};
\node[ellipse, above=1.5cm of CM] (RWE) {Real world events};
\node[ellipse, above=1.55cm of RWE] (TA) {Taking action};
\node[draw=none,text width=3cm, above right=of RWE, xshift=.4cm, yshift=-.8cm] (box1) {Real-world events occurring over time};
\node[draw=none, text width= 3cm, above right = of CM, xshift=.4cm, yshift=-1.8cm] (box2) {Systems thinking on the real world};

\draw[arrow] (PS.south) -- (RD.north);
\draw[arrow] (RD.east) -- (CM.west);
\draw[arrow] (CM.north) -- (RWE.south);
\draw[arrow] (RWE.north) -- (TA.south);
\draw[arrow] (TA.west) -- (PS.east);

\begin{pgfonlayer}{background}
\node[fill=yellow!20, draw=none, fit =(PS) (TA) (RWE) (box1) (current bounding box.north east)] {};
    \node[fill=green!20, draw=none, fit =(RD) (CM) (box2) (current bounding box.south east)] {};
\end{pgfonlayer}
\end{tikzpicture}

矩形仍然是错误的

但是,如果我使用边界框将绿色和黄色带的两条线替换为单一背景颜色,则垂直边框是相同的:

\begin{pgfonlayer}{background}
\node[fill=blue!30, fit= (current bounding box.north west) (current bounding box.south east)] {};   
\end{pgfonlayer}

这给了我: 整体边界框正确

是否有某种方法可以使用calc选项以预定义的垂直距离分割边界框,并使用两种颜色?

答案1

有很多种方法可以进行拆分,这里是其中一种。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,positioning,shapes.geometric}
\begin{document}
\begin{tikzpicture}[every node/.style=draw, arrow/.style={thick, -stealth}]
\node[ellipse, text width=3.2cm] (PS) {Finding out about a problem};
\node[ellipse, below=3.5cm of PS] (RD) {Root definitions};
\node[ellipse, right=1cm of RD] (CM) {Conceptual models};
\node[ellipse, above=1.5cm of CM] (RWE) {Real world events};
\node[ellipse, above=1.55cm of RWE] (TA) {Taking action};
\node[draw=none,text width=3cm, above right=of RWE, xshift=.4cm, yshift=-.8cm] (box1) {Real-world events occurring over time};
\node[draw=none, text width= 3cm, above right = of CM, xshift=.4cm, yshift=-1.8cm] (box2) {Systems thinking on the real world};

\draw[arrow] (PS.south) -- (RD.north);
\draw[arrow] (RD.east) -- (CM.west);
\draw[arrow] (CM.north) -- (RWE.south);
\draw[arrow] (RWE.north) -- (TA.south);
\draw[arrow] (TA.west) -- (PS.east);
\path (CM) -- (RWE) coordinate[pos=0.5] (aux)
([xshift=1ex,yshift=1ex]current bounding box.north east) coordinate (BBNE) 
([xshift=-1ex,yshift=-1ex]current bounding box.south west) coordinate (BBSW);; 
\begin{pgfonlayer}{background}
 \fill[yellow!20] (BBNE)  rectangle (BBSW|-aux);
 \fill[green!20] (BBSW)  rectangle (BBNE|-aux);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

获得双色背景的另一种方法:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,
                calc,
                fit,
                positioning,
                shapes.geometric}
\begin{document}
    \begin{tikzpicture}[
    node distance = 9mm and 12mm,
       ell/.style = {ellipse, draw, align=center, 
                     inner xsep=-3pt, outer sep=0pt},
  FIT/.style args = {#1/#2}{draw, fill = #1, fit = #2,
                     inner sep=0pt, outer sep=0pt,
                     node contents={}},
every edge/.style = {draw, thick, -stealth},
                    ]
\node[ell] (CM)                 {Conceptual models};
\node[ell, above=of CM] (RWE)   {Real world events};
\node[ell, above=of RWE] (TA)   {Taking action};
\node[ell, left=of TA] (PS)     {Finding out\\ about a problem};
\node[ell, at={(PS |- CM)}] (RD){Root definitions};
%
\node[right=of $(RWE.east)!0.5!(RWE.east |- TA)$,text width=9em] (box1) 
                                {Real-world events occurring over time};
\node[right=of $(RWE.east |- CM)$,text width=9em] (box2)
                                {Systems thinking on the real world};
%
\draw   (PS)    edge (RD)
        (RD)    edge (CM)
        (CM)    edge (RWE)
        (RWE)   edge (TA)
        (TA)    edge (PS);
%
    \begin{pgfonlayer}{background}
\node[fit=(PS) (CM) (box1)] (f) {};
\node[FIT=yellow!20/(f.north west) (f.north east) ($(RWE)!0.5!(CM)$)];
\node[FIT= green!20/(f.south west) (f.south east) ($(RWE)!0.5!(CM)$)];
    \end{pgfonlayer}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

此版本使用 [fit=...] 选项。请注意,每个节点都添加了 [inner sep],这对于添加边框很有用,但会更改当前边界框并与中点重叠。

\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds,positioning,shapes.geometric,fit,calc}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every node/.style=draw, arrow/.style={thick, -stealth},local bounding box=fred]
\node[ellipse, text width=3.2cm] (PS) {Finding out about a problem};
\node[ellipse, below=3.5cm of PS] (RD) {Root definitions};
\node[ellipse, right=1cm of RD] (CM) {Conceptual models};
\node[ellipse, above=1.5cm of CM] (RWE) {Real world events};
\node[ellipse, above=1.55cm of RWE] (TA) {Taking action};
\node[draw=none,text width=3cm, above right=of RWE, xshift=.4cm, yshift=-.8cm] (box1) {Real-world events occurring over time};
\node[draw=none, text width= 3cm, above right = of CM, xshift=.4cm, yshift=-1.8cm] (box2) {Systems thinking on the real world};

\draw[arrow] (PS.south) -- (RD.north);
\draw[arrow] (RD.east) -- (CM.west);
\draw[arrow] (CM.north) -- (RWE.south) coordinate[midway](divide);
\draw[arrow] (RWE.north) -- (TA.south);
\draw[arrow] (TA.west) -- (PS.east);
\end{scope}

\path (divide) +(0pt,0.333em) coordinate(dtop) +(0pt,-0.333em) coordinate(dbottom);

\begin{pgfonlayer}{background}
\node[fill=blue, draw=none, fit =(dtop) (fred.north west) (fred.north east)] {};
    \node[fill=green!20, draw=none, fit =(dbottom) (fred.south west) (fred.south east)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

答案4

这只是另一种方法。它声明一个辅助坐标来固定颜色位置之间的边界,并使用fit节点来inner sep=0pt绘制背景。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,positioning,shapes.geometric, fit}
\begin{document}
\begin{tikzpicture}[every node/.style=draw, arrow/.style={thick, -stealth}]
\node[ellipse, text width=3.2cm] (PS) {Finding out about a problem};
\node[ellipse, below=3.5cm of PS] (RD) {Root definitions};
\node[ellipse, right=1cm of RD] (CM) {Conceptual models};
\node[ellipse, above=1.5cm of CM] (RWE) {Real world events};
\node[ellipse, above=1.55cm of RWE] (TA) {Taking action};
\node[draw=none,text width=3cm, above right=of RWE, xshift=.4cm, yshift=-.8cm] (box1) {Real-world events occurring over time};
\node[draw=none, text width= 3cm, above right = of CM, xshift=.4cm, yshift=-1.8cm] (box2) {Systems thinking on the real world};

%No need for `.south` or `.north` anchors. Tikz automatically stops on node's borders.
\draw[arrow] (PS) -- (RD); 
\draw[arrow] (RD) -- (CM);
\draw[arrow] (CM) -- coordinate (aux) (RWE);
\draw[arrow] (RWE) -- (TA);
\draw[arrow] (TA) -- (PS);

\begin{pgfonlayer}{background}
\node[fill=yellow!20, draw=none, inner sep=0pt, 
    fit = (current bounding box.north west) (aux-|current bounding box.east)] {};
\node[fill=green!20, draw=none, inner sep=0pt,
    fit = (current bounding box.south west) (aux-|current bounding box.east)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容