如何在 Tikz 中的两行节点之间添加线以及如何垂直对齐节点?

如何在 Tikz 中的两行节点之间添加线以及如何垂直对齐节点?

我有两个问题:

  1. 如何在 Tikz 中的两行节点之间居中添加一条水平线?
  2. 如何更改节点的垂直位置以便更好地与其他节点对齐

我的意思可以通过一张图片来更清楚地说明:

在此处输入图片描述

蓝线表示我想添加的线(当然应该是直线)。注意两个红色圆圈。由于下标,箭头不会像 那样“击中”它们的中心XX_1, X_2, X_3那么我怎样才能将它们稍微向下移动一点呢?

这是一个最小的工作示例。

\documentclass[a4paper,11pt]{article}

\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}

\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center} ]{
\node [] {$X_1$}; & \node [] {$X_2$}; & \node [] {$X_3$}; & \node [] {$\ldots$}; & \node [] {$X_{\frac{n}{8}-1}$}; & \node [] {$X_{\frac{n}{8}}$}; \\
\node [] {$Y_1$}; & \node [] {$Y_2$}; & \node [] {$Y_3$}; & \node [] {$\ldots$}; & \node [] {$Y_{\frac{n}{8}-1}$}; & \node [] {$Y_{\frac{n}{8}}$}; \\
};

\path [>=latex,->] (m-1-1) edge (m-2-1);
\path [>=latex,->] (m-1-1) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-2) edge (m-2-2);
\path [>=latex,->] (m-1-2) edge (m-2-3);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-4) edge (m-2-5);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-6);
\path [>=latex,->] (m-1-6) edge (m-2-6);

\end{tikzpicture}


\end{document}

答案1

您可以使用选项简化语法matrix of math nodes

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center,text depth=0pt} ]{
X_1 & X_2 & X_3 &\ldots & X_{\frac{n}{8}-1} & X_{\frac{n}{8}} \\
Y_1 & Y_2 & Y_3 &\ldots & Y_{\frac{n}{8}-1} & Y_{\frac{n}{8}} \\
};

\path [>=latex,->] (m-1-1) edge (m-2-1);
\path [>=latex,->] (m-1-1) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-2) edge (m-2-2);
\path [>=latex,->] (m-1-2) edge (m-2-3);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-4) edge (m-2-5);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-6);
\path [>=latex,->] (m-1-6) edge (m-2-6);
\draw[shorten <=-1cm,shorten >=-1cm] ($(m-1-1)!0.5!(m-2-1)$)--($(m-1-6)!0.5!(m-2-6)$);
\end{tikzpicture}

\end{document}

答案2

every path您可以使用和使代码更加干净every node

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center,text depth=0pt} ]{
X_1 & X_2 & X_3 &\ldots & X_{\frac{n}{8}-1} & X_{\frac{n}{8}} \\
Y_1 & Y_2 & Y_3 &\ldots & Y_{\frac{n}{8}-1} & Y_{\frac{n}{8}} \\
};

\tikzstyle{every path}=[>=latex,->]
\tikzstyle{every node}=[above]
\path (m-1-1) edge (m-2-1)
  (m-1-1) edge node {$\ast$} (m-1-2)
  (m-1-2) edge (m-2-2)
  (m-1-2) edge (m-2-3)
  (m-1-3) edge node {$\ast$} (m-1-2)
  (m-1-3) edge node {$\ast$} (m-1-4)
  (m-1-4) edge (m-2-5)
  (m-1-5) edge node {$\ast$} (m-1-4)
  (m-1-5) edge node {$\ast$} (m-1-6)
  (m-1-6) edge (m-2-6);
\draw[shorten <=-1cm,shorten >=-1cm,>=] ($(m-1-1)!0.5!(m-2-1)$)--($(m-1-6)!0.5!(m-2-6)$);
\end{tikzpicture}

\end{document}

相关内容