我正在尝试绘制 ERD 图。我从这里的几篇文章中回收了代码,并添加了自己的内容,最终得到:
\documentclass[border=0.5in]{文章} \usepackage{tikz} \usetikzlibrary{shapes.multipart} \usetikzlibrary{矩阵} \usetikzlibrary{定位} \usetikzlibrary{计算} \usepackage{scalefnt} \usepackage{几何} \usepackage{pdflscape} \制作字母 \pgfarrowsdeclare{oone}{oone} { \pgfarrowsleftextend{+0pt}% \pgfarrowsrightextend{+0pt}% } { \pgfutil@tempdima=0.25pt% \advance\pgfutil@tempdima by0.25\pgflinewidth% \pgfsetdash{}{+0pt}% \pgfsetmiterjoin% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-12\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-12\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfusepathqstroke% } \pgfarrowsdeclare{一}{一} { \pgfarrowsleftextend{+0pt}% \pgfarrowsrightextend{+0pt}% } { \pgfutil@tempdima=0.25pt% \advance\pgfutil@tempdima by0.25\pgflinewidth% \pgfsetdash{}{+0pt}% \pgfsetmiterjoin% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfusepathqstroke% } \pgfarrowsdeclare{许多}{许多} { \pgfarrowsleftextend{+0pt}% \pgfarrowsrightextend{+0pt}% } { \pgfutil@tempdima=0.25pt% \advance\pgfutil@tempdima by0.25\pgflinewidth% \pgfsetdash{}{+0pt}% \pgfsetmiterjoin% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-6\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-14\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-6\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-14\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfusepathqstroke% } \pgfarrowsdeclare{奥马尼}{奥马尼} { \pgfarrowsleftextend{+0pt}% \pgfarrowsrightextend{+0pt}% } { \pgfutil@tempdima=0.25pt% \advance\pgfutil@tempdima by0.25\pgflinewidth% \pgfsetdash{}{+0pt}% \pgfsetmiterjoin% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{0\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-8\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-6\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-14\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-6\pgfutil@tempdima}{0\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-14\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfpathmoveto{\pgfqpoint{-18\pgfutil@tempdima}{+8\pgfutil@tempdima}}% \pgfpathlineto{\pgfqpoint{-18\pgfutil@tempdima}{-8\pgfutil@tempdima}}% \pgfusepathqstroke% } \开始{文档} \tikz设置{ 实体/.代码={ \tikz设置{ 圆角, 名称=#1, 内分离度=2pt, 每个实体/.try, 标签=以下:#1 }% \def\实体名称{#1}% }, 实体锚点/.style={matrix anchor=#1}, 每个实体/.style={ 画, }, 每个属性/.style={ 内部 xsep=0.20cm,内部 ysep=0.075cm,锚点=west,文本宽度=1in }, 之字形至/.style={ 到路径 = {(\tikztostart) -| ($(\tikztostart)!#1!(\tikztotarget)$) |- (\tikztotarget)} }, 之字形到/.default=0.5, 多对一/.style={ 多对一,之字形 }, 一个到 omany/.style={ 一人一机,曲折前行 } } \def\property#1{\node[name=\entityname-#1,每个属性/.try]{#1};} \def\properties{\begingroup\catcode`\_=11\relax\processproperties} \def\processproperties#1{\endgroup% \def\属性代码{}% \foreach \p 在 {#1}{% \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\propertycode% \expandafter\expandafter\expandafter{\expandafter\propertycode\expandafter\property\expandafter{\p}\\}% }% \属性代码% } \开始{景观} \scalefont{0.5} \begin{tikzpicture}[每个节点/.style={font=\ttfamily},节点距离=0.5in] \matrix [实体=租金] { \特性{ ID : int(11), 开始日期 :日期时间, 结束日期 :日期时间, 取货日期:日期时间, 返回日期:日期时间, PickupMiles : int(11), 返回里程:int(11), 缺失:bool, 车辆 VIN : varchar(255), 员工用户名:varchar(255), 客户ID : int(11), 出发车库: int(11), 到达车库: int(11), 优惠编号 : int(11), Kasko :布尔, TPI :布尔值, CDW :布尔值, TW :布尔值, DER :布尔值, 限制年龄: bool, 额外里程单位: int(11) } }; \matrix [实体=员工,以上=租金] { \特性{ 用户名:varchar(255), 密码:varchar(255), 名称:varchar(255), 姓氏:varchar(255) } }; \matrix [实体=车辆,左侧=租金] { \特性{ VIN : varchar(255), 车牌:varchar(255), 座位数: int(11), 门 :int(11), 4WD:布尔, 年份 : int(11), 柴油:布尔, 排量:双, 消费:双倍, LPG :布尔值, ManualShift : 位, 自治:双重, 鉴别器:int(11), 模型ID : int(11), HousedInGarage: int(11), 退休:bool, FullHybrid : 布尔值, 里程:int(11) } }; \draw[one to omany] (车辆) 到 (租金); \draw[one-omany] (员工) 至 (租金); \结束{tikzpicture} \end{景观} \结束{文档}
现在,我想要标签里面节点,可能带有分隔符或类似的东西。ATM 我得到:
但我更希望:
有人可以建议一种方法来做到这一点吗?
非常感谢。
答案1
我想这是实现此目的的一种方法:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\tikzset{
entity/.code={
\tikzset{
rounded corners,
name=#1,
inner sep=2pt,
every entity/.try,
}%
\def\entityname{#1}%
},
entity anchor/.style={matrix anchor=#1},
every entity/.style={
draw,
},
every property/.style={
inner xsep=0.20cm, inner ysep=0.075cm, anchor=west, text width=1.75in
}
}
\def\property#1{\node[name=\entityname-#1, every property/.try]{\propertysplit#1;};}
\def\properties{\begingroup\catcode`\_=11\relax\processproperties}
\def\processproperties#1{\endgroup%
\gdef\propertycode{}%
\foreach \p in {#1}{%
\expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\propertycode%
\expandafter\expandafter\expandafter{\expandafter\propertycode\expandafter\property\expandafter{\p}\\}%
}%
\propertycode%
}
\def\propertysplit#1:#2;{#1:\hfill#2}
\def\entitynamenode{%
\node[every entity name/.try] (\entityname-name) {\entityname};
\draw (\entityname-name.south west) -- (\entityname-name.south east);
\\[1ex]
}
\tikzset{
every entity name/.style={every property/.try, align=center}
}
\begin{tikzpicture}[every node/.style={font=\ttfamily}, node distance=0.5in]
\matrix [entity=Employee] {
\entitynamenode
\properties{
Username : varchar(255),
Password : varchar(255),
Name : varchar(255),
LastName : varchar(255)
}
};
\end{tikzpicture}
\end{document}
答案2
最后我采用了这个小技巧,虽然它并不完美,也不利于节省空间,但效果还算可以:
实体/.代码={ \tikz设置{ 圆角, 名称=#1, 内分离度=10pt, 每个实体/.try, 标签={[xshift=0cm, font=\bfseries, yshift=-10.5pt]#1} }% \def\实体名称{#1}% }
我正在改变标签里面盒子,我正在添加足够的填充物来容纳它。
但是,除了有点丑之外 - 它仍然没有给我分隔符。
真正知道自己在做什么的人肯定可以改进这一点。