如何在同一 x 轴上的节点之间画直线?

如何在同一 x 轴上的节点之间画直线?

我想在两列中从最左到最右在节点之间(在同一 x 轴上)绘制直线。我试图给出一个中间有两个节点的示例,但可能不止两个。

我的代码:

\documentclass[5p,times]{elsarticle}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
    chains,
    positioning,
    shapes.geometric
}
\begin{document}
\section{Example}
\noindent
\begin{tikzpicture}[
        node distance = 4mm and 8mm,
        arr/.style = {-Stealth, semithick},
        C/.style = {circle, draw, font=\footnotesize},
        N/.style = {draw, very thick,
                font=\small, align=left,
                inner sep=5pt}
    ]
    \draw[help lines] (0,0) grid (8,2);
    %
    \node (n1) [N] at (0,1) {\$ dummy node \\
        \hphantom{\$ } example};

    \node (n2) [N] at (6,1) {\$ dummy node \\
        \hphantom{\$ } example};

    \draw[very thick] (n1.east)-- (n2.west);
\end{tikzpicture}
\newline
\lipsum[-1]
\end{document}

输出:

在此处输入图片描述

在这里我无法找到绘制线的最左边和最右边的位置。


想要的输出:

在此处输入图片描述

答案1

我不完全确定你想要实现什么,但也许这可以帮助你:

\documentclass[5p,times]{elsarticle}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
    chains,
    positioning,
    shapes.geometric
}
\begin{document}
\section{Example}
\noindent  % <-- this is important because (0,0) needs to sit at the start of the column
\begin{tikzpicture}[
        clip,  % <-- prevent line ends to overlap box
        node distance = 4mm and 8mm,
        arr/.style = {-Stealth, semithick},
        C/.style = {circle, draw, font=\footnotesize},
        N/.style = {draw, very thick,
                font=\small, align=left,
                inner sep=5pt}
    ]
    \draw[help lines] (0,0) grid (8,2);
    %
    \node (n1) [N] at (3,1) {\$ dummy node \\
        \hphantom{\$ } example};

    \node (n2) [N] at (6,1) {\$ dummy node \\
        \hphantom{\$ } example};

    \draw[very thick] 
        (0,0 |- n1.west) -- (n1.west)
        (n1.east) -- (n2.west)
        (n2.east) -- (n2.east -| \columnwidth,0);
\end{tikzpicture}
\newline
\lipsum[1-1]
\end{document}

在此处输入图片描述

(0,0 |- n1.west)语法含义为:x坐标为(0,0),y坐标为 的点n1.west

答案2

@Jasper Habicht 的回答有一点变化(+1):

\documentclass[5p,times]{elsarticle}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning
                }
\begin{document}
\section{Example}
\noindent  % <-- this is important because (0,0) needs to sit at the start of the column
    \begin{tikzpicture}[
node distance = 4mm and 8mm,
   arr/.style = {-Stealth, semithick},
     C/.style = {circle, draw, font=\footnotesize},
     N/.style = {draw, very thick,
                 font=\small, align=left,
                 inner sep=5pt}
                        ]
\draw[help lines] (0,-1) grid (\columnwidth,1);
    %
\node (n1) [N] at (3,0)     {\$ dummy node \\
                             \hphantom{\$ } example};
\node (n2) [N, right=of n1] {\$ dummy node \\
                             \hphantom{\$ } example};
\draw[very thick]
        (0,0) -- (n1) -- (n2) -- (\columnwidth,0);
\end{tikzpicture}
\newline
\lipsum[1-1]
\end{document}

在此处输入图片描述

相关内容