Tikz:如何在两个节点之间画一条水平线

Tikz:如何在两个节点之间画一条水平线

代码:

\documentclass[preview]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw] (1) {Node};
\node [draw, below = of 1] (1) {Long text};
\end{tikzpicture}
\end{document}

如何在节点之间画虚线,并距边框 1 厘米处添加填充?

在此处输入图片描述

答案1

无需库的选项calc,仅使用垂直坐标系和xshift;主要思想是在两个节点中间放置一个辅助坐标:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\path
  node [draw] (1) {Node}
  node [draw, below = 2cm of 1] (2) {Long text}
  (1.south) -- coordinate (aux) (1.south|-2.north);
\draw[dashed] 
  ([xshift=-1cm]2.north west|-aux) -- ([xshift=1cm]2.north east|-aux);    
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

使用 calc 库,您可以使用点的坐标来画一条线。

\documentclass[preview]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}    %coordinate calculation
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw] (1) {Node};
\node [draw, below = of 1] (2) {Long text};
\draw[dashed] let \p1=(1),\p2=(2),\p3=(2.north west),\p4=(2.north east) in 
    (\x3-1cm,{(\y1+\y2)/2}) -- (\x4+1cm,{(\y1+\y2)/2});
\end{tikzpicture}
\end{document}

\x3 是 \p3(第二个框的左上角)的 x 坐标。{(\y1+\y2)/2} 是两个框之间的中点。括号很重要。

相关内容