1

I'm drawing something like a linked list, but my rectangles are pretty small, how can I increase their size?

\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}
  • for rectangle split horizontal you can only determine height of shape for whole shape. width can be determined only for each part separately as for example \nodepart[minimum width=7mm]{second} – Zarko Mar 28 '17 at 22:40
  • @Zarko and how can I specify height? – typos Mar 28 '17 at 22:42
  • minimum height=..., you should look in PGF & TikZ manual. I will prepare short answer. – Zarko Mar 28 '17 at 22:43
1

enter image description here

\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}

of course, it is simpler just increase inner sep as suggest AboAmmar in his answer, but with his solution can width of shape parts wary. Anyway, see how the code of arrows are changed. Your approach is at least strange :).

Edit: I expand above MWE with case of vertical for vertical align. Distance between nodes are determined with node distance.

I also change the numbers in shape part, that you can see, that the width of parts is independent of it content (when it is shorter then `text width, of course).

  • Thanks for the answer. But one more question, if I use something like this, and if I want to put some nodes below another one it gets shifted a bit to the right, is not exactly below. For example if we do \node[list,below of=A] (B).... How can I make so that they are aligned when they are one below the other and the distance between them is visible, like in horizontal case? – typos Mar 28 '17 at 22:57
1

Like this?

enter image description here

When the rectangle is split horizontally, minimum height specifications will be effective, but any minimum width specifications will be ignored. And the inner sep will be applied to every part that is used. So, here I add 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}
  • Yes, that's more like it. But the problem with using below of still persists. Please see below my comment to @Zarko's answer. – typos Mar 28 '17 at 22:59
  • @typos : Please edit your question to add this extra requirement. – AboAmmar Mar 28 '17 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.