将矩形并排分割,每半部分使用不同的渐变进行填充

将矩形并排分割,每半部分使用不同的渐变进行填充

作为后续问题使用渐变还是图片作为分割矩形的填充?非常感谢 Zarko 的回答,我尝试修改他们的代码以并排分割矩形。

以下代码:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{backgrounds, fit, positioning, shapes.multipart}
\newcommand\ppbb{path picture bounding box}

\begin{document}
\begin{tikzpicture}[
box/.style args = {#1/#2}{
   rectangle split, rectangle split horizontal, rectangle split parts=2,
   rounded corners,
   text width=3cm, align=center,
   path picture={   \begin{scope}[on background layer]
                    \draw[thick, % node part one
                          left color=#1, right color=#1!20!white]
        (\ppbb.south) [rounded corners] -| (\ppbb.west) |- (\ppbb.north) [sharp corners] -| (\ppbb.north |- \ppbb.east) -| cycle;
                    \draw[thick, % node part two
                          left color=#2, right color=#2!20!white]
        (\ppbb.south) [rounded corners] -| (\ppbb.east) |- (\ppbb.north) [sharp corners] -| (\ppbb.south |- \ppbb.west) -| cycle;
                    \end{scope}
                }%end of path picture
        }
                ]
\node [box=blue/red]
   {\nodepart{one} text \\ text \\ text \\ text
    \nodepart{two} text \\ text \\ text \\ text};
\end{tikzpicture}
\end{document}

产生下图: 在此处输入图片描述

但是,我不知道如何修复上部(我希望上部与下部相同),因为我不理解这部分代码:

\draw[thick, % node part one
                      left color=#1, right color=#1!20!white]
    (\ppbb.south) [rounded corners] -| (\ppbb.west) |- (\ppbb.north) [sharp corners] -| (\ppbb.north |- \ppbb.east) -| cycle;
                \draw[thick, % node part two
                      left color=#2, right color=#2!20!white]
    (\ppbb.south) [rounded corners] -| (\ppbb.east) |- (\ppbb.north) [sharp corners] -| (\ppbb.south |- \ppbb.west) -| cycle;

有人可以解释一下吗?

答案1

您的 mwe 非常接近正确的解决方案 :-)。您只需要更正绘制填充边框。这是“tikz \& pgf manula, v 3.0.3a”第 729 页上的有用图像。

根据我的附录中的答案

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{backgrounds, fit, positioning, shapes.multipart}
\newcommand\ppbb{path picture bounding box}

\begin{document}
\begin{tikzpicture}[
box/.style args = {#1/#2}{
        rectangle split, rectangle split horizontal, rectangle split parts=2,
        rounded corners,
        text width=3cm, align=center,
        draw,  thick,
        append after command={\pgfextra
            \fill[left color=#1, right color=#1!20!white]
        (\tikzlastnode.south)
        [rounded corners] -| (\tikzlastnode.west) |- (\tikzlastnode.one north)
        [sharp corners]   -| (\tikzlastnode.one split) |- cycle;
            \fill[left color=#2, right color=#2!20!white]
        (\tikzlastnode.two south)
        [rounded corners] -| (\tikzlastnode.east) |- (\tikzlastnode.north)
        [sharp corners]   -| (\tikzlastnode.one split) |- cycle;
                                        \endpgfextra}% end of the append after command
                            }% end of the box style definition
                    ]
\node [box=blue/red]
   {\nodepart{one} text \\ text \\ text \\ text
    \nodepart{two} text \\ text \\ text \\ text};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容