![如何在 tikzpicture 中制作正方形](https://linux22.com/image/349573/%E5%A6%82%E4%BD%95%E5%9C%A8%20tikzpicture%20%E4%B8%AD%E5%88%B6%E4%BD%9C%E6%AD%A3%E6%96%B9%E5%BD%A2.png)
我有以下脚本:
\documentclass[conference,compsoc]{IEEEtran}
\usepackage{tikz} %for the heirarchical tree
%for the hierarchical tree
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}
\tikzset{
basic/.style = {draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
root/.style = {basic, rounded corners=2pt, thin, align=center,
fill=green!30},
level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=green!60,
text width=8em},
level 3/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em}
}
\begin{document}
\begin{tikzpicture}
[-,thick,%
every node/.style={shape=rectangle,inner sep=3pt,draw,thick}]
\footnotesize
\node {Parent} [edge from parent fork down]
[sibling distance=4cm]
child {node {Type}}
child {node {Title Here}
[sibling distance=2cm]
child {node {Two Words Long}}
child {node {Two Word Long}}
}
;
\end{tikzpicture}
\end{document}
我需要让子节点与其父节点的平方相似,并避免任何两个节点相交。如何做到这一点?
答案1
这很简单:不要定义带有圆角的节点形状,并使用适当的较大兄弟距离。对于矩形形状的节点:
\documentclass[conference,compsoc]{IEEEtran}
\usepackage{tikz}
\usetikzlibrary{arrows, positioning, shadows, shapes, trees}
\begin{document}
\begin{tikzpicture}[-,thick,%
every node/.style = {rectangle, draw, thick, fill=white,
drop shadow, font=\sffamily},
sibling distance = 30mm
]
\node {Parent} [edge from parent fork down]
child {node {Type}}
child {node {Title Here}
child {node {Two Words Long}}
child {node {Two Word Long}}
};
\end{tikzpicture}
\end{document}
更forest
简单,不需要定义兄弟距离,节点之间距离即可:
\documentclass[conference,compsoc]{IEEEtran}
\usepackage{tikz} % for tikz trees
\usetikzlibrary{arrows, positioning, shadows, shapes, trees}
\usepackage[edges]{forest} % for forest tree
\begin{document}
\begin{forest}
for tree={ % style of tree nodes
draw, semithick,
font = \sffamily,
fill = white, drop shadow,
edge = {draw, semithick},
anchor = north,
grow = south,
% style of tree (edges, distances, direction)
forked edge, % for forked edge
s sep = 8mm, % sibling distance
l sep = 8mm, % level distance
fork sep = 4mm, % distance from parent to branching point
tier/.option=level,
}
[Parent
[Type]
[Title Here
[Two Words Long]
[Two Words Long]
]
]
\end{forest}
\end{document}
结果几乎是一样的:
要使节点具有方形形状则更加棘手。我假设,实际树中的文本长度相同,则可以根据定义的文本宽度计算文本高度:
\documentclass[conference,compsoc]{IEEEtran}
\usepackage{tikz} % for tikz trees
\usetikzlibrary{arrows, positioning, shadows, shapes, trees}
\usepackage[edges]{forest} % for forest tree
\begin{document}
\begin{tikzpicture}[-,thick,%
every node/.style = {rectangle, draw, thick, fill=white,
inner sep=2mm, text width=18mm, minimum height=18mm,
align=flush center,
drop shadow, font=\sffamily},
sibling distance = 30mm,
level distance = 30mm
]
\node {Parent} [edge from parent fork down]
child {node {Type}}
child {node {Title Here}
child {node {Two Words Long}}
child {node {Two Word Long}}
};
\end{tikzpicture}
\end{document}
正如预期的那样,结果很糟糕,并且让我相信你的意思长方形并不是正方形形状(正如我在第一个和第二个例子中假设的那样):