如何减少节点之间的空间?

如何减少节点之间的空间?

我想让下vector图中的节点相互接触。但无论我做什么text widthnode distance节点之间的距离都保持不变。

\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations.pathreplacing}

\tikzstyle{cell} = [rectangle, draw, text width=1.3cm ]
\tikzstyle{capx} = [rectangle, draw, text width=1.3cm, color=black!40  ]

\begin{tikzpicture}[
    every node/.style={text centered,  minimum height=1.5em, minimum width=1.5cm,node distance=0pt},
    background rectangle/.style={fill=black!10},show background rectangle,
    ]

   \node at (10,0) [capx] (n5) {leer};
   \node at (12,0) [capx] (n6) {leer};
   \node at (14,0) [capx] (n7) {leer};

   \node at (0,0) [cell] (n0) {wert};    
   \node at (2,0) [cell] (n1) {wert};
   \node at (4,0) [cell] (n2) {wert};
   \node at (6,0) [cell] (n3) {wert};
   \node at (8,0) [cell] (n4) {wert};

\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt]
  (-0.6,0.5) -- (8.8,0.5) node [black,midway,xshift=-0.5cm,yshift=18pt] 
  {\hskip6ex size()};

\draw [decorate,decoration={brace,amplitude=10pt,mirror},xshift=-4pt,yshift=0pt,color=black!40]
  (-0.6,-0.5) -- (14.8,-0.5) node [black,midway,xshift=-0.5cm,yshift=-18pt,color=black!40] 
  {\hskip6ex capacity()};

\end{tikzpicture}

坏向量

答案1

使用该positioning库并放置您的节点right= of <name>

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{
  cell/.style = {rectangle, draw, text width=1.3cm,outer sep=0pt},
  capx/.style = {rectangle, draw, text width=1.3cm, color=black!40,outer sep=0pt}
}

\begin{document}

\begin{tikzpicture}[
    every node/.style={align=center, minimum height=1.5em, minimum width=1.5cm,node distance=0pt},
    background rectangle/.style={fill=black!10},show background rectangle,
    ]


   \node at (0,0) [cell] (n0) {wert};    
   \node[right=of n0,cell] (n1) {wert};
   \node[right=of n1,cell] (n2) {wert};
   \node[right=of n2,cell] (n3) {wert};
   \node[right=of n3,cell] (n4) {wert};

   \node[right=of n4,capx] (n5) {leer};
   \node[right=of n5,capx] (n6) {leer};
   \node[right=of n6,capx] (n7) {leer};

\draw[
   decorate,
  decoration={brace,amplitude=10pt}
]
  ([yshift=4pt]n0.north west) -- 
  node [black,yshift=18pt] {size()}
  ([yshift=4pt]n4.north east) ;

\draw[
  decorate,
  decoration={brace,amplitude=10pt,mirror},
  color=black!40
]
  ([yshift=-4pt]n0.south west) -- 
    node [black,yshift=-18pt,color=black!40]  {capacity()}
  ([yshift=-4pt]n7.south east) ;

\end{tikzpicture}

\end{document}

在此处输入图片描述

一些评论:

  1. \tikzstyle已被弃用;我改为\tikzset

  2. 使用现有节点的锚点来放置支架。

  3. 设置outer sep0pt这样节点的线就可以很好地重叠。

  4. 请注意,我还改变了支架节点的位置。

答案2

这是不使用positioning库的另一种方法。我们将nodes 放置在 处previous node.east,并将锚点设置为 ,west使它们相互接触。

\node[anchor=west,cell] at (n1.east) (n2) {wert};

应该outer sep为零,如 Gonzalo 的回答中所述。您可以保留密钥node distance=1.5cm

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{
  cell/.style = {rectangle, draw, text width=1.3cm,outer sep=0pt},
  capx/.style = {rectangle, draw, text width=1.3cm, color=black!40,outer sep=0pt}
}

\begin{document}

\begin{tikzpicture}[
    every node/.style={align=center, minimum height=1.5em, minimum width=1.5cm,node distance=1.5cm},
    background rectangle/.style={fill=black!10},show background rectangle,
    ]


   \node at (0,0) [cell] (n0) {wert};
   \node[cell,anchor=west] at (n0.east) (n1) {wert};
   \node[anchor=west,cell] at (n1.east) (n2) {wert};
   \node[anchor=west,cell] at (n2.east) (n3) {wert};
   \node[anchor=west,cell] at (n3.east) (n4) {wert};

   \node[anchor=west,capx] at (n4.east) (n5) {leer};
   \node[anchor=west,capx] at (n5.east) (n6) {leer};
   \node[anchor=west,capx] at (n6.east) (n7) {leer};

\draw[
   decorate,
  decoration={brace,amplitude=10pt}
]
  ([yshift=4pt]n0.north west) --
  node [black,yshift=18pt] {size()}
  ([yshift=4pt]n4.north east) ;

\draw[
  decorate,
  decoration={brace,amplitude=10pt,mirror},
  color=black!40
]
  ([yshift=-4pt]n0.south west) --
    node [black,yshift=-18pt,color=black!40]  {capacity()}
  ([yshift=-4pt]n7.south east) ;

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容