tikz 节点放置不正确:节点之间存在间隙

tikz 节点放置不正确:节点之间存在间隙

我正在使用以下代码来生成如下所示的图像。 在此处输入图片描述

现在我将节点距离设置为 0,并在节点中将 draw=none 设置为无,以删除边框。 comeback, comein节点大小为 2.5 厘米,而handle为 1.75 厘米,none大小为 0.75 厘米。

现在,由于块的大小,我期望最后一个 none 块和 comeback 块垂直对齐。然而事实并非如此。我看到节点的放置之间存在一个小间隙,即使node distance=0cm

如何消除这个差距?

谢谢 !

\documentclass{article}
\usepackage{tikz}
 \usetikzlibrary{positioning}
   \begin{tikzpicture}[node distance=0cm]
\definecolor{color1}{RGB}{51, 122, 153}
\definecolor{color2}{RGB}{102, 153, 102}
\definecolor{color3}{RGB}{150, 150, 150}
\definecolor{color4}{RGB}{204, 152, 0}

\tikzstyle{comein}=[minimum height=1cm,minimum width=2.5cm,draw=none,text centered,fill=color1,anchor=west]
\tikzstyle{handle}=[minimum height=1cm,minimum width=1.75cm,draw=none,text centered,fill=color2,anchor=west]
\tikzstyle{comeback}=[minimum height=1cm,minimum width=2.5cm,draw=none,text centered,fill=color4,anchor=west]
\tikzstyle{noneStyle}=[minimum height=1cm,minimum width=0.75cm,draw=none,text centered,fill=color3,anchor=west]

\node at (17.5,3.1) {\large s=1};
\draw[dashed] (18.75,-1.15) -- (18.75,3.25);
\draw[dashed] (16.25,-1.15) -- (16.25,3.25);

\node at (11,-1.5) {\LARGE Time};
\draw[->] (-1,-1) -- (22,-1);
\draw (-0.75,-1.25) -- (-0.75,3.5);

\node at (-2,2.25) {Input};
\node at (-2,1) {HANDLE};
\node at (-2,-0.15) {Output};

\begin{scope}[yshift=-0.15cm]
\node[comein]  (P0) at (-0.5,0) {$comein_0$};
\node[comein]  (P1) at (P0.east) {$comein_1$};
\node[comein]  (P2) at (P1.east) {$comein_2$};
\node[comein]  (P3) at (P2.east) {$comein_3$};
\node[comein]  (P4) at (P3.east) {$comein_4$};
\node[comein]  (P5) at (P4.east) {$comein_5$};
\end{scope}

\begin{scope}
\node[noneStyle,minimum width=2.5cm]  (I0) at (-0.5,1) {$none$};
\node[handle]  (C0) at (I0.east) {$handle_0$};
\node[noneStyle]  (I1) at (C0.east) {};
\node[handle]  (C1) at (I1.east) {$handle_1$};
\node[noneStyle]  (I2) at (C1.east) {};
\node[handle]  (C2) at  (I2.east) {$handle_2$};
\node[noneStyle]  (I3) at (C2.east) {};
\node[handle]  (C3) at  (I3.east) {$handle_3$};
\node[noneStyle]  (I4) at (C3.east) {};
\node[handle]  (C4) at  (I4.east) {$handle_4$};
\node[noneStyle]  (I5) at (C4.east) {};
\node[handle]  (C5) at  (I5.east) {$handle_5$};
\node[noneStyle,minimum width=2.5cm]  (I1) at (C5.east) {$none$};
\end{scope}

\begin{scope}[yshift=0.15cm]
\node[comeback]  (W0) at (3.75,2) {$comeback_0$};
\node[comeback]  (W1) at (W0.east) {$comeback_1$};
\node[comeback]  (W2) at (W1.east) {$comeback_2$};
\node[comeback]  (W3) at (W2.east) {$comeback_3$};
\node[comeback]  (W4) at (W3.east) {$comeback_4$};
\node[comeback]  (W5) at (W4.east) {$comeback_5$};
\end{scope}

\coordinate (a) at (-0.5,1.75);
\coordinate (b) at (3.0,1.75);
\draw[<->, black!20!white, line width=2pt]   (-0.5,1.75) to node[black,above]{INIT} (2.0,1.75) ;
\draw[<->, black!20!white, line width=2pt]   (16.25,-0.5) to node[black,above]{FINAL} (18.75,-0.5) ;

\end{tikzpicture}
\end{document}

谢谢,

答案1

您需要杀死outer sep(将其设置为0pt):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\definecolor{color1}{RGB}{51, 122, 153}
\definecolor{color2}{RGB}{102, 153, 102}
\definecolor{color3}{RGB}{150, 150, 150}
\definecolor{color4}{RGB}{204, 152, 0}

\begin{document}

\begin{tikzpicture}[
node distance=0cm,
comein/.style={
  minimum height=1cm,
  minimum width=2.5cm,
  draw=none,
  text centered,
  fill=color1,
  anchor=west,
  outer sep=0pt
  },
handle/.style={
  minimum height=1cm,
  minimum width=1.75cm,
  draw=none,
  text centered,
  fill=color2,
  anchor=west,
  outer sep=0pt
  },
comeback/.style={
  minimum height=1cm,
  minimum width=2.5cm,
  draw=none,
  text centered,
  fill=color4,
  anchor=west,
  outer sep=0pt
  },
noneStyle/.style={
  minimum height=1cm,
  minimum width=0.75cm,
  draw=none,
  text centered,
  fill=color3,
  anchor=west,
  outer sep=0pt
  }
]
\node at (17.5,3.1) {\large s=1};
\draw[dashed] (18.75,-1.15) -- (18.75,3.25);
\draw[dashed] (16.25,-1.15) -- (16.25,3.25);

\node at (11,-1.5) {\LARGE Time};
\draw[->] (-1,-1) -- (22,-1);
\draw (-0.75,-1.25) -- (-0.75,3.5);

\node at (-2,2.25) {Input};
\node at (-2,1) {HANDLE};
\node at (-2,-0.15) {Output};

\begin{scope}[yshift=-0.15cm]
\node[comein]  (P0) at (-0.5,0) {$comein_0$};
\node[comein]  (P1) at (P0.east) {$comein_1$};
\node[comein]  (P2) at (P1.east) {$comein_2$};
\node[comein]  (P3) at (P2.east) {$comein_3$};
\node[comein]  (P4) at (P3.east) {$comein_4$};
\node[comein]  (P5) at (P4.east) {$comein_5$};
\end{scope}

\begin{scope}
\node[noneStyle,minimum width=2.5cm]  (I0) at (-0.5,1) {$none$};
\node[handle]  (C0) at (I0.east) {$handle_0$};
\node[noneStyle]  (I1) at (C0.east) {};
\node[handle]  (C1) at (I1.east) {$handle_1$};
\node[noneStyle]  (I2) at (C1.east) {};
\node[handle]  (C2) at  (I2.east) {$handle_2$};
\node[noneStyle]  (I3) at (C2.east) {};
\node[handle]  (C3) at  (I3.east) {$handle_3$};
\node[noneStyle]  (I4) at (C3.east) {};
\node[handle]  (C4) at  (I4.east) {$handle_4$};
\node[noneStyle]  (I5) at (C4.east) {};
\node[handle]  (C5) at  (I5.east) {$handle_5$};
\node[noneStyle,minimum width=2.5cm]  (I1) at (C5.east) {$none$};
\end{scope}

\begin{scope}[yshift=0.15cm]
\node[comeback]  (W0) at (3.75,2) {$comeback_0$};
\node[comeback]  (W1) at (W0.east) {$comeback_1$};
\node[comeback]  (W2) at (W1.east) {$comeback_2$};
\node[comeback]  (W3) at (W2.east) {$comeback_3$};
\node[comeback]  (W4) at (W3.east) {$comeback_4$};
\node[comeback]  (W5) at (W4.east) {$comeback_5$};
\end{scope}

\coordinate (a) at (-0.5,1.75);
\coordinate (b) at (3.0,1.75);
\draw[<->, black!20!white, line width=2pt]   (-0.5,1.75) to node[black,above]{INIT} (2.0,1.75) ;
\draw[<->, black!20!white, line width=2pt]   (16.25,-0.5) to node[black,above]{FINAL} (18.75,-0.5) ;

\end{tikzpicture}
\end{document}

在此处输入图片描述

顺便说一句,也许您可​​能有兴趣改变\tikzstyle\tikzset正如我在回答中所做的那样),也许使用链(带有一些循环)会简化您的代码,如以下示例所示:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,chains}

\definecolor{color1}{RGB}{51, 122, 153}
\definecolor{color2}{RGB}{102, 153, 102}
\definecolor{color3}{RGB}{150, 150, 150}
\definecolor{color4}{RGB}{204, 152, 0}

\begin{document}

\begin{tikzpicture}[
node distance=0cm,
comein/.style={
  minimum height=1cm,
  minimum width=2.5cm,
  draw=none,
  text centered,
  fill=color1,
  anchor=west,
  outer sep=0pt
  },
handle/.style={
  minimum height=1cm,
  minimum width=1.75cm,
  draw=none,
  text centered,
  fill=color2,
  anchor=west,
  outer sep=0pt
  },
comeback/.style={
  minimum height=1cm,
  minimum width=2.5cm,
  draw=none,
  text centered,
  fill=color4,
  anchor=west,
  outer sep=0pt
  },
noneStyle/.style={
  minimum height=1cm,
  minimum width=0.75cm,
  draw=none,
  text centered,
  fill=color3,
  anchor=west,
  outer sep=0pt
  }
]
\node at (17.5,3.1) {\large s=1};
\draw[dashed] (18.75,-1.15) -- (18.75,3.25);
\draw[dashed] (16.25,-1.15) -- (16.25,3.25);

\node at (11,-1.5) {\LARGE Time};
\draw[->] (-1,-1) -- (22,-1);
\draw (-0.75,-1.25) -- (-0.75,3.5);

\node at (-2,2.25) {Input};
\node at (-2,1) {HANDLE};
\node at (-2,-0.15) {Output};

\begin{scope}[yshift=-0.15cm,start chain,every node/.style={comein,on chain}]
\node  (P0) at (-0.5,0) {$comein_0$};
\foreach \Valor in {1,...,5} 
  \node  (P\Valor) {$comein_\Valor$};
\end{scope}

\begin{scope}[start chain,every node/.style={on chain}]
\node[noneStyle,minimum width=2.5cm]  (I0) at (-0.5,1) {$none$};
\node[handle]  (C0) at (I0.east) {$handle_0$};
\foreach \Valor in {1,...,5}
{
  \node[noneStyle]  (I\Valor) {};
  \node[handle]  (C\Valor) {$handle_\Valor$};
}
\node[noneStyle,minimum width=2.5cm]  (I1) at (C5.east) {$none$};
\end{scope}

\begin{scope}[yshift=0.15cm,start chain,every node/.style={comeback,on chain}]
\node  (W0) at (3.75,2) {$comeback_0$};
\foreach \Valor in {1,...,5}
  \node  (W\Valor) {$comeback_\Valor$};
\end{scope}

\coordinate (a) at (-0.5,1.75);
\coordinate (b) at (3.0,1.75);
\draw[<->, black!20!white, line width=2pt]   (-0.5,1.75) to node[black,above]{INIT} (2.0,1.75) ;
\draw[<->, black!20!white, line width=2pt]   (16.25,-0.5) to node[black,above]{FINAL} (18.75,-0.5) ;
\end{tikzpicture}

\end{document}

相关内容