我想在 tikz 中创建一棵树,并使用图案突出显示某些节点。但是,我想将图案限制在矩形内,以便文本在白色背景上可见。
以下是 MWE:
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns,trees}
\begin{document}
\begin{tikzpicture}[
every node/.style={rectangle,draw,align=center, minimum height=0.5cm},
grandchild/.style={grow=down,xshift=0.5em,anchor=west,
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
first/.style={level distance=7mm},
second/.style={level distance=14mm},
third/.style={level distance=21mm},
level 1/.style={sibling distance=12em}]
\node[font = {\small\bfseries}] {This is a very long root}
[edge from parent fork down,level distance=10mm]
child{node[font = {\footnotesize}, style={}] {Foo\\[-0.3mm]Bar}
[edge from parent fork down,level distance=10mm]
child[style={sibling distance=6em, level distance=10mm}] {node[font = {\footnotesize}, ]{Alpha}}
child[style={sibling distance=6em, level distance=10mm}] {node[font = {\footnotesize}]{Beta}
}
child[style={sibling distance=6em, level distance=10mm,}] {node[font = {\footnotesize},preaction={clip, postaction={pattern=north west lines, pattern color=red, }}]{Charlie}}
}
child{node[font = {\footnotesize},] {Bar\\[-0.3mm]Foo}
child[grandchild,first] {node[font = {\footnotesize},]{Test 1}}
child[grandchild,second] {node[font = {\footnotesize},]{Test 2}}
child[grandchild,third] {node[font = {\footnotesize},preaction={clip, postaction={pattern=north west lines, pattern color=red, }}]{Test 3}}
};
\end{tikzpicture}
\end{document}
其结果为:
我想要的结果应该是这样的:
答案1
您可以将文本包装到白色框中:
\documentclass[border=0.1cm]{standalone}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns,trees}
\setlength{\fboxsep}{1.5pt}
\begin{document}
\begin{tikzpicture}[
every node/.style={rectangle,draw,align=center, minimum height=0.5cm},
grandchild/.style={grow=down,xshift=0.5em,anchor=west,
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
first/.style={level distance=7mm},
second/.style={level distance=14mm},
third/.style={level distance=21mm},
level 1/.style={sibling distance=12em}]
\node[font = {\small\bfseries}] {This is a very long root}
[edge from parent fork down,level distance=10mm]
child{node[font = {\footnotesize}, style={}] {Foo\\[-0.3mm]Bar}
[edge from parent fork down,level distance=10mm]
child[style={sibling distance=6em, level distance=10mm}] {node[font = {\footnotesize}, ]{Alpha}}
child[style={sibling distance=6em, level distance=10mm}] {node[font = {\footnotesize}]{Beta}
}
child[style={sibling distance=6em, level distance=10mm,}] {node[inner sep=.3333em-\fboxsep,font = {\footnotesize},preaction={clip, postaction={pattern=north west lines, pattern color=red, }}]{\colorbox{white}{Charlie}}}
}
child{node[font = {\footnotesize},] {Bar\\[-0.3mm]Foo}
child[grandchild,first] {node[font = {\footnotesize},]{Test 1}}
child[grandchild,second] {node[font = {\footnotesize},]{Test 2}}
child[grandchild,third] {node[inner sep=.3333em-\fboxsep,font = {\footnotesize},preaction={clip, postaction={pattern=north west lines, pattern color=red, }}]{\colorbox{white}{Test 3}}}
};
\end{tikzpicture}
\end{document}