我对 Qrrbrbirlbel 的答案中复杂的 TikZ 节点填充解决方案有一个疑问https://tex.stackexchange.com/a/130119/238944
看: https://www.latex4technics.com?note=zzvqse
\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{fit}
\usetikzlibrary{shapes.multipart}
\tikzset{
three parts left node/.style={
every three parts node,
name=qrr@tikz@tp@l,
%fill=green,
at=(qrr@tikz@[email protected]),
anchor=north east,
outer sep=+0pt},
three parts right node/.style={
every three parts node,
name=qrr@tikz@tp@r,
%fill=red,
at=(qrr@tikz@[email protected]),
anchor=north west,
outer sep=+0pt},
three parts top node/.style={
every three parts node,
name=qrr@tikz@tp@t,
fill=yellow,
outer sep=+0pt},
three parts node/.style={
inner sep=-.5\pgflinewidth,
minimum size=+0pt,
fit=(qrr@tikz@tp@l)(qrr@tikz@tp@r)(qrr@tikz@tp@t)},
every three parts node/.style={align=center},
three parts node after/.style={
insert path={
([xshift=\pgflinewidth] qrr@tikz@[email protected] west) edge[three parts node after edge 1/.try] ([xshift=-\pgflinewidth]qrr@tikz@[email protected] east)
([yshift=-.5\pgflinewidth]qrr@tikz@[email protected] east) edge[three parts node after edge 2/.try] ([yshift=\pgflinewidth] qrr@tikz@[email protected] west)}},
}
\makeatletter
\tikzset{
three parts/.code args={#1#2#3}{%
\pgfutil@ifnextchar[%
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts top node}}
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts top node}[]}#1\egroup\pgf@stop
\pgfutil@ifnextchar[%
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts left node}}
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts left node}[]}#2\egroup\pgf@stop
\pgfutil@ifnextchar[%
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts right node}}
{\expandafter\tikz@scan@next@command\qrr@tikz@split@nodeOpt{three parts right node}[]}#3\egroup\pgf@stop
\tikz@scan@next@command node[three parts node/.try]{}[three parts node after]\pgf@stop
}
}
\def\qrr@tikz@split@nodeOpt#1[#2]{node[#2,#1]\bgroup}
\makeatother
\begin{document}
\begin{tikzpicture}[three parts node/.append style={draw,rounded corners}]
\path [three parts={[blue!50!red]\textit{marriage}, 1850}{[blue]John\\Smith}{[red]Mary\\Jones}] ;
\node [ rounded corners,rectangle split,rectangle split parts=2,draw=black, rectangle split part fill={yellow,none}] at (0,1) (v){marriage\nodepart{two} John+Marry};
\end{tikzpicture}
\end{document}
当使用背景色时,解决方案的行为有所不同,即背景色为尖角着色,而原始分割矩形仅为圆角着色。
如何修改源代码才能让背景适应圆角?
答案1
正如 user238301 所建议的,您可以使用tcolorbox
。
为了避免tcolorbox
在 内部嵌套问题tikzpicture
,您可以创建普通盒子并使用它们。
\documentclass[border=10pt]{standalone}
\usepackage{makecell}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,xparse}
\tcbset{
common/.style={
colback = white,
colbacktitle=yellow,
size=fbox,
nobeforeafter,
center title,
arc=1.5mm
},
triplebox/.style={
common,
enhanced,
sidebyside gap=7pt,
width=3cm,
coltitle=red,
segmentation style=solid,
},
}
\newtcolorbox{doulblebox}[2][]{%
common,
coltitle=black,
capture=hbox,
title = #2,
#1}
\newsavebox{\boxdouble}
\newsavebox{\boxtriple}
\begin{document}
\savebox{\boxdouble}{%
\begin{doulblebox}{marriage}
John+Mary
\end{doulblebox}%
}
\savebox{\boxtriple}{%
\tcbsidebyside[
triplebox,
title={{\emph{marriage}, 1850}},
]
{\makecell{John\\ Smith}}
{\makecell{Mary\\ Jones}}%
}
\begin{tikzpicture}
\node (A) {\usebox{\boxdouble}};
\node[below=0mm of A] {\usebox{\boxtriple}};
\end{tikzpicture}
\end{document}
答案2
这里的问题是,顶部节点的形状是矩形,但我们希望它被填充为好像它的两个上角是圆的并且两个下角保持尖锐。
目前 tikz 及其(“标准”)库不提供此类形状。如果你的用例不是那么灵活,那么替换
three parts top node/.style={
fill=yellow,
...
}
和
three parts top node/.style={
path picture={
\fill[yellow, rounded corners]
(path picture bounding box.south west)
-- (path picture bounding box.north west)
-- (path picture bounding box.north east)
[sharp corners]-- (path picture bounding box.south east)
-- cycle;
},
...
}
有助于。
path picture
PS: 我从以下网址学到了 option 的使用方法这个答案。