如何增加 TikZ 中矩形的尺寸?

如何增加 TikZ 中矩形的尺寸?

我正在绘制类似链表的东西,但是我的矩形非常小,如何增加它们的尺寸?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,chains,arrows}

\begin{document}
\begin{tikzpicture}[list/.style={rectangle split, rectangle split parts=2,
    draw, rectangle split horizontal}, >=stealth, start chain]

  \node[list,on chain] (A) {1};
  \node[list,on chain] (B) {2};
  \node[list,on chain] (C) {2\nodepart{second} 3};

  \draw[*->] let \p1 = (A.two), \p2 = (A.center) in (\x1,\y2) -- (B);
  \draw[*->] let \p1 = (B.two), \p2 = (B.center) in (\x1,\y2) -- (C);

\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, chains, calc, positioning, shapes.multipart}

\begin{document}
\begin{tikzpicture}[
node distance = 9mm and 12mm, % <vertical distance> and <horizontal distance> between nodes
  start chain = going right,
    mw/.style = {minimum width=#1},% for shortcut in "list" shapes
  list/.style = {rectangle split, rectangle split parts=2,
                 rectangle split horizontal, draw,
                 align=center,
                 text width=7mm, % <-- have effect only on the foirst part of shape 
                 minimum height=9mm, % define height od multi part shape
                 inner sep=1mm, on chain}, 
          > = stealth, 
          ]

  \node[list] (A) {1\nodepart[mw=7mm]{two}  };
  \node[list] (B) {2\nodepart[mw=7mm]{two}  };
  \node[list] (C) {2\nodepart[mw=7mm]{two} 3};

  \node[list, below=of A] (D) {4\nodepart[mw=7mm]{two}  };% <-- observe correct syntax of "positioning" library
  \node[list] (E) {5\nodepart[mw=7mm]{two}  };
  \node[list] (F) {6\nodepart[mw=7mm]{two} 3};

  \draw[*->] ($(A.two north)!0.5!(A.two south)$) -- (B);
  \draw[*->] ($(B.two north)!0.5!(B.two south)$) -- (C);
\end{tikzpicture}
\end{document}

当然,更简单的是增加inner sep建议阿博阿马尔在他的回答中,但他的解决方案可以谨慎对待形状部分的宽度。无论如何,看看箭头的代码是如何改变的。你的方法至少很奇怪:)。

编辑: 我扩展了 MWE 上面的内容,使用垂直对齐的情况。节点之间的距离用 确定node distance

我还改变了形状部分的数字,您可以看到,部分的宽度与其内容无关(当然,当它短于文本宽度时)。

答案2

像这样?

在此处输入图片描述

当矩形水平分割时,minimum height规范将生效,但任何minimum width规范都将被忽略。并且inner sep将应用于使用的每个部分。所以,我在这里添加了inner sep=1em

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.multipart,chains,arrows}

\begin{document}
\begin{tikzpicture}[list/.style={rectangle split, rectangle split parts=2, draw, 
  rectangle split horizontal, inner sep=1em}, >=stealth, start chain]

  \node[list,on chain] (A) {1};
  \node[list,on chain] (B) {2};
  \node[list,on chain] (C) {2\nodepart{second} 3};

  \draw[*->] let \p1 = (A.two), \p2 = (A.center) in (\x1,\y2) -- (B);
  \draw[*->] let \p1 = (B.two), \p2 = (B.center) in (\x1,\y2) -- (C);

\end{tikzpicture}
\end{document}

相关内容