如何在 tikz 中绘制括号树?
例如
bob {dave
eve
alice { francis
carol -- { grace
hans
答案1
答案2
与forest
和tikz
,以非常复杂的方式......
\documentclass[border=3.141592]{standalone}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing,
calligraphy}
\tikzset{
BC/.style args = {#1/#2}{
decorate,
decoration={calligraphic brace, amplitude=2pt,
pre =moveto, pre length=1pt,
post=moveto, post length=1pt,
raise=#1,
#2},% for mirroring of brace
thick,
pen colour={red}}, % <--- you can change to desired color
}
\begin{document}
\begin{forest}
for tree = {
align= left,
inner sep = 0pt,
edge = {draw=none},
anchor = west,
tier/.option = level,
grow'= 0,
l sep= 3mm,
s sep= 4mm}
[Alice
[Bob, name=b
[Dave\\ eve, name=de]
]
[Carol, name=c
[Francis\\ Grace\\ Henry, name=fgh]
]
]
\draw[BC=1mm/mirror] (b.north west) -- (c.south west);
\draw[BC=1mm/mirror] (de.north west) -- (de.south west);
\draw[BC=1mm/mirror] (fgh.north west) -- (fgh.south west);
\end{forest}
\end{document}
编写如此复杂的代码是否值得?;-)
答案3
这是一个forest
使用forked edge
s 和rounded corners
制作括号的解决方案。
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={grow'=east, forked edge, text width=8mm, edge=rounded corners, l sep=3mm, fork sep=1mm}
[Alice[Bob[Dave][Eve]][Carol[Frances][Grace, no edge][Hans]]]
\end{forest}
\end{document}
答案4
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{fillbetween}
\usepackage{xcolor}
\usepackage{nicefrac}
\begin{document}
\begin{tikzpicture}
\coordinate [label=left: Alice] (O) at (0,0);
\coordinate [label=right: Bob] (A0) at (2,1.5);
\coordinate [label=right: Carol](B0) at (2,-1.5);
\draw (O)--(A0);
\draw (O)--(B0);
\coordinate (A1) at (3.2,1.5);
\coordinate (B1) at (3.2,-1.5);
\coordinate [label=right: Dave] (AA1) at (5,2);
\coordinate [label=right: Eve](BB1) at (5,1);
\draw (A1)--(AA1);
\draw (A1)--(BB1);
\coordinate[label=right: Francis] (BA1) at (5,0);
\coordinate[label=right: Hans] (BB1) at (5,-3);
\coordinate[label=right: Grace] (CC1) at (5,-1.5);
\draw (B1)--(BA1);
\draw (B1)--(BB1);
\draw (B1)--(CC1);
\end{tikzpicture}
\end{document}