我正在使用以下解决方案如何使用不同的颜色和文本行将 tikz 矩形一分为二。除了这个解决方案之外,是否可以将一行分成两行并为其涂上不同的颜色?
@Torbjørn T.
链接答案中的解决方案:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\tikzset{
state/.style={
rectangle split,
rectangle split parts=2,
rectangle split part fill={red!30,blue!20},
rounded corners,
draw=black, very thick,
minimum height=2em,
text width=3cm,
inner sep=2pt,
text centered,
}
}
\begin{document}
\begin{tikzpicture}
\node [state] {text\\txet \nodepart{two} blue background \\ here};
\end{tikzpicture}
\end{document}
想要的输出:
|-----|-------|
| red | white |
|-----|-------|
| blue |
|_____________|
答案1
与@anis 答案类似,但问题中的代码重现显示了图像:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,
positioning}
\begin{document}
\tikzset{
sectors/.style n args={8}{minimum width=#1, minimum height=#2,
text depth=0.25ex, outer sep=0pt,
append after command={\pgfextra{\let\LN\tikzlastnode
\draw[fill=#3] (\LN.west) -| (\LN.north)
{[rounded corners=3mm] -- (\LN.north west)}
|- cycle;
%
\draw[fill=#5] (\LN.north) |- (\LN.east)
{[rounded corners=3mm] -- (\LN.north east)}
-- cycle;
%
\draw[fill=#7] (\LN.west) {[rounded corners=3mm] |- (\LN.south east)}
|- cycle;
\path (\LN.center) -- node[align=center] {#4} (\LN.north west)
(\LN.center) -- node[align=center] {#6} (\LN.north east)
(\LN.center) -- node[align=center] {#8} (\LN.south);
} } }
}
\begin{tikzpicture}
\node [sectors= {32mm}{24mm}
{red!30}{text\\ text}
{white}{Disk}
{blue!30}{blue bacground\\ here}] {};
\end{tikzpicture}
\end{document}
不过,我怀疑您正在寻找通用构建块(BB),您可以通过它组成任何块结构:
- 如果是这种情况,您应该意识到将节点拆分为子节点是一项非常艰巨的任务。
- 更简单的是按照所需的连接方式将节点(BB)紧密地连接在一起。
- 在这个过程中,绘图更加简单,如果节点形状是具有尖角的矩形(仅定义一个 BB 就足够了),
- 为了获得进一步/更好的帮助,请向我们展示您喜欢绘制的结构示例。
- 以下是由矩形形状的简单构建块组成的示例:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{
node distance = 0pt,
BB/.style args={#1/#2/#3}{% Building Box, options
draw, semithick,
font=\small\linespread{0.84}\selectfont, align=center,
minimum width=#1, minimum height=#2, text depth=0.25ex,
fill=#3, outer sep=0pt}
} % end of tikzset
\begin{tikzpicture}
\node (L1) [BB=32mm/8mm/teal!30] {basic layer};
\node (L2) [BB=32mm/8mm/yellow!30,
above=of L1] {second layer};
\node (L31) [BB=16mm/12mm/red!30,
above right=of L2.north west] {third\\ level\\ first};
\node (L32) [BB=16mm/12mm/orange!30,
right=of L31] {third\\ level\\ second};
\end{tikzpicture}
\end{document}
答案2
以下是我对这个解决方案的改编: 如何在 Tikz 中用 T 从内部线条创建圆形节点?T 将节点拆分为 3 个扇区
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\tikzset{sectors/.style n args={6}{%
rectangle,
draw,
minimum width=#4,
minimum height=#5,
append after command={%
\pgfextra{ %
\draw (\tikzlastnode.center) -- (\tikzlastnode.south) ;
\draw (\tikzlastnode.west) -- (\tikzlastnode.east) ;
\draw[fill = blue] (\tikzlastnode.west) rectangle (\tikzlastnode.north east);
\path (\tikzlastnode.center) -- node[#6] {#1} (\tikzlastnode.north);
\draw[fill = cyan] (\tikzlastnode.center) rectangle (\tikzlastnode.south west);
\path (\tikzlastnode.center) -- node[#6] {#2} (\tikzlastnode.south west);
\draw[fill = green] (\tikzlastnode.center) rectangle (\tikzlastnode.south east);
\path (\tikzlastnode.center) -- node[#6] {#3} (\tikzlastnode.south east);
} }}}
\begin{tikzpicture}[ultra thick]
\node [sectors={1}{2}{3}{5cm}{5cm}{font=\Huge\bfseries,text=red}] (c) {};
\end{tikzpicture}
\end{document}