让 tikz 绘制几乎完全完成

让 tikz 绘制几乎完全完成

通过查看此站点上的示例,我已经对 tikz 有了相当的了解,但还没有完全了解。我知道这可能被认为是一个糟糕的问题——但是由于我通过查看类似的问题走到了这一步,这可能是我们在 tikz 方面能做的最好的事情了——但我现在得到的是:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{figure}
\begin{tikzpicture}

\matrix(Person) [matrix of nodes, 
                 matrix/.style={rows={1}{fill=gray}},
                 label=above:Person, 
                 column 1/.style={nodes={text width=1cm, align=center}},
                 column 2/.style={nodes={text width=2cm}},
                 column 3/.style={nodes={text width=2.5cm}},
                 column 4/.style={nodes={text width=2cm}},
                 column 5/.style={nodes={text width=2cm, align=center}}
                  ] {
    id & name       & address       & birth date & occupation.id \\
    1  & Mary Smith & Main street 1 & 1970-04-17 & 1 \\
    2  & John Doe   & Highway 2     & 1972-07-24 & 1 \\ 
    3  & Clara Doe  & Highway 2     & 1995-11-11 & 2 \\};

\matrix(Child) [matrix of nodes, 
                label=above:Child, 
                nodes={text width=2cm, align=center}, 
                right=1 cm of Person] {
    Person.id & Person.id \\ 
    2 & 3                 \\
                          \\};

\matrix(Occupation) [matrix of nodes, 
                     label=above:Occupation, 
                     below=1.25cm of Person,
                     nodes={text width=2.5cm},
                     column 1/.style={nodes={text width=1cm, align=center}}] {
    id & title     & location \\
    1  & secretary & Enterprises ltd \\
    2  & student   & School \\};

\draw[thick](Person-1-1.north west)--(Person-1-5.north east);
\draw(Person-1-1.south west)--(Person-1-5.south east);
\draw[thick](Person-4-1.south west)--(Person-4-5.south east);

\draw[thick](Child-1-1.north west)--(Child-1-2.north east);
\draw(Child-1-1.south west)--(Child-1-2.south east);
\draw[thick](Child-2-1.south west)--(Child-2-2.south east);

\draw[thick](Occupation-1-1.north west)--(Occupation-1-3.north east);
\draw(Occupation-1-1.south west)--(Occupation-1-3.south east);
\draw[thick](Occupation-3-1.south west)--(Occupation-3-3.south east);

\draw[<->] (Child-1-1.north)--++(0,1.5)-|(Person-1-1.north);
\draw[<->] (Child-1-2.north)--++(0,1.75)-|(Person-1-1.north);
\draw[<->] (Occupation-1-1.north)--++(0,1)-|(Person-4-5.south);
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

我将其全部发布出来是因为问题并不多,而且也许您在修复我无法修复的问题的同时,还可以展示其他更好的方法来完成我所做的事情——这样我就可以学习,其他偶然发现此页面的人也可以学习。

现在,我的问题是,我认为什么是表格,什么不是表格还不够清楚。我认为如果箭头实际上没有接触表格线,那么也许会变得更清楚。但到目前为止,我还没有弄明白如何解决这个问题……

答案1

一些建议:

  1. 要从表格中分离箭头,您可以使用shorten >= length 、 和shorten <= length键。

  2. 您还可以更改箭头的线宽和/或颜色。在我的示例代码中,我使用了样式help lines

  3. 如果您仔细查看当前的表格,会发现由于节点内容的高度和深度不同,一些线条并非完全水平;为了保证尺寸相等(将产生完全水平的线条),您可以为矩阵节点设置text depthtext height,或者您也可以使用垂直坐标系来绘制线条。在我的示例中,我选择了第一个选项。

代码:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{figure}
\begin{tikzpicture}

\matrix(Person) [matrix of nodes,
                 nodes={text depth=0.5ex,text height=2ex}, 
                 matrix/.style={rows={1}{fill=gray}},
                 label=above:Person, 
                 column 1/.style={nodes={text width=1cm, align=center}},
                 column 2/.style={nodes={text width=2cm}},
                 column 3/.style={nodes={text width=2.5cm}},
                 column 4/.style={nodes={text width=2cm}},
                 column 5/.style={nodes={text width=2cm, align=center}}
                  ] {
    id & name       & address       & birth date & occupation.id \\
    1  & Mary Smith & Main street 1 & 1970-04-17 & 1 \\
    2  & John Doe   & Highway 2     & 1972-07-24 & 1 \\ 
    3  & Clara Doe  & Highway 2     & 1995-11-11 & 2 \\};

\matrix(Child) [matrix of nodes, 
                label=above:Child, 
                nodes={text width=2cm, align=center}, 
                right=1 cm of Person] {
    Person.id & Person.id \\ 
    2 & 3                 \\
                          \\};

\matrix(Occupation) [matrix of nodes, 
                     label=above:Occupation, 
                     below=1.25cm of Person,
                     nodes={text width=2.5cm},
                     column 1/.style={nodes={text width=1cm, align=center}}] {
    id & title     & location \\
    1  & secretary & Enterprises ltd \\
    2  & student   & School \\};

\draw[thick](Person-1-1.north west)--(Person-1-5.north east);
\draw(Person-1-1.south west)--(Person-1-5.south east);
\draw[thick](Person-4-1.south west)--(Person-4-5.south east);

\draw[thick](Child-1-1.north west)--(Child-1-2.north east);
\draw(Child-1-1.south west)--(Child-1-2.south east);
\draw[thick](Child-2-1.south west)--(Child-2-2.south east);

\draw[thick](Occupation-1-1.north west)--(Occupation-1-3.north east);
\draw(Occupation-1-1.south west)--(Occupation-1-3.south east);
\draw[thick](Occupation-3-1.south west)--(Occupation-3-3.south east);

\begin{scope}[shorten >= 5pt,shorten <= 5pt,help lines,>=latex]
\draw[<->] (Child-1-1.north)--++(0,1.5)-|(Person-1-1.north);
\draw[<->] (Child-1-2.north)--++(0,1.75)-|(Person-1-1.north);
\draw[<->] (Occupation-1-1.north)--++(0,1)-|(Person-4-5.south);
\end{scope}

\end{tikzpicture}
\end{figure}

\end{document}

结果:

在此处输入图片描述

添加rounded corners箭头也可能会改善结果:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{figure}
\begin{tikzpicture}

\matrix(Person) [matrix of nodes,
                 nodes={text depth=0.5ex,text height=2ex}, 
                 matrix/.style={rows={1}{fill=gray}},
                 label=above:Person, 
                 column 1/.style={nodes={text width=1cm, align=center}},
                 column 2/.style={nodes={text width=2cm}},
                 column 3/.style={nodes={text width=2.5cm}},
                 column 4/.style={nodes={text width=2cm}},
                 column 5/.style={nodes={text width=2cm, align=center}}
                  ] {
    id & name       & address       & birth date & occupation.id \\
    1  & Mary Smith & Main street 1 & 1970-04-17 & 1 \\
    2  & John Doe   & Highway 2     & 1972-07-24 & 1 \\ 
    3  & Clara Doe  & Highway 2     & 1995-11-11 & 2 \\};

\matrix(Child) [matrix of nodes, 
                label=above:Child, 
                nodes={text width=2cm, align=center}, 
                right=1 cm of Person] {
    Person.id & Person.id \\ 
    2 & 3                 \\
                          \\};

\matrix(Occupation) [matrix of nodes, 
                     label=above:Occupation, 
                     below=1.25cm of Person,
                     nodes={text width=2.5cm},
                     column 1/.style={nodes={text width=1cm, align=center}}] {
    id & title     & location \\
    1  & secretary & Enterprises ltd \\
    2  & student   & School \\};

\draw[thick](Person-1-1.north west)--(Person-1-5.north east);
\draw(Person-1-1.south west)--(Person-1-5.south east);
\draw[thick](Person-4-1.south west)--(Person-4-5.south east);

\draw[thick](Child-1-1.north west)--(Child-1-2.north east);
\draw(Child-1-1.south west)--(Child-1-2.south east);
\draw[thick](Child-2-1.south west)--(Child-2-2.south east);

\draw[thick](Occupation-1-1.north west)--(Occupation-1-3.north east);
\draw(Occupation-1-1.south west)--(Occupation-1-3.south east);
\draw[thick](Occupation-3-1.south west)--(Occupation-3-3.south east);

\begin{scope}[shorten >= 5pt,shorten <= 5pt,help lines,>=latex,rounded corners]
\draw[<->] (Child-1-1.north)--++(0,1.5)-|(Person-1-1.north);
\draw[<->] (Child-1-2.north)--++(0,1.75)-|(Person-1-1.north);
\draw[<->] (Occupation-1-1.north)--++(0,1)-|(Person-4-5.south);
\end{scope}

\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

相关内容