使节点大小依赖于其他节点

使节点大小依赖于其他节点

我使用 Tikz 绘制了下图:

在此处输入图片描述

以下是代码:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[rounded corners=2pt,inner sep=5pt,node distance=.8cm]
\node [draw](chapter1) {\textit{Chapter 1}};
\node [draw,right=of chapter1] (paper1) {\textit{Paper 1}};

\node [draw, below=of chapter1](chapter2) {\textit{Chapter 2}};
\node [draw,right=of chapter2] (paper2) {\textit{Paper 2}};

\node [draw, below=of chapter2](chapter3) {\textit{Chapter 3}};
\node [draw,right=of chapter3] (paper3) {\textit{Paper 3}};
\node [draw,below=of paper3] (paper4) {\textit{Paper 4}};

\draw [-] (chapter1) -- (paper1);
\draw [-] (chapter2) -- (paper2);
\draw [-] (chapter3) -- (paper3);
\draw [-] (chapter3) -- (paper4);

\end{tikzpicture}
\end{document}

我实际上想要的是使 chapter3 节点的高度足够大,以便顶部与 paper3 对齐(现在是这样),底部与 paper 4 对齐(现在还不是这种情况)。

我怎样才能实现这个目标?

答案1

如果您可以先放置paper节点,那么您可以使用let语法计算从顶部paper3到底部的距离paper4,并将其用作节点高度。还必须减去线的宽度。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}[rounded corners=2pt,inner sep=5pt,node distance=.8cm]
\node [draw](chapter1) {\textit{Chapter 1}};
\node [draw,right=of chapter1] (paper1) {\textit{Paper 1}};

\node [draw, below=of chapter1](chapter2) {\textit{Chapter 2}};
\node [draw,right=of chapter2] (paper2) {\textit{Paper 2}};

\node [draw,below=of paper2] (paper3) {\textit{Paper 3}};
\node [draw,below=of paper3] (paper4) {\textit{Paper 4}};

\path let
  \p1=(paper2.north), \p2=(paper3.south), \n1 = {\y1-\y2-\pgflinewidth}
  in
  node [draw, below=of chapter2,minimum height=\n1](chapter3) {\textit{Chapter 3}};

\draw [-] (chapter1) -- (paper1);
\draw [-] (chapter2) -- (paper2);
\draw [-] (paper3 -| chapter3.east) -- (paper3);
\draw [-] (paper4 -| chapter3.east) -- (paper4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

一种选择是绘制节点paper3paper4之前chapter3并使用两者在库帮助下设置chapter3节点大小fit

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning, fit}

\begin{document}

\begin{tikzpicture}[rounded corners=2pt,inner sep=5pt,node distance=.8cm]
\node [draw](chapter1) {\textit{Chapter 1}};
\node [draw,right=of chapter1] (paper1) {\textit{Paper 1}};

\node [draw, below=of chapter1](chapter2) {\textit{Chapter 2}};
\node [draw,right=of chapter2] (paper2) {\textit{Paper 2}};

\node [draw,below=of paper2] (paper3) {\textit{Paper 3}};
\node [draw,below=of paper3] (paper4) {\textit{Paper 4}};

\node [draw, inner sep=0pt, fit=(paper3.north-|chapter2.west) (paper4.south-|chapter2.east), label=center:\textit{Chapter 3}, below=of chapter2](chapter3) {};

\draw [-] (chapter1) -- (paper1);
\draw [-] (chapter2) -- (paper2);
\draw [-] (chapter3.east|-paper3) -- (paper3);
\draw [-] (chapter3.east|-paper4) -- (paper4);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

以下是暴力破解方法:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}[font=\itshape,rounded corners=2pt,inner sep=5pt,node distance=.8cm]
\node [draw](chapter1) {Chapter 1};
\node [draw,right=of chapter1] (paper1) {Paper 1};

\node [draw, below=of chapter1](chapter2) {Chapter 2};
\node [draw,right=of chapter2] (paper2) {Paper 2};


\node [draw,below=of paper2] (paper3) {Paper 3};
\node [draw,below=of paper3] (paper4) {Paper 4};

%% define a coordinate between paper 3 and paper 4.
\coordinate (p34) at ($(paper3)!0.5!(paper4)$);

% put the node chapter3 at this mid point, don't draw the border.
\node (chapter3) at (chapter2|- p34) {Chapter 3};

% draw the border.
\draw (chapter2.west|-paper4.south) rectangle (chapter2.east|-paper3.north);

\draw [-] (chapter1) -- (paper1);
\draw [-] (chapter2) -- (paper2);
\draw [-,rounded corners=0pt] (chapter3.east) |- (paper3);
\draw [-,rounded corners=0pt] (chapter3.east) |- (paper4);

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容