我正在尝试生成一个具有自动定位节点功能的 ER 图。此代码的输出效果不错,但我必须手动管理节点的位置。我尝试替换“left of = person”,但这样做会导致节点重叠。我如何告诉 tikz 自动将节点定位在“Person”附近的某个位置?
\documentclass[a4paper ,11pt,x11names]{article}
\usepackage{tikz}
\usetikzlibrary{er}
\tikzset{multi attribute/.style={attribute,double distance=1.5pt}}
\tikzset{derived attribute/.style={attribute ,dashed}}
\tikzset{total/.style={double distance=1.5pt}}
\tikzset{every entity/.style={draw=orange, fill=orange!20}}
\tikzset{every attribute/.style={draw=MediumPurple1, fill=MediumPurple1!20}}
\tikzset{every relationship/.style={draw=Chartreuse2, fill=Chartreuse2!20}}
\newcommand{\key}[1]{\underline{#1}}
\begin{document}
\begin{tikzpicture}[node distance=7em]
\node[entity] (person) {Person};
\node[attribute] (pid) [left of = person] {\key{ID}} edge (person);
\node[attribute] (name) [above left of=person] {Name} edge (person);
\end{tikzpicture}
\end{document}
输出
答案1
在所有情况下,这不会完全自动,但该库比已弃用的,语法positioning
做得更好。例如:left of
above left of
\documentclass[tikz,multi,border=10pt,svgnames,dvipsnames,x11names]{standalone}
\usetikzlibrary{er,positioning}
\tikzset{%
multi attribute/.style={attribute,double distance=1.5pt},
derived attribute/.style={attribute ,dashed},
total/.style={double distance=1.5pt},
every entity/.style={draw=orange, fill=orange!20},
every attribute/.style={draw=MediumPurple1, fill=MediumPurple1!20},
every relationship/.style={draw=Chartreuse2, fill=Chartreuse2!20},
}
\newcommand{\key}[1]{\underline{#1}}
\begin{document}
\begin{tikzpicture}
\node[entity] (person) {Person};
\node[attribute] (pid) [left = of person] {\key{ID}} edge (person);
\node[attribute] (name) [above left = of person] {Name} edge (person);
\end{tikzpicture}
\end{document}
生产
或者,如果您的图表采用树的形式,则forest
可以自动布局树并适当定位节点。例如:
\documentclass[tikz,multi,border=10pt,svgnames,dvipsnames,x11names]{standalone}
\usepackage{forest}
\usetikzlibrary{er}
\tikzset{%
multi attribute/.style={attribute,double distance=1.5pt},
derived attribute/.style={attribute ,dashed},
total/.style={double distance=1.5pt},
every entity/.style={draw=orange, fill=orange!20},
every attribute/.style={draw=MediumPurple1, fill=MediumPurple1!20},
every relationship/.style={draw=Chartreuse2, fill=Chartreuse2!20},
}
\newcommand{\key}[1]{\underline{#1}}
\begin{document}
\begin{forest}
for tree={%
grow'=180
}
[Person, name=person, entity
[\key{ID}, name=pid, attribute, calign with current edge]
[Name, name=name, attribute]
]
\end{forest}
\end{document}
另一个选择是chains
库。对于真正的自动定位,您可以查看graph
TikZ 提供的基于 Lua 的绘图算法。